From fac762c84d5832edf4ef1ce2df2b34a19699351d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Abdulkadir=20Furkan=20=C5=9Eanl=C4=B1?= Date: Sun, 19 Jul 2026 15:30:31 +0200 Subject: [PATCH] Time limit for radio. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Abdulkadir Furkan Şanlı --- main.py | 50 +++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 41 insertions(+), 9 deletions(-) diff --git a/main.py b/main.py index 48b8493..2e26fcd 100755 --- a/main.py +++ b/main.py @@ -182,28 +182,44 @@ def add_video_to_playlist(youtube, playlist_id, video_id, retry_count=6): raise error +def parse_iso8601_duration(duration_str): + """Parse ISO 8601 duration string into seconds.""" + pattern = re.compile(r'PT(?:(\d+)H)?(?:(\d+)M)?(?:(\d+)S)?') + match = pattern.match(duration_str) + if not match: + return 0 + hours = int(match.group(1)) if match.group(1) else 0 + minutes = int(match.group(2)) if match.group(2) else 0 + seconds = int(match.group(3)) if match.group(3) else 0 + return hours * 3600 + minutes * 60 + seconds + + def get_video_info(youtube, video_id): - """Check whether a YouTube video is music and return its title and channel.""" + """Check whether a YouTube video is music and return its title, channel, and duration.""" try: - video_details = youtube.videos().list(id=video_id, part="snippet").execute() + video_details = youtube.videos().list(id=video_id, part="snippet,contentDetails").execute() if not video_details.get("items"): - return False, "[Video unavailable or private]", "" + return False, "[Video unavailable or private]", "", 0 - snippet = video_details["items"][0]["snippet"] + item = video_details["items"][0] + snippet = item["snippet"] + content_details = item.get("contentDetails", {}) + duration_str = content_details.get("duration", "PT0S") + duration = parse_iso8601_duration(duration_str) is_music = snippet.get("categoryId") in ("10", "24") title = snippet.get("title", "[Unknown Title]") channel = snippet.get("channelTitle", "[Unknown Channel]") - return is_music, title, channel + return is_music, title, channel, duration except errors.HttpError as error: print(f"YouTube API error fetching info for {video_id}: {error}") - return False, "[Error fetching title]", "" + return False, "[Error fetching title]", "", 0 except Exception as e: print(f"Unexpected error fetching info for {video_id}: {e}") - return False, "[Error fetching title]", "" + return False, "[Error fetching title]", "", 0 async def send_intro_message(client, sender, room_id): @@ -299,7 +315,7 @@ async def message_callback(conn, cursor, youtube, client, room, event): for link in youtube_links: video_id = link.split("v=")[-1].split("&")[0].split("/")[-1] - is_music_vid, title, channel = get_video_info(youtube, video_id) + is_music_vid, title, channel, duration = get_video_info(youtube, video_id) if recent: plain_text = f"{title}" @@ -322,6 +338,19 @@ async def message_callback(conn, cursor, youtube, client, room, event): }, ) + if is_music_vid and duration > 1200: + warning_text = f"⚠️ Track is too long for the radio ({(duration // 60)}m {duration % 60}s). Skipped radio upload." + await client.room_send( + room_id=room.room_id, + message_type="m.room.message", + content={ + "msgtype": "m.text", + "body": warning_text, + "format": "org.matrix.custom.html", + "formatted_body": f"{html.escape(warning_text)}", + }, + ) + if is_music_vid: message_id = record_message(conn, cursor, sender, link, timestamp) if in_playlist(cursor, video_id, playlist_id): @@ -339,7 +368,10 @@ async def message_callback(conn, cursor, youtube, client, room, event): ) print(f"Added track to this week's playlist: {link}") if recent: - asyncio.create_task(process_radio_track(link, video_id, title)) + if duration <= 1200: + asyncio.create_task(process_radio_track(link, video_id, title)) + else: + print(f"📻 Skipping radio upload for {title} (duration: {duration}s > 1200s)") def in_playlist(cursor, video_id, playlist_id):