import threading import time import os import json from get_playlists import update_playlist_if_needed CONFIG_PATH = os.path.join(os.path.dirname(__file__), 'main_data', 'app_config.txt') MEDIA_DATA_PATH = os.path.join(os.path.dirname(__file__), 'static_data', 'media') PLAYLIST_DIR = os.path.join(os.path.dirname(__file__), 'static_data', 'playlist') LOCAL_PLAYLIST_PATH = os.path.join(PLAYLIST_DIR, 'server_playlist.json') def playlist_update_loop(refresh_time, stop_event, config, local_playlist_path, media_dir, playlist_dir): while not stop_event.is_set(): updated = update_playlist_if_needed(local_playlist_path, config, media_dir, playlist_dir) if updated: print(f"[REFRESH] Playlist updated from server at {time.strftime('%X')}") else: print(f"[REFRESH] Playlist already up to date at {time.strftime('%X')}") time.sleep(refresh_time) def main(): with open(CONFIG_PATH, 'r') as f: config = json.load(f) refresh_time = int(config.get('refresh_time', 5)) * 60 # minutes stop_event = threading.Event() update_thread = threading.Thread( target=playlist_update_loop, args=(refresh_time, stop_event, config, LOCAL_PLAYLIST_PATH, MEDIA_DATA_PATH, PLAYLIST_DIR), daemon=True ) update_thread.start() print("Playlist update thread started. Press Ctrl+C to exit.") try: while True: time.sleep(1) except KeyboardInterrupt: print("Exiting...") stop_event.set() update_thread.join() if __name__ == '__main__': main()