From cac50fb23a324af03880ad424377978de3961f09 Mon Sep 17 00:00:00 2001 From: dpedu Date: Mon, 31 Aug 2015 21:22:51 -0700 Subject: [PATCH] Update link titler module for new youtube api --- examples/data/config/LinkTitler.json | 4 ++ examples/data/config/LinkTitler.yml | 1 - examples/data/config/Youtube.json | 3 ++ pyircbot/modules/LinkTitler.py | 62 ++++++++++++++--------- pyircbot/modules/Youtube.py | 74 +++++++++++++++------------- pyircbot/pyircbot.py | 2 + 6 files changed, 89 insertions(+), 57 deletions(-) create mode 100644 examples/data/config/LinkTitler.json delete mode 100644 examples/data/config/LinkTitler.yml create mode 100644 examples/data/config/Youtube.json diff --git a/examples/data/config/LinkTitler.json b/examples/data/config/LinkTitler.json new file mode 100644 index 0000000..fdddd9a --- /dev/null +++ b/examples/data/config/LinkTitler.json @@ -0,0 +1,4 @@ +{ + "agent": "pyircbot3 by /u/(changeme)", + "youtube_api_key": "" +} diff --git a/examples/data/config/LinkTitler.yml b/examples/data/config/LinkTitler.yml deleted file mode 100644 index 74a9465..0000000 --- a/examples/data/config/LinkTitler.yml +++ /dev/null @@ -1 +0,0 @@ -agent: pyircbot3 by /u/(changeme) diff --git a/examples/data/config/Youtube.json b/examples/data/config/Youtube.json new file mode 100644 index 0000000..230fbfe --- /dev/null +++ b/examples/data/config/Youtube.json @@ -0,0 +1,3 @@ +{ + "api_key": "" +} \ No newline at end of file diff --git a/pyircbot/modules/LinkTitler.py b/pyircbot/modules/LinkTitler.py index d591246..5abe807 100755 --- a/pyircbot/modules/LinkTitler.py +++ b/pyircbot/modules/LinkTitler.py @@ -90,33 +90,49 @@ class LinkTitler(ModuleBase): self.bot.act_PRIVMSG(args[0], "%s: \x02%s\x02" % (sender.nick, title)) return - # For youtbue + # For youtube + def getISOdurationseconds(self, stamp): + ISO_8601_period_rx = re.compile( + 'P' # designates a period + '(?:(?P\d+)Y)?' # years + '(?:(?P\d+)M)?' # months + '(?:(?P\d+)W)?' # weeks + '(?:(?P\d+)D)?' # days + '(?:T' # time part must begin with a T + '(?:(?P\d+)H)?' # hours + '(?:(?P\d+)M)?' # minutes + '(?:(?P\d+)S)?' # seconds + ')?' # end of time part + ) # http://stackoverflow.com/a/16742742 + return ISO_8601_period_rx.match(stamp).groupdict() def get_video_description(self, vid_id): - j = get("http://gdata.youtube.com/feeds/api/videos/%s?v=2&alt=jsonc" % vid_id).json() - if j.get('error'): + apidata = get('https://www.googleapis.com/youtube/v3/videos?part=snippet,contentDetails,statistics&id=%s&key=%s' % (vid_id, self.config["youtube_api_key"])).json() + + if not apidata['pageInfo']['totalResults']: return - j = j['data'] - out = '\x02\x031,0You\x0f\x030,4Tube\x02\x0f :: \x02%s\x02' % j['title'] - if not j.get('duration'): - return out + + video = apidata['items'][0] + snippet = video["snippet"] + duration = self.getISOdurationseconds(video["contentDetails"]["duration"]) + + out = '\x02\x031,0You\x0f\x030,4Tube\x02\x0f :: \x02%s\x02' % snippet["title"] + out += ' - length \x02' - length = j['duration'] - if length / 3600: # > 1 hour - out += '%dh ' % (length / 3600) - if length / 60: - out += '%dm ' % (length / 60 % 60) - out += "%ds\x02" % (length % 60) - if 'rating' in j: - out += ' - rated \x02%.2f/5.0\x02 (%d)' % (j['rating'], - j['ratingCount']) - if 'viewCount' in j: - out += ' - \x02%s\x02 views' % self.group_int_digits(j['viewCount']) - upload_time = time.strptime(j['uploaded'], "%Y-%m-%dT%H:%M:%S.000Z") - out += ' - \x02%s\x02 on \x02%s\x02' % ( - j['uploader'], time.strftime("%Y.%m.%d", upload_time)) - if 'contentRating' in j: - out += ' - \x034NSFW\x02' + if duration["hours"]!=None: + out += '%dh ' % int(duration["hours"]) + if duration["minutes"]!=None: + out += '%dm ' % int(duration["minutes"]) + out += "%ds\x02" % int(duration["seconds"]) + + totalvotes = float(video["statistics"]["dislikeCount"])+float(video["statistics"]["likeCount"]) + rating = float(video["statistics"]["likeCount"]) / totalvotes + out += ' - rated \x02%.2f/5\x02' % round(rating*5,1) + out += ' - \x02%s\x02 views' % self.group_int_digits(video["statistics"]["viewCount"]) + upload_time = time.strptime(snippet['publishedAt'], "%Y-%m-%dT%H:%M:%S.000Z") + out += ' - by \x02%s\x02 on \x02%s\x02' % (snippet['channelTitle'], time.strftime("%Y.%m.%d", upload_time)) + return out + def group_int_digits(self, number, delimiter=',', grouping=3): base = str(number).strip() builder = [] diff --git a/pyircbot/modules/Youtube.py b/pyircbot/modules/Youtube.py index 60aaa72..7f88797 100755 --- a/pyircbot/modules/Youtube.py +++ b/pyircbot/modules/Youtube.py @@ -10,12 +10,28 @@ from pyircbot.modulebase import ModuleBase,ModuleHook from requests import get import time +import re class Youtube(ModuleBase): def __init__(self, bot, moduleName): ModuleBase.__init__(self, bot, moduleName); self.hooks=[ModuleHook("PRIVMSG", self.youtube)] + def getISOdurationseconds(self, stamp): + ISO_8601_period_rx = re.compile( + 'P' # designates a period + '(?:(?P\d+)Y)?' # years + '(?:(?P\d+)M)?' # months + '(?:(?P\d+)W)?' # weeks + '(?:(?P\d+)D)?' # days + '(?:T' # time part must begin with a T + '(?:(?P\d+)H)?' # hours + '(?:(?P\d+)M)?' # minutes + '(?:(?P\d+)S)?' # seconds + ')?' # end of time part + ) # http://stackoverflow.com/a/16742742 + return ISO_8601_period_rx.match(stamp).groupdict() + def youtube(self, args, prefix, trailing): cmd = self.bot.messageHasCommand(".youtube", trailing) @@ -34,41 +50,33 @@ class Youtube(ModuleBase): self.bot.act_PRIVMSG(args[0], "http://youtu.be/%s :: %s" % (vid_id, self.get_video_description(vid_id))) def get_video_description(self, vid_id): - j = get("http://gdata.youtube.com/feeds/api/videos/%s?v=2&alt=jsonc" % vid_id).json() - - if j.get('error'): + apidata = get('https://www.googleapis.com/youtube/v3/videos?part=snippet,contentDetails,statistics&id=%s&key=%s' % (vid_id, self.config["api_key"])).json() + + if not apidata['pageInfo']['totalResults']: return - - j = j['data'] - - out = '\x02\x031,0You\x0f\x030,4Tube\x02\x0f :: \x02%s\x02' % j['title'] - - if not j.get('duration'): - return out - + + video = apidata['items'][0] + snippet = video["snippet"] + duration = self.getISOdurationseconds(video["contentDetails"]["duration"]) + + out = '\x02\x031,0You\x0f\x030,4Tube\x02\x0f :: \x02%s\x02' % snippet["title"] + out += ' - length \x02' - length = j['duration'] - if length / 3600: # > 1 hour - out += '%dh ' % (length / 3600) - if length / 60: - out += '%dm ' % (length / 60 % 60) - out += "%ds\x02" % (length % 60) - - if 'rating' in j: - out += ' - rated \x02%.2f/5.0\x02 (%d)' % (j['rating'], - j['ratingCount']) - - if 'viewCount' in j: - out += ' - \x02%s\x02 views' % self.group_int_digits(j['viewCount']) - - upload_time = time.strptime(j['uploaded'], "%Y-%m-%dT%H:%M:%S.000Z") - out += ' - \x02%s\x02 on \x02%s\x02' % ( - j['uploader'], time.strftime("%Y.%m.%d", upload_time)) - - if 'contentRating' in j: - out += ' - \x034NSFW\x02' - + if duration["hours"]!=None: + out += '%dh ' % int(duration["hours"]) + if duration["minutes"]!=None: + out += '%dm ' % int(duration["minutes"]) + out += "%ds\x02" % int(duration["seconds"]) + + totalvotes = float(video["statistics"]["dislikeCount"])+float(video["statistics"]["likeCount"]) + rating = float(video["statistics"]["likeCount"]) / totalvotes + out += ' - rated \x02%.2f/5\x02' % round(rating*5,1) + out += ' - \x02%s\x02 views' % self.group_int_digits(video["statistics"]["viewCount"]) + upload_time = time.strptime(snippet['publishedAt'], "%Y-%m-%dT%H:%M:%S.000Z") + out += ' - by \x02%s\x02 on \x02%s\x02' % (snippet['channelTitle'], time.strftime("%Y.%m.%d", upload_time)) + return out + def group_int_digits(self, number, delimiter=',', grouping=3): base = str(number).strip() builder = [] @@ -76,4 +84,4 @@ class Youtube(ModuleBase): builder.append(base[-grouping:]) base = base[:-grouping] builder.reverse() - return delimiter.join(builder) \ No newline at end of file + return delimiter.join(builder) diff --git a/pyircbot/pyircbot.py b/pyircbot/pyircbot.py index b926f96..0eb033a 100644 --- a/pyircbot/pyircbot.py +++ b/pyircbot/pyircbot.py @@ -104,6 +104,7 @@ class PyIRCBot: :type moduleName: str""" " check if already exists " if not name in self.modules: + self.log.debug("Importing %s" % name) " attempt to load " try: moduleref = __import__(name) @@ -123,6 +124,7 @@ class PyIRCBot: :param moduleName: Name of the module to import :type moduleName: str""" + self.log.debug("Deporting %s" % name) " unload if necessary " if name in self.moduleInstances: self.unloadmodule(name)