Add all-time playlist

Signed-off-by: Abdulkadir Furkan Şanlı <me@abdulocra.cy>
This commit is contained in:
Abdulkadir Furkan Şanlı 2024-01-25 17:57:05 +01:00
parent 5a3ace4297
commit f84b214ab0
Signed by: afk
SSH Key Fingerprint: SHA256:s1hULLl4YWdqU501MUfGe1CAG/m1pf9Cs6vFsqeTNHk

24
main.py
View File

@ -139,7 +139,7 @@ def make_playlist(youtube, title):
def get_or_make_playlist(conn, cursor, youtube, playlist_date):
"""Get ID of playlist for given Monday's week, make if doesn't exist."""
"""Get ID of playlist with given named suffix, make if doesn't exist."""
title = f"{YOUTUBE_PLAYLIST_TITLE} {playlist_date.strftime('%Y-%m-%d')}"
cursor.execute("SELECT playlist_id FROM playlists WHERE title = ?", (title,))
@ -232,6 +232,16 @@ async def send_playlist_of_week(client, sender, room_id, playlist_id):
content={"msgtype": "m.text", "body": reply_msg},
)
async def send_playlist_of_week(client, sender, room_id, playlist_id):
"""Sends playlist of all time in reply to sender, in room with room_id."""
playlist_link = f"https://www.youtube.com/playlist?list={playlist_id}"
reply_msg = f"{sender}, here's the playlist of all time: {playlist_link}"
await client.room_send(
room_id=room_id,
message_type="m.room.message",
content={"msgtype": "m.text", "body": reply_msg},
)
async def message_callback(conn, cursor, youtube, client, room, event):
"""Event handler for received messages."""
@ -242,6 +252,9 @@ async def message_callback(conn, cursor, youtube, client, room, event):
playlist_id = get_or_make_playlist(
conn, cursor, youtube, monday_date(timestamp)
)
all_playlist_id = get_or_make_playlist(
conn, cursor, youtube, datetime.datetime.fromtimestamp(0)
)
timestamp_sec = datetime.datetime.fromtimestamp(
event.server_timestamp / 1000, datetime.UTC # millisec to sec
@ -253,7 +266,11 @@ async def message_callback(conn, cursor, youtube, client, room, event):
await send_intro_message(client, sender, room.room_id)
return
if body == "!pow" and recent:
if body == "!week" and recent:
await send_playlist_of_week(client, sender, room.room_id, playlist_id)
return
if body == "!all" and recent:
await send_playlist_of_week(client, sender, room.room_id, playlist_id)
return
@ -270,8 +287,9 @@ async def message_callback(conn, cursor, youtube, client, room, event):
if in_playlist(cursor, video_id, playlist_id):
print(f"Track is already in this week's playlist: {link}")
else:
# Add video to playlist and record it in the database
# Add video to playlists and record it in the database
add_video_to_playlist(youtube, playlist_id, video_id)
add_video_to_playlist(youtube, all_playlist_id, video_id)
with conn:
cursor.execute(
(