Compare commits
No commits in common. "cde8d8883d39f580543a14e793af054c87d9a66e" and "93d59ff30b783d6a7db38a62bd5e392d565cda3d" have entirely different histories.
cde8d8883d
...
93d59ff30b
|
@ -12,8 +12,7 @@ from edmond.plugin import Plugin
|
|||
class YoutubeParserPlugin(Plugin):
|
||||
|
||||
VIDEO_URL_RE = re.compile(
|
||||
r"https?:\/\/(?:www\.youtube\.com\/watch\?(?:.*&)?v=(?P<code1>[^&\s]+)"
|
||||
r"|youtu\.be/(?P<code2>[^&\s]+))"
|
||||
r"https:\/\/www\.youtube\.com\/watch\?(?:.*&)?v=([^&]+)"
|
||||
)
|
||||
|
||||
def __init__(self, bot):
|
||||
|
@ -33,30 +32,21 @@ class YoutubeParserPlugin(Plugin):
|
|||
self.is_ready = False
|
||||
|
||||
def on_pubmsg(self, event):
|
||||
words = event.arguments[0].split()
|
||||
for word in words:
|
||||
self.bot.log_d("testing " + word)
|
||||
match = self.VIDEO_URL_RE.match(word)
|
||||
match = self.VIDEO_URL_RE.match(event.arguments[0])
|
||||
if match:
|
||||
self.bot.log_d("match!")
|
||||
return self.handle_match(match, event.target)
|
||||
self.tell_video_title(match.group(1), event.target)
|
||||
return True
|
||||
return False
|
||||
|
||||
def handle_match(self, match, target):
|
||||
groupdict = match.groupdict()
|
||||
code = groupdict.get("code1") or groupdict.get("code2")
|
||||
if not code:
|
||||
return False
|
||||
def tell_video_title(self, video_id, target):
|
||||
try:
|
||||
search_response = (
|
||||
self.youtube_plugin.youtube
|
||||
.videos()
|
||||
.list(id=code, part="snippet")
|
||||
.execute()
|
||||
)
|
||||
search_response = self.youtube_plugin.youtube.videos().list(
|
||||
id=video_id,
|
||||
part="snippet",
|
||||
).execute()
|
||||
except GoogleApiError:
|
||||
self.signal_failure(target)
|
||||
return False
|
||||
return
|
||||
title = ""
|
||||
for result in search_response.get("items", []):
|
||||
if result["kind"] == "youtube#video":
|
||||
|
@ -64,6 +54,5 @@ class YoutubeParserPlugin(Plugin):
|
|||
break
|
||||
else:
|
||||
self.signal_failure(target)
|
||||
return False
|
||||
return
|
||||
self.bot.say(target, title)
|
||||
return True
|
||||
|
|
Loading…
Reference in a new issue