Time limit for radio.

Signed-off-by: Abdulkadir Furkan Şanlı <me@afk.pm>
This commit is contained in:
afk
2026-07-19 15:30:31 +02:00
parent 5c990752fb
commit fac762c84d
+41 -9
View File
@@ -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"<em><span data-mx-color='#ff0000'>{html.escape(warning_text)}</span></em>",
},
)
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):