2022-09-11 23:14:51 +02:00
|
|
|
|
from typing import cast, Optional
|
|
|
|
|
|
2022-11-28 23:29:22 +01:00
|
|
|
|
from googleapiclient.discovery import build as gapi_discovery_build
|
|
|
|
|
from googleapiclient.errors import Error as GoogleApiError
|
2020-11-03 16:15:23 +01:00
|
|
|
|
|
|
|
|
|
from edmond.plugin import Plugin
|
2022-09-11 23:14:51 +02:00
|
|
|
|
from edmond.plugins.playlist_of_the_day import PlaylistOfTheDayPlugin
|
2020-11-03 16:15:23 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class YoutubePlugin(Plugin):
|
|
|
|
|
|
|
|
|
|
REQUIRED_CONFIGS = ["commands", "api_key"]
|
|
|
|
|
VIDEO_URL_FMT = "https://www.youtube.com/watch?v={}"
|
|
|
|
|
CHANNEL_URL_FMT = "https://www.youtube.com/channel/{}"
|
|
|
|
|
PLAYLIST_URL_FMT = "https://www.youtube.com/playlist?list={}"
|
|
|
|
|
|
|
|
|
|
def __init__(self, bot):
|
|
|
|
|
super().__init__(bot)
|
|
|
|
|
self._youtube = None
|
2022-09-11 23:14:51 +02:00
|
|
|
|
self._playlist_of_the_day_plugin = None
|
2020-11-03 16:15:23 +01:00
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def youtube(self):
|
|
|
|
|
if self._youtube is None:
|
|
|
|
|
self._youtube = gapi_discovery_build(
|
2022-09-11 23:14:51 +02:00
|
|
|
|
"youtube",
|
|
|
|
|
"v3",
|
|
|
|
|
developerKey=self.config["api_key"],
|
2020-11-03 16:15:23 +01:00
|
|
|
|
)
|
|
|
|
|
return self._youtube
|
|
|
|
|
|
2022-09-11 23:14:51 +02:00
|
|
|
|
@property
|
|
|
|
|
def playlist_of_the_day_plugin(self) -> Optional[PlaylistOfTheDayPlugin]:
|
|
|
|
|
if self._playlist_of_the_day_plugin is None:
|
|
|
|
|
self._playlist_of_the_day_plugin = cast(
|
|
|
|
|
PlaylistOfTheDayPlugin,
|
|
|
|
|
self.bot.get_plugin("playlistoftheday"),
|
|
|
|
|
)
|
|
|
|
|
return self._playlist_of_the_day_plugin
|
|
|
|
|
|
|
|
|
|
def on_welcome(self, _):
|
|
|
|
|
if not self.config["api_key"]:
|
|
|
|
|
self.bot.log_w("API key unavailable.")
|
|
|
|
|
self.is_ready = False
|
2020-11-03 16:15:23 +01:00
|
|
|
|
|
|
|
|
|
def on_pubmsg(self, event):
|
|
|
|
|
if self.should_handle_command(event.arguments[0]):
|
|
|
|
|
self.handle_commands(event.target)
|
|
|
|
|
return True
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
def handle_commands(self, target):
|
2022-08-15 13:21:24 +02:00
|
|
|
|
if self.command.ident != self.config["commands"][0]:
|
|
|
|
|
return
|
|
|
|
|
try:
|
|
|
|
|
search_response = (
|
|
|
|
|
self.youtube.search()
|
|
|
|
|
.list(
|
|
|
|
|
q=self.command.content,
|
|
|
|
|
part="id,snippet",
|
|
|
|
|
maxResults=1,
|
2022-08-09 23:47:28 +02:00
|
|
|
|
)
|
2022-08-15 13:21:24 +02:00
|
|
|
|
.execute()
|
|
|
|
|
)
|
|
|
|
|
except GoogleApiError:
|
|
|
|
|
self.signal_failure(target)
|
|
|
|
|
return
|
2020-11-03 16:15:23 +01:00
|
|
|
|
|
2022-08-15 13:21:24 +02:00
|
|
|
|
link = ""
|
|
|
|
|
icon = ""
|
|
|
|
|
title = ""
|
|
|
|
|
for result in search_response.get("items", []):
|
|
|
|
|
kind = result["id"]["kind"]
|
|
|
|
|
if kind == "youtube#video":
|
|
|
|
|
video_id = result["id"]["videoId"]
|
|
|
|
|
link = self.VIDEO_URL_FMT.format(video_id)
|
|
|
|
|
icon = "📼"
|
|
|
|
|
elif kind == "youtube#channel":
|
|
|
|
|
channel_id = result["id"]["channelId"]
|
|
|
|
|
link = self.CHANNEL_URL_FMT.format(channel_id)
|
|
|
|
|
icon = "📺"
|
|
|
|
|
elif kind == "youtube#playlist":
|
|
|
|
|
playlist_id = result["id"]["playlistId"]
|
|
|
|
|
link = self.PLAYLIST_URL_FMT.format(playlist_id)
|
|
|
|
|
icon = "➕"
|
|
|
|
|
if link:
|
|
|
|
|
title = result["snippet"]["title"]
|
|
|
|
|
break
|
|
|
|
|
else:
|
|
|
|
|
self.signal_failure(target)
|
|
|
|
|
return
|
|
|
|
|
self.bot.say(target, f"{icon} {link} {title}")
|
2022-09-11 23:14:51 +02:00
|
|
|
|
|
|
|
|
|
if self.playlist_of_the_day_plugin:
|
|
|
|
|
self.playlist_of_the_day_plugin.add_line(f"{link} {title}")
|