import requests import os import json from kivy.logger import Logger CONFIG_FILE = './Resurse/app_config.txt' def load_config(): """Load configuration from app_config.txt.""" if os.path.exists(CONFIG_FILE): try: with open(CONFIG_FILE, 'r') as file: Logger.info("python_functions: Configuration file loaded successfully.") return json.load(file) except json.JSONDecodeError as e: Logger.error(f"python_functions: Failed to parse configuration file. Error: {e}") return { "screen_orientation": "Landscape", "screen_name": "", "quickconnect_key": "", "server_ip": "", "port": "" } else: Logger.error(f"python_functions: Configuration file {CONFIG_FILE} not found.") return { "screen_orientation": "Landscape", "screen_name": "", "quickconnect_key": "", "server_ip": "", "port": "" } # Load configuration and initialize variables config_data = load_config() server = config_data.get("server_ip", "") host = config_data.get("screen_name", "") quick = config_data.get("quickconnect_key", "") port = config_data.get("port", "") # Determine the configuration status if server and host and quick and port: config_status = "ok" else: config_status = "not_ok" Logger.info(f"python_functions: Configuration loaded: server={server}, host={host}, quick={quick}, port={port}") Logger.info(f"python_functions: Configuration status: {config_status}") def load_playlist(): """Load playlist from the server or local storage.""" try: Logger.info("python_functions: Attempting to load playlist from server...") server_ip = f'{server}:{port}' # Construct the server IP with port url = f'http://{server_ip}/api/playlists' params = { 'hostname': host, 'quickconnect_code': quick } response = requests.get(url, params=params) # Print the raw response content and status code for debugging Logger.debug(f"python_functions: Status Code: {response.status_code}") Logger.debug(f"python_functions: 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.info("python_functions: Playlist loaded successfully.") Logger.debug(f"python_functions: Loaded playlist: {playlist}") return playlist except json.JSONDecodeError as e: Logger.error(f"python_functions: Failed to parse JSON response: {e}") else: Logger.error(f"python_functions: Failed to retrieve playlist: {response.text}") except requests.exceptions.RequestException as e: Logger.error(f"python_functions: Failed to load playlist: {e}") return [] def download_media_files(playlist): """Download media files from the playlist.""" Logger.info("python_functions: Starting media file download...") 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) Logger.info(f"python_functions: Created directory {base_dir} for media files.") 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.info(f"python_functions: Downloaded {file_name} to {file_path}") else: Logger.error(f"python_functions: Failed to download {file_name}: {response.status_code}") except requests.exceptions.RequestException as e: Logger.error(f"python_functions: Failed to download {file_name}: {e}") def clean_unused_files(playlist): """Remove unused media files from the resource folder.""" Logger.info("python_functions: Cleaning unused media files...") base_dir = os.path.join(os.path.dirname(__file__), 'static', 'resurse') # Update this to the correct path if not os.path.exists(base_dir): Logger.debug(f"python_functions: Directory {base_dir} does not exist. No files to clean.") return # Get all file names from the playlist playlist_files = {media.get('file_name', '') for media in playlist} # Get all files in the directory all_files = set(os.listdir(base_dir)) # Determine unused files unused_files = all_files - playlist_files # Delete unused files for file_name in unused_files: file_path = os.path.join(base_dir, file_name) try: os.remove(file_path) Logger.info(f"python_functions: Deleted unused file: {file_path}") except OSError as e: Logger.error(f"python_functions: Failed to delete {file_path}: {e}")