From 0978b43d2c8297ef602340419d1d0e0bb9e727cc Mon Sep 17 00:00:00 2001 From: dece Date: Mon, 15 Aug 2022 13:21:24 +0200 Subject: [PATCH] youtube: add title and an response type icon --- edmond/plugins/youtube.py | 69 ++++++++++++++++++++++----------------- 1 file changed, 39 insertions(+), 30 deletions(-) diff --git a/edmond/plugins/youtube.py b/edmond/plugins/youtube.py index 863fdff..c5759b0 100644 --- a/edmond/plugins/youtube.py +++ b/edmond/plugins/youtube.py @@ -40,35 +40,44 @@ class YoutubePlugin(Plugin): return False def handle_commands(self, target): - if self.command.ident == self.config["commands"][0]: - try: - search_response = ( - self.youtube.search() - .list( - q=self.command.content, - part="id,snippet", - maxResults=1, - ) - .execute() + 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, ) - except GoogleApiError: - self.signal_failure(target) - return + .execute() + ) + except GoogleApiError: + self.signal_failure(target) + return - link = "" - for result in search_response.get("items", []): - if result["id"]["kind"] == "youtube#video": - video_id = result["id"]["videoId"] - link = self.VIDEO_URL_FMT.format(video_id) - elif result["id"]["kind"] == "youtube#channel": - channel_id = result["id"]["channelId"] - link = self.CHANNEL_URL_FMT.format(channel_id) - elif result["id"]["kind"] == "youtube#playlist": - playlist_id = result["id"]["playlistId"] - link = self.PLAYLIST_URL_FMT.format(playlist_id) - if link: - break - else: - self.signal_failure(target) - return - self.bot.say(target, link) + link = "" + icon = "" + title = "" + self.bot.log_i(str(search_response)) + 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}")