Files
digiserver/test_api.py
2025-04-12 11:44:59 +03:00

44 lines
1.5 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 = 'tv1'
quickconnect_code = '4321'
# Construct the URL with the hostname and quick connect code as query parameters
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
playlist = response.json().get('playlist', [])
print('Playlist:', playlist)
# Download each file in the playlist
for item in playlist:
file_url = item['url']
file_name = item['file_name']
response = requests.get(file_url)
if response.status_code == 200:
with open(file_name, 'wb') as f:
f.write(response.content)
print(f'Downloaded {file_name}')
else:
print(f'Failed to download {file_name}: {response.status_code}')
except requests.exceptions.JSONDecodeError as e:
print(f'Failed to parse JSON response: {e}')
else:
print('Failed to retrieve playlist:', response.text)