Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 9c1f18fbee | |||
| 98e2683b80 | |||
| cba7a701bf | |||
| 884f1c6dd2 | |||
| 84c245b948 |
@@ -4,6 +4,7 @@
|
|||||||
import argparse
|
import argparse
|
||||||
import asyncio
|
import asyncio
|
||||||
import datetime
|
import datetime
|
||||||
|
import html
|
||||||
import os
|
import os
|
||||||
import pickle
|
import pickle
|
||||||
import re
|
import re
|
||||||
@@ -178,15 +179,30 @@ def add_video_to_playlist(youtube, playlist_id, video_id, retry_count=6):
|
|||||||
raise error
|
raise error
|
||||||
|
|
||||||
|
|
||||||
def is_music(youtube, video_id):
|
def get_video_info(youtube, video_id):
|
||||||
"""Check whether a YouTube video is music."""
|
"""Check whether a YouTube video is music and return its title and channel."""
|
||||||
|
try:
|
||||||
video_details = youtube.videos().list(id=video_id, part="snippet").execute()
|
video_details = youtube.videos().list(id=video_id, part="snippet").execute()
|
||||||
|
|
||||||
# Check if the video category is Music (typically category ID 10)
|
# Check if the video actually exists/is accessible
|
||||||
return video_details["items"][0]["snippet"]["categoryId"] in (
|
if not video_details.get("items"):
|
||||||
"10", # music
|
return False, "[Video unavailable or private]", ""
|
||||||
"24", # entertainment
|
|
||||||
)
|
snippet = video_details["items"][0]["snippet"]
|
||||||
|
|
||||||
|
# Check if the video category is Music (10) or Entertainment (24)
|
||||||
|
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
|
||||||
|
|
||||||
|
except errors.HttpError as error:
|
||||||
|
print(f"YouTube API error fetching info for {video_id}: {error}")
|
||||||
|
return False, "[Error fetching title]", ""
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Unexpected error fetching info for {video_id}: {e}")
|
||||||
|
return False, "[Error fetching title]", ""
|
||||||
|
|
||||||
|
|
||||||
async def send_intro_message(client, sender, room_id):
|
async def send_intro_message(client, sender, room_id):
|
||||||
@@ -261,7 +277,9 @@ async def message_callback(conn, cursor, youtube, client, room, event):
|
|||||||
event.server_timestamp / 1000, datetime.UTC # millisec to sec
|
event.server_timestamp / 1000, datetime.UTC # millisec to sec
|
||||||
)
|
)
|
||||||
current_time = datetime.datetime.now(datetime.UTC)
|
current_time = datetime.datetime.now(datetime.UTC)
|
||||||
recent = current_time - timestamp_sec < datetime.timedelta(seconds=30)
|
|
||||||
|
# Account for up to 5 minutes of clock drift
|
||||||
|
recent = abs(current_time - timestamp_sec) < datetime.timedelta(minutes=5)
|
||||||
|
|
||||||
if body == "!parkerbot" and recent:
|
if body == "!parkerbot" and recent:
|
||||||
await send_intro_message(client, sender, room.room_id)
|
await send_intro_message(client, sender, room.room_id)
|
||||||
@@ -283,7 +301,34 @@ async def message_callback(conn, cursor, youtube, client, room, event):
|
|||||||
|
|
||||||
for link in youtube_links:
|
for link in youtube_links:
|
||||||
video_id = link.split("v=")[-1].split("&")[0].split("/")[-1]
|
video_id = link.split("v=")[-1].split("&")[0].split("/")[-1]
|
||||||
if is_music(youtube, video_id):
|
|
||||||
|
# Safely fetch the category check, title, and channel
|
||||||
|
is_music_vid, title, channel = get_video_info(youtube, video_id)
|
||||||
|
|
||||||
|
# Send the title to the channel so people know what the link is
|
||||||
|
# Only do this for recent messages to prevent spam during backwards-sync
|
||||||
|
if recent:
|
||||||
|
plain_text = f"{title}"
|
||||||
|
if channel:
|
||||||
|
plain_text += f" - {channel}"
|
||||||
|
|
||||||
|
# Escape HTML characters to prevent broken rendering in Matrix
|
||||||
|
escaped_text = html.escape(plain_text)
|
||||||
|
html_text = f"<em><span data-mx-color='#808080'>{escaped_text}</span></em>"
|
||||||
|
|
||||||
|
await client.room_send(
|
||||||
|
room_id=room.room_id,
|
||||||
|
message_type="m.room.message",
|
||||||
|
content={
|
||||||
|
"msgtype": "m.text",
|
||||||
|
"body": plain_text,
|
||||||
|
"format": "org.matrix.custom.html",
|
||||||
|
"formatted_body": html_text
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
# Only add to the playlist if it's categorized as music/entertainment
|
||||||
|
if is_music_vid:
|
||||||
message_id = record_message(conn, cursor, sender, link, timestamp)
|
message_id = record_message(conn, cursor, sender, link, timestamp)
|
||||||
if in_playlist(cursor, video_id, playlist_id):
|
if in_playlist(cursor, video_id, playlist_id):
|
||||||
print(f"Track is already in this week's playlist: {link}")
|
print(f"Track is already in this week's playlist: {link}")
|
||||||
|
|||||||
Reference in New Issue
Block a user