updated to new files
This commit is contained in:
58
src/python_functions.py
Normal file
58
src/python_functions.py
Normal file
@@ -0,0 +1,58 @@
|
||||
import requests
|
||||
import os
|
||||
import json
|
||||
from kivy.logger import Logger
|
||||
|
||||
def load_playlist():
|
||||
# Load playlist from the server or local storage
|
||||
try:
|
||||
Logger.debug("Attempting to load playlist from server...")
|
||||
server_ip = 'http://192.168.0.115:5000'
|
||||
hostname = 'TvHolBa1'
|
||||
quickconnect_code = 'Initial01!'
|
||||
url = f'{server_ip}/api/playlists'
|
||||
params = {
|
||||
'hostname': hostname,
|
||||
'quickconnect_code': quickconnect_code
|
||||
}
|
||||
response = requests.get(url, params=params)
|
||||
|
||||
# Print the raw response content and status code for debugging
|
||||
Logger.debug(f'Status Code: {response.status_code}')
|
||||
Logger.debug(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', [])
|
||||
Logger.debug(f'Playlist: {playlist}')
|
||||
return playlist
|
||||
except json.JSONDecodeError as e:
|
||||
Logger.error(f'Failed to parse JSON response: {e}')
|
||||
else:
|
||||
Logger.error(f'Failed to retrieve playlist: {response.text}')
|
||||
except requests.exceptions.RequestException as e:
|
||||
Logger.error(f"Failed to load playlist: {e}")
|
||||
return []
|
||||
|
||||
def download_media_files(playlist):
|
||||
base_dir = os.path.join(os.path.dirname(__file__), 'static', 'resurse') # Update this to the correct path
|
||||
if not os.path.exists(base_dir):
|
||||
os.makedirs(base_dir)
|
||||
|
||||
for media in playlist:
|
||||
file_name = media.get('file_name', '')
|
||||
file_url = media.get('url', '')
|
||||
file_path = os.path.join(base_dir, file_name)
|
||||
|
||||
try:
|
||||
response = requests.get(file_url)
|
||||
if response.status_code == 200:
|
||||
with open(file_path, 'wb') as file:
|
||||
file.write(response.content)
|
||||
Logger.debug(f"Downloaded {file_name} to {file_path}")
|
||||
else:
|
||||
Logger.error(f"Failed to download {file_name}: {response.status_code}")
|
||||
except requests.exceptions.RequestException as e:
|
||||
Logger.error(f"Failed to download {file_name}: {e}")
|
||||
Reference in New Issue
Block a user