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