diff --git a/main.py b/main.py index e5c8ed5..8f44b0f 100755 --- a/main.py +++ b/main.py @@ -481,11 +481,15 @@ async def process_radio_track(video_link, video_id, title): ) stdout, _ = await curl_proc.communicate() + unique_id = None + azura_path = None + try: response = json.loads(stdout.decode()) unique_id = response.get("unique_id") + azura_path = response.get("path") or response.get("file") - if not unique_id: + if not unique_id or not azura_path: search_cmd = [ "curl", "-s", @@ -509,33 +513,19 @@ async def process_radio_track(video_link, video_id, title): and "rows" in search_data and len(search_data["rows"]) > 0 ): - unique_id = search_data["rows"][0].get("unique_id") + unique_id = unique_id or search_data["rows"][0].get("unique_id") + azura_path = azura_path or search_data["rows"][0].get("path") elif isinstance(search_data, list) and len(search_data) > 0: - unique_id = search_data[0].get("unique_id") + unique_id = unique_id or search_data[0].get("unique_id") + azura_path = azura_path or search_data[0].get("path") - if unique_id: - req_cmd = [ - "curl", - "-s", - "-X", - "POST", - f"{base_url}/api/station/{AZURACAST_STATION_ID}/request/{unique_id}", - "-H", - f"X-API-Key: {AZURACAST_API_KEY}", - ] - req_proc = await asyncio.create_subprocess_exec( - *req_cmd, - stdout=asyncio.subprocess.DEVNULL, - stderr=asyncio.subprocess.DEVNULL, - ) - await req_proc.communicate() - print(f"✅ Queued on radio: {title}") - else: - print(f"⚠️ Uploaded, but no unique_id returned.") except json.JSONDecodeError: - print(f"❌ Failed to parse AzuraCast response") + print(f"❌ Failed to parse AzuraCast upload response") + + # 1. Add the track to the "Currents" playlist FIRST + # AzuraCast rejects requests for files that aren't assigned to any active playlist. + target_path = azura_path if azura_path else filename - # Add the track to the "Currents" playlist immediately try: pl_cmd = [ "curl", @@ -561,7 +551,7 @@ async def process_radio_track(video_link, video_id, title): payload = { "do": "playlist", "playlists": [currents_id], - "files": [filename], + "files": [target_path], } json_path = os.path.join(DATA_DIR, f"assign_{filename}.json") with open(json_path, "w") as f: @@ -587,10 +577,43 @@ async def process_radio_track(video_link, video_id, title): if os.path.exists(json_path): os.remove(json_path) - print(f"✅ Auto-assigned to 'Currents': {title}") + print(f"✅ Auto-assigned to 'Currents' (Path: {target_path})") except Exception as e: print(f"❌ Failed to auto-assign playlist for {title}: {e}") + # 2. Issue the Play Next Request SECOND + # Now that the file is legally in a playlist, AzuraCast will accept the request. + if unique_id: + req_cmd = [ + "curl", + "-s", + "-X", + "POST", + f"{base_url}/api/station/{AZURACAST_STATION_ID}/request/{unique_id}", + "-H", + f"X-API-Key: {AZURACAST_API_KEY}", + ] + req_proc = await asyncio.create_subprocess_exec( + *req_cmd, + stdout=asyncio.subprocess.PIPE, + stderr=asyncio.subprocess.PIPE, + ) + req_stdout, _ = await req_proc.communicate() + + try: + req_resp = json.loads(req_stdout.decode()) + # Check the JSON response directly so we don't lie about success + if req_resp.get("success") or req_resp.get("code") == 200: + print(f"✅ Queued on radio to play next: {title}") + else: + print( + f"⚠️ AzuraCast rejected the request for {title}: {req_stdout.decode()}" + ) + except json.JSONDecodeError: + print(f"⚠️ Failed to queue {title}. API returned: {req_stdout.decode()}") + else: + print(f"⚠️ Uploaded, but no unique_id returned to make the request.") + if os.path.exists(filepath): os.remove(filepath)