youtube_parser: look for youtube URL in whole msg

This commit is contained in:
dece 2021-06-07 10:52:04 +02:00
parent 7ca8fa2ce4
commit cde8d8883d

View file

@ -33,25 +33,30 @@ class YoutubeParserPlugin(Plugin):
self.is_ready = False self.is_ready = False
def on_pubmsg(self, event): def on_pubmsg(self, event):
match = self.VIDEO_URL_RE.match(event.arguments[0]) words = event.arguments[0].split()
if match: for word in words:
groupdict = match.groupdict() self.bot.log_d("testing " + word)
code = groupdict.get("code1") or groupdict.get("code2") match = self.VIDEO_URL_RE.match(word)
if not code: if match:
return False self.bot.log_d("match!")
self.tell_video_title(code, event.target) return self.handle_match(match, event.target)
return True
return False return False
def tell_video_title(self, video_id, target): def handle_match(self, match, target):
groupdict = match.groupdict()
code = groupdict.get("code1") or groupdict.get("code2")
if not code:
return False
try: try:
search_response = self.youtube_plugin.youtube.videos().list( search_response = (
id=video_id, self.youtube_plugin.youtube
part="snippet", .videos()
).execute() .list(id=code, part="snippet")
.execute()
)
except GoogleApiError: except GoogleApiError:
self.signal_failure(target) self.signal_failure(target)
return return False
title = "" title = ""
for result in search_response.get("items", []): for result in search_response.get("items", []):
if result["kind"] == "youtube#video": if result["kind"] == "youtube#video":
@ -59,5 +64,6 @@ class YoutubeParserPlugin(Plugin):
break break
else: else:
self.signal_failure(target) self.signal_failure(target)
return return False
self.bot.say(target, title) self.bot.say(target, title)
return True