Compare commits

..

No commits in common. "cde8d8883d39f580543a14e793af054c87d9a66e" and "93d59ff30b783d6a7db38a62bd5e392d565cda3d" have entirely different histories.

View file

@ -12,8 +12,7 @@ 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=(?P<code1>[^&\s]+)" r"https:\/\/www\.youtube\.com\/watch\?(?:.*&)?v=([^&]+)"
r"|youtu\.be/(?P<code2>[^&\s]+))"
) )
def __init__(self, bot): def __init__(self, bot):
@ -33,30 +32,21 @@ class YoutubeParserPlugin(Plugin):
self.is_ready = False self.is_ready = False
def on_pubmsg(self, event): def on_pubmsg(self, event):
words = event.arguments[0].split() match = self.VIDEO_URL_RE.match(event.arguments[0])
for word in words: if match:
self.bot.log_d("testing " + word) self.tell_video_title(match.group(1), event.target)
match = self.VIDEO_URL_RE.match(word) return True
if match:
self.bot.log_d("match!")
return self.handle_match(match, event.target)
return False return False
def handle_match(self, match, target): def tell_video_title(self, video_id, target):
groupdict = match.groupdict()
code = groupdict.get("code1") or groupdict.get("code2")
if not code:
return False
try: try:
search_response = ( search_response = self.youtube_plugin.youtube.videos().list(
self.youtube_plugin.youtube id=video_id,
.videos() part="snippet",
.list(id=code, part="snippet") ).execute()
.execute()
)
except GoogleApiError: except GoogleApiError:
self.signal_failure(target) self.signal_failure(target)
return False return
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":
@ -64,6 +54,5 @@ class YoutubeParserPlugin(Plugin):
break break
else: else:
self.signal_failure(target) self.signal_failure(target)
return False return
self.bot.say(target, title) self.bot.say(target, title)
return True