youtube: links posted are sent to today's playlist

This commit is contained in:
dece 2022-09-11 23:14:51 +02:00
parent 404f5bb673
commit 65f9e8494a

View file

@ -1,3 +1,5 @@
from typing import cast, Optional
try: try:
from googleapiclient.discovery import build as gapi_discovery_build from googleapiclient.discovery import build as gapi_discovery_build
from googleapiclient.errors import Error as GoogleApiError from googleapiclient.errors import Error as GoogleApiError
@ -7,6 +9,7 @@ except ImportError:
DEPENDENCIES_FOUND = False DEPENDENCIES_FOUND = False
from edmond.plugin import Plugin from edmond.plugin import Plugin
from edmond.plugins.playlist_of_the_day import PlaylistOfTheDayPlugin
class YoutubePlugin(Plugin): class YoutubePlugin(Plugin):
@ -19,21 +22,33 @@ class YoutubePlugin(Plugin):
def __init__(self, bot): def __init__(self, bot):
super().__init__(bot) super().__init__(bot)
self._youtube = None self._youtube = None
self._playlist_of_the_day_plugin = None
@property @property
def youtube(self): def youtube(self):
if self._youtube is None: if self._youtube is None:
self._youtube = gapi_discovery_build( self._youtube = gapi_discovery_build(
"youtube", "v3", developerKey=self.config["api_key"] "youtube",
"v3",
developerKey=self.config["api_key"],
) )
return self._youtube return self._youtube
def has_api_key(self): @property
return self.config["api_key"] != "" 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
def on_pubmsg(self, event): def on_pubmsg(self, event):
if not self.has_api_key():
return False
if self.should_handle_command(event.arguments[0]): if self.should_handle_command(event.arguments[0]):
self.handle_commands(event.target) self.handle_commands(event.target)
return True return True
@ -80,3 +95,6 @@ class YoutubePlugin(Plugin):
self.signal_failure(target) self.signal_failure(target)
return return
self.bot.say(target, f"{icon} {link} {title}") self.bot.say(target, f"{icon} {link} {title}")
if self.playlist_of_the_day_plugin:
self.playlist_of_the_day_plugin.add_line(f"{link} {title}")