Files
digiserver/test_api.py
2025-06-20 16:33:21 +03:00

60 lines
2.3 KiB
Python

import requests
import os
# Replace with the actual server IP address or domain name, hostname, and quick connect code
server_ip = 'http://localhost:5000'
hostname = 'rpi-tv11'
quickconnect_code = '8887779'
# Construct the URL for the playlist API
url = f'{server_ip}/api/playlists'
params = {
'hostname': hostname,
'quickconnect_code': quickconnect_code
}
# Make the GET request to the API
response = requests.get(url, params=params)
# Print the raw response content and status code for debugging
print(f'Status Code: {response.status_code}')
print(f'Response Content: {response.text}')
# Check if the request was successful
if response.status_code == 200:
try:
# Parse the JSON response
response_data = response.json()
playlist = response_data.get('playlist', [])
playlist_version = response_data.get('playlist_version', None)
print(f'Playlist Version: {playlist_version}')
print(f'Playlist: {playlist}')
# Define the local folder for saving files
local_folder = './static/resurse'
if not os.path.exists(local_folder):
os.makedirs(local_folder)
# Download each file in the playlist
for media in playlist:
file_name = media.get('file_name', '')
file_url = media.get('url', '')
duration = media.get('duration', 10) # Default duration if not provided
local_file_path = os.path.join(local_folder, file_name)
print(f'Downloading {file_name} from {file_url}...')
try:
file_response = requests.get(file_url, timeout=10)
if file_response.status_code == 200:
with open(local_file_path, 'wb') as file:
file.write(file_response.content)
print(f'Successfully downloaded {file_name} to {local_file_path}')
else:
print(f'Failed to download {file_name}. Status Code: {file_response.status_code}')
except requests.exceptions.RequestException as e:
print(f'Error downloading {file_name}: {e}')
except requests.exceptions.JSONDecodeError as e:
print(f'Failed to parse JSON response: {e}')
else:
print(f'Failed to retrieve playlist. Status Code: {response.status_code}')