2024-01-19 11:12:06 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
"""parkerbot: Matrix bot to generate YouTube (music) playlists from links sent to a channel."""
|
|
|
|
|
2024-01-21 09:42:28 +01:00
|
|
|
import asyncio
|
2024-01-19 11:12:06 +01:00
|
|
|
import os
|
2024-01-21 09:42:28 +01:00
|
|
|
import pickle
|
2024-01-19 11:12:06 +01:00
|
|
|
import re
|
|
|
|
import sqlite3
|
2024-01-21 10:38:05 +01:00
|
|
|
import sys
|
2024-01-19 11:12:06 +01:00
|
|
|
from datetime import datetime, timedelta
|
2024-01-21 09:42:28 +01:00
|
|
|
|
|
|
|
from google.auth.transport.requests import Request
|
2024-01-19 11:12:06 +01:00
|
|
|
from google_auth_oauthlib.flow import InstalledAppFlow
|
|
|
|
from googleapiclient.discovery import build
|
2024-01-21 10:38:05 +01:00
|
|
|
from nio import AsyncClient, RoomMessageText, SyncResponse
|
|
|
|
|
|
|
|
DATA_DIR = os.getenv("DATA_DIR", "./")
|
|
|
|
DB_PATH = os.path.join(DATA_DIR, "parkerbot.sqlite3")
|
|
|
|
TOKEN_PATH = os.path.join(DATA_DIR, "sync_token")
|
|
|
|
PICKLE_PATH = os.path.join(DATA_DIR, "token.pickle")
|
2024-01-19 11:12:06 +01:00
|
|
|
|
|
|
|
MATRIX_SERVER = os.getenv("MATRIX_SERVER")
|
|
|
|
MATRIX_ROOM = os.getenv("MATRIX_ROOM")
|
|
|
|
MATRIX_USER = os.getenv("MATRIX_USER")
|
|
|
|
MATRIX_PASSWORD = os.getenv("MATRIX_PASSWORD")
|
2024-01-21 10:38:05 +01:00
|
|
|
|
|
|
|
YOUTUBE_PLAYLIST_TITLE = os.getenv("YOUTUBE_PLAYLIST_TITLE")
|
2024-01-19 11:12:06 +01:00
|
|
|
YOUTUBE_CLIENT_SECRETS_FILE = os.getenv("YOUTUBE_CLIENT_SECRETS_FILE")
|
|
|
|
YOUTUBE_API_SERVICE_NAME = "youtube"
|
|
|
|
YOUTUBE_API_VERSION = "v3"
|
|
|
|
|
|
|
|
conn = sqlite3.connect(DB_PATH)
|
|
|
|
cursor = conn.cursor()
|
|
|
|
|
|
|
|
|
2024-01-21 09:42:28 +01:00
|
|
|
def define_tables():
|
|
|
|
"""Define tables for use with program."""
|
2024-01-19 11:12:06 +01:00
|
|
|
with conn:
|
|
|
|
cursor.execute(
|
|
|
|
"""CREATE TABLE IF NOT EXISTS messages (
|
|
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
|
|
sender TEXT,
|
|
|
|
message TEXT,
|
|
|
|
timestamp DATETIME,
|
|
|
|
UNIQUE (sender, message, timestamp))"""
|
|
|
|
)
|
|
|
|
cursor.execute(
|
|
|
|
"""CREATE TABLE IF NOT EXISTS playlists (
|
|
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
|
|
title TEXT,
|
|
|
|
playlist_id TEXT UNIQUE,
|
|
|
|
creation_date DATE)"""
|
|
|
|
)
|
|
|
|
cursor.execute(
|
|
|
|
"""CREATE TABLE IF NOT EXISTS playlist_tracks (
|
|
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
|
|
playlist_id INTEGER,
|
|
|
|
message_id INTEGER,
|
|
|
|
FOREIGN KEY (playlist_id) REFERENCES playlists(id),
|
|
|
|
FOREIGN KEY (message_id) REFERENCES messages(id),
|
|
|
|
UNIQUE (playlist_id, message_id))"""
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def get_authenticated_service():
|
2024-01-21 09:42:28 +01:00
|
|
|
"""Get an authentivated YouTube service."""
|
2024-01-19 11:12:06 +01:00
|
|
|
credentials = None
|
2024-01-21 10:38:05 +01:00
|
|
|
# Stores the user's access and refresh tokens.
|
|
|
|
if os.path.exists(PICKLE_PATH):
|
|
|
|
with open(PICKLE_PATH, "rb") as token:
|
2024-01-19 11:12:06 +01:00
|
|
|
credentials = pickle.load(token)
|
|
|
|
|
|
|
|
# If there are no valid credentials available, let the user log in.
|
|
|
|
if not credentials or not credentials.valid:
|
|
|
|
if credentials and credentials.expired and credentials.refresh_token:
|
|
|
|
credentials.refresh(Request())
|
|
|
|
else:
|
|
|
|
flow = InstalledAppFlow.from_client_secrets_file(
|
|
|
|
YOUTUBE_CLIENT_SECRETS_FILE,
|
|
|
|
scopes=["https://www.googleapis.com/auth/youtube.force-ssl"],
|
|
|
|
)
|
|
|
|
credentials = flow.run_local_server(port=8080)
|
|
|
|
# Save the credentials for the next run
|
2024-01-21 10:38:05 +01:00
|
|
|
with open(PICKLE_PATH, "wb") as token:
|
2024-01-19 11:12:06 +01:00
|
|
|
pickle.dump(credentials, token)
|
|
|
|
|
|
|
|
return build("youtube", "v3", credentials=credentials)
|
|
|
|
|
|
|
|
|
|
|
|
def get_monday_date():
|
2024-01-21 09:42:28 +01:00
|
|
|
"""Get Monday of current week. Weeks start on Monday."""
|
2024-01-19 11:12:06 +01:00
|
|
|
today = datetime.now()
|
|
|
|
return today - timedelta(days=today.weekday())
|
|
|
|
|
|
|
|
|
2024-01-21 09:42:28 +01:00
|
|
|
def make_playlist(youtube, title):
|
|
|
|
"""Make a playlist with given title."""
|
2024-01-19 11:12:06 +01:00
|
|
|
response = (
|
|
|
|
youtube.playlists()
|
|
|
|
.insert(
|
|
|
|
part="snippet,status",
|
|
|
|
body={
|
|
|
|
"snippet": {
|
|
|
|
"title": title,
|
2024-01-21 09:42:28 +01:00
|
|
|
"description": "Weekly playlist generated by ParkerBot",
|
2024-01-19 11:12:06 +01:00
|
|
|
},
|
|
|
|
"status": {"privacyStatus": "public"},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.execute()
|
|
|
|
)
|
|
|
|
|
|
|
|
return response["id"]
|
|
|
|
|
|
|
|
|
2024-01-21 09:42:28 +01:00
|
|
|
def get_or_make_playlist(youtube, monday_date):
|
|
|
|
"""Get ID of playlist for given Monday's week, make if doesn't exist."""
|
2024-01-21 10:38:05 +01:00
|
|
|
playlist_title = f"{YOUTUBE_PLAYLIST_TITLE} {monday_date.strftime('%Y-%m-%d')}"
|
2024-01-19 11:12:06 +01:00
|
|
|
|
|
|
|
# Check if playlist exists in the database
|
|
|
|
cursor.execute(
|
|
|
|
"SELECT playlist_id FROM playlists WHERE title = ?", (playlist_title,)
|
|
|
|
)
|
|
|
|
row = cursor.fetchone()
|
|
|
|
if row:
|
|
|
|
return row[0] # Playlist already exists
|
|
|
|
|
2024-01-21 09:42:28 +01:00
|
|
|
# If not, make a new playlist on YouTube and save it in the database
|
|
|
|
playlist_id = make_playlist(youtube, playlist_title)
|
2024-01-19 11:12:06 +01:00
|
|
|
with conn:
|
|
|
|
cursor.execute(
|
|
|
|
"INSERT INTO playlists (title, playlist_id, creation_date) VALUES (?, ?, ?)",
|
|
|
|
(playlist_title, playlist_id, monday_date),
|
|
|
|
)
|
|
|
|
|
|
|
|
return playlist_id
|
|
|
|
|
|
|
|
|
|
|
|
def add_video_to_playlist(youtube, playlist_id, video_id):
|
2024-01-21 09:42:28 +01:00
|
|
|
"""Add video to playlist."""
|
2024-01-19 11:12:06 +01:00
|
|
|
youtube.playlistItems().insert(
|
|
|
|
part="snippet",
|
|
|
|
body={
|
|
|
|
"snippet": {
|
|
|
|
"playlistId": playlist_id,
|
|
|
|
"resourceId": {"kind": "youtube#video", "videoId": video_id},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
).execute()
|
|
|
|
|
|
|
|
|
2024-01-21 09:42:28 +01:00
|
|
|
def is_music(youtube, video_id):
|
|
|
|
"""Check whether a YouTube video is music."""
|
|
|
|
video_details = youtube.videos().list(id=video_id, part="snippet").execute()
|
|
|
|
|
|
|
|
# Check if the video category is Music (typically category ID 10)
|
|
|
|
return video_details["items"][0]["snippet"]["categoryId"] == "10"
|
|
|
|
|
|
|
|
|
|
|
|
async def message_callback(client, room, event):
|
|
|
|
"""Event handler for received messages."""
|
2024-01-19 11:12:06 +01:00
|
|
|
youtube_link_pattern = r"(https?://(?:www\.|music\.)?youtube\.com/(?!playlist\?list=)watch\?v=[\w-]+|https?://youtu\.be/[\w-]+)"
|
2024-01-21 09:42:28 +01:00
|
|
|
sender = event.sender
|
|
|
|
if sender != MATRIX_USER:
|
|
|
|
body = event.body
|
|
|
|
timestamp = event.server_timestamp
|
2024-01-19 11:12:06 +01:00
|
|
|
room_id = room.room_id
|
|
|
|
monday_date = get_monday_date()
|
|
|
|
youtube = get_authenticated_service()
|
2024-01-21 09:42:28 +01:00
|
|
|
playlist_id = get_or_make_playlist(youtube, monday_date)
|
|
|
|
youtube_links = re.findall(youtube_link_pattern, body)
|
2024-01-19 11:12:06 +01:00
|
|
|
|
2024-01-21 09:42:28 +01:00
|
|
|
if body == "!pow":
|
2024-01-19 11:12:06 +01:00
|
|
|
playlist_link = f"https://www.youtube.com/playlist?list={playlist_id}"
|
|
|
|
reply_msg = f"{sender}, here's the playlist of the week: {playlist_link}"
|
|
|
|
await client.room_send(
|
|
|
|
room_id=room_id,
|
|
|
|
message_type="m.room.message",
|
2024-01-21 09:42:28 +01:00
|
|
|
content={"msgtype": "m.text", "body": reply_msg},
|
2024-01-19 11:12:06 +01:00
|
|
|
)
|
|
|
|
|
2024-01-21 09:42:28 +01:00
|
|
|
for link in youtube_links:
|
2024-01-21 12:29:34 +01:00
|
|
|
video_id = link.split("v=")[-1].split("&")[0].split("/")[-1]
|
2024-01-21 09:42:28 +01:00
|
|
|
if is_music(youtube, video_id):
|
2024-01-19 11:12:06 +01:00
|
|
|
try:
|
|
|
|
cursor.execute(
|
|
|
|
"INSERT INTO messages (sender, message, timestamp) VALUES (?, ?, ?)",
|
|
|
|
(sender, link, timestamp),
|
|
|
|
)
|
|
|
|
conn.commit()
|
|
|
|
print(f"Saved YouTube link from {sender}: {link}")
|
|
|
|
except sqlite3.IntegrityError as e:
|
|
|
|
if "UNIQUE constraint failed" in str(e):
|
|
|
|
print(f"Entry already exists: {sender} {link} {timestamp}")
|
|
|
|
else:
|
|
|
|
raise e
|
|
|
|
|
|
|
|
# Check if the link is already added to any playlist
|
|
|
|
cursor.execute("SELECT id FROM messages WHERE message = ?", (link,))
|
|
|
|
message_row = cursor.fetchone()
|
|
|
|
|
|
|
|
if message_row:
|
|
|
|
cursor.execute(
|
|
|
|
"SELECT id FROM playlist_tracks WHERE message_id = ? AND playlist_id = ?",
|
|
|
|
(message_row[0], playlist_id),
|
|
|
|
)
|
|
|
|
track_row = cursor.fetchone()
|
|
|
|
|
|
|
|
if track_row:
|
|
|
|
print(f"Track already in playlist: {link}")
|
|
|
|
else:
|
|
|
|
# Add video to playlist and record it in the database
|
|
|
|
add_video_to_playlist(youtube, playlist_id, video_id)
|
|
|
|
with conn:
|
|
|
|
cursor.execute(
|
|
|
|
"INSERT INTO playlist_tracks (playlist_id, message_id) VALUES (?, ?)",
|
|
|
|
(playlist_id, message_row[0]),
|
|
|
|
)
|
|
|
|
print(f"Added track to playlist: {link}")
|
|
|
|
|
|
|
|
|
2024-01-21 09:42:28 +01:00
|
|
|
async def sync_callback(response):
|
2024-01-21 10:38:05 +01:00
|
|
|
"""Save Matrix sync token."""
|
2024-01-21 09:42:28 +01:00
|
|
|
# Save the sync token to a file or handle it as needed
|
2024-01-21 10:38:05 +01:00
|
|
|
with open(TOKEN_PATH, "w") as f:
|
2024-01-21 09:42:28 +01:00
|
|
|
f.write(response.next_batch)
|
|
|
|
|
|
|
|
|
|
|
|
def load_sync_token():
|
2024-01-21 10:38:05 +01:00
|
|
|
"""Get an existing Matrix sync token if it exists."""
|
2024-01-21 09:42:28 +01:00
|
|
|
try:
|
2024-01-21 10:38:05 +01:00
|
|
|
with open(TOKEN_PATH, "r") as file:
|
2024-01-21 09:42:28 +01:00
|
|
|
return file.read().strip()
|
|
|
|
except FileNotFoundError:
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
async def get_client():
|
2024-01-21 10:38:05 +01:00
|
|
|
"""Returns configured and logged in Matrix client."""
|
2024-01-19 11:12:06 +01:00
|
|
|
client = AsyncClient(MATRIX_SERVER, MATRIX_USER)
|
2024-01-21 09:42:28 +01:00
|
|
|
client.add_event_callback(
|
|
|
|
lambda room, event: message_callback(client, room, event), RoomMessageText
|
|
|
|
)
|
|
|
|
client.add_response_callback(sync_callback, SyncResponse)
|
2024-01-19 11:12:06 +01:00
|
|
|
print(await client.login(MATRIX_PASSWORD))
|
|
|
|
await client.join(MATRIX_ROOM)
|
2024-01-21 09:42:28 +01:00
|
|
|
return client
|
2024-01-19 11:12:06 +01:00
|
|
|
|
|
|
|
|
2024-01-21 09:42:28 +01:00
|
|
|
async def main():
|
|
|
|
"""Get DB and Matrix client ready, and start syncing."""
|
|
|
|
define_tables()
|
|
|
|
client = await get_client()
|
|
|
|
sync_token = load_sync_token()
|
2024-01-21 12:29:34 +01:00
|
|
|
await client.sync_forever(30000, full_state=True, since=sync_token)
|
2024-01-21 10:38:05 +01:00
|
|
|
|
2024-01-21 09:42:28 +01:00
|
|
|
|
2024-01-19 11:12:06 +01:00
|
|
|
if __name__ == "__main__":
|
|
|
|
asyncio.run(main())
|