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): class YoutubeParserPlugin(Plugin):
VIDEO_URL_RE = re.compile( 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): def __init__(self, bot):
@ -32,21 +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:
self.tell_video_title(match.group(1), event.target) self.bot.log_d("testing " + word)
return True match = self.VIDEO_URL_RE.match(word)
if match:
self.bot.log_d("match!")
return self.handle_match(match, event.target)
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":
@ -54,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