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()
for word in words:
self.bot.log_d("testing " + word)
match = self.VIDEO_URL_RE.match(word)
if match: if match:
self.bot.log_d("match!")
return self.handle_match(match, event.target)
return False
def handle_match(self, match, target):
groupdict = match.groupdict() groupdict = match.groupdict()
code = groupdict.get("code1") or groupdict.get("code2") code = groupdict.get("code1") or groupdict.get("code2")
if not code: if not code:
return False return False
self.tell_video_title(code, event.target)
return True
return False
def tell_video_title(self, video_id, target):
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