diff --git a/main.py b/main.py index 1775f5d..0086e96 100755 --- a/main.py +++ b/main.py @@ -459,13 +459,12 @@ async def process_radio_track(video_link, video_id, title): "yt-dlp", "--no-cache-dir", "--extract-audio", - "--output", f"{base_filepath}.%(ext)s", - video_link + "--output", + f"{base_filepath}.%(ext)s", + video_link, ] process = await asyncio.create_subprocess_exec( - *dl_cmd, - stdout=asyncio.subprocess.PIPE, - stderr=asyncio.subprocess.PIPE + *dl_cmd, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE ) stdout, stderr = await process.communicate() if process.returncode != 0: @@ -480,19 +479,22 @@ async def process_radio_track(video_link, video_id, title): return filepath = downloaded_files[0] + filename = os.path.basename(filepath) print(f"📻 Uploading to AzuraCast: {title}") - # 2. Upload to AzuraCast using curl + # 2. Upload to AzuraCast using curl with the correct multipart endpoint upload_cmd = [ "curl", "-s", "-X", "POST", - f"{AZURACAST_URL}/api/station/{AZURACAST_STATION_ID}/files", + f"{AZURACAST_URL}/api/station/{AZURACAST_STATION_ID}/files/upload", "-H", f"X-API-Key: {AZURACAST_API_KEY}", "-F", + f"path={filename}", + "-F", f"file=@{filepath}", ] @@ -506,6 +508,35 @@ async def process_radio_track(video_link, video_id, title): response = json.loads(stdout.decode()) unique_id = response.get("unique_id") + # If the /upload endpoint doesn't return the ID directly, fetch it via search + if not unique_id: + search_cmd = [ + "curl", + "-s", + "-G", + f"{AZURACAST_URL}/api/station/{AZURACAST_STATION_ID}/files", + "--data-urlencode", + f"search={filename}", + "-H", + f"X-API-Key: {AZURACAST_API_KEY}", + ] + search_proc = await asyncio.create_subprocess_exec( + *search_cmd, + stdout=asyncio.subprocess.PIPE, + stderr=asyncio.subprocess.DEVNULL, + ) + search_out, _ = await search_proc.communicate() + + search_data = json.loads(search_out.decode()) + if ( + isinstance(search_data, dict) + and "rows" in search_data + and len(search_data["rows"]) > 0 + ): + unique_id = search_data["rows"][0].get("unique_id") + elif isinstance(search_data, list) and len(search_data) > 0: + unique_id = search_data[0].get("unique_id") + if unique_id: req_cmd = [ "curl",