Compare commits

...

2 commits

Author SHA1 Message Date
dece cde8d8883d youtube_parser: look for youtube URL in whole msg 2021-06-07 10:52:04 +02:00
dece 7ca8fa2ce4 youtube_parser: fix regex 2021-06-07 10:45:54 +02:00

View file

@ -12,7 +12,8 @@ from edmond.plugin import Plugin
class YoutubeParserPlugin(Plugin):
VIDEO_URL_RE = re.compile(
r"https:\/\/www\.youtube\.com\/watch\?(?:.*&)?v=([^&]+)"
r"https?:\/\/(?:www\.youtube\.com\/watch\?(?:.*&)?v=(?P<code1>[^&\s]+)"
r"|youtu\.be/(?P<code2>[^&\s]+))"
)
def __init__(self, bot):
@ -32,21 +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:
self.tell_video_title(match.group(1), 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":
@ -54,5 +64,6 @@ class YoutubeParserPlugin(Plugin):
break
else:
self.signal_failure(target)
return
return False
self.bot.say(target, title)
return True