From 9fa8ff7581095af24d8d4723b3a1a2b25403bdd2 Mon Sep 17 00:00:00 2001 From: PrgmrBill Date: Wed, 24 Jun 2015 18:49:10 -0400 Subject: [PATCH] SpiffyTitles: fixes #57 - Adds vimeo handler --- README.md | 8 +++++ config.py | 7 +++++ plugin.py | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++----- 3 files changed, 95 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 12ada4a..5587d6b 100644 --- a/README.md +++ b/README.md @@ -135,6 +135,14 @@ Default value: `^ {%if not_safe_for_work %}NSFW{% endif %} [{{channel.title}}] { `coubHandlerEnabled` - Whether to enable additional information about coub videos. +### vimeo handler + +`vimeoTitleTemplate` - Template for [vimeo](https://vimeo.com) links. + +Default value: `^ {{title}} :: Duration: {{duration}} :: {{stats_number_of_plays}} plays :: {{stats_number_of_comments}} comments` + +`vimeoHandlerEnabled` - Whether to enable additional information about vimeo videos. + ## Other options `useBold` - Whether to bold the title. Default value: `False` diff --git a/config.py b/config.py index bd1237e..3cb84d6 100644 --- a/config.py +++ b/config.py @@ -76,6 +76,13 @@ conf.registerGlobalValue(SpiffyTitles, 'defaultTitleTemplate', conf.registerGlobalValue(SpiffyTitles, 'youtubeTitleTemplate', registry.String("^ {{yt_logo}} :: {{title}} :: Duration: {{duration}} :: Views: {{view_count}} uploaded by {{channel_title}} :: {{like_count}} likes :: {{dislike_count}} dislikes :: {{favorite_count}} favorites", _("""Template used for YouTube title responses"""))) +# Vimeo template +conf.registerGlobalValue(SpiffyTitles, 'vimeoTitleTemplate', + registry.String("^ {{title}} :: Duration: {{duration}} :: {{stats_number_of_plays}} plays :: {{stats_number_of_comments}} comments", _("""Template used for Vimeo title responses"""))) + +conf.registerGlobalValue(SpiffyTitles, 'vimeoHandlerEnabled', + registry.Boolean(True, _("""Enable additional information about Vimeo videos"""))) + # User agents conf.registerGlobalValue(SpiffyTitles, 'userAgents', registry.CommaSeparatedListOfStrings(["Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.60 Safari/537.36", "Mozilla/5.0 (Windows NT 6.3; rv:36.0) Gecko/20100101 Firefox/36.0", "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; AS; rv:11.0) like Gecko"], _("""Reported user agent when fetching links"""))) diff --git a/plugin.py b/plugin.py index db0aa63..17cc3b9 100644 --- a/plugin.py +++ b/plugin.py @@ -63,10 +63,77 @@ class SpiffyTitles(callbacks.Plugin): self.add_imdb_handlers() self.add_imgur_handlers() self.add_coub_handlers() + self.add_vimeo_handlers() + + def add_vimeo_handlers(self): + self.handlers["vimeo.com"] = self.handler_vimeo def add_coub_handlers(self): self.handlers["coub.com"] = self.handler_coub + def handler_vimeo(self, url, domain): + """ + Handles Vimeo links + """ + vimeo_handler_enabled = self.registryValue("vimeoHandlerEnabled") + self.log.info("SpiffyTitles: calling vimeo handler for %s" % url) + title = None + video_id = None + + """ Get video ID """ + if vimeo_handler_enabled: + result = re.search(r'^(http(s)://)?(www\.)?(vimeo\.com/)?(\d+)', url) + + if result is not None: + video_id = result.group(5) + + if video_id is not None: + api_url = "https://vimeo.com/api/v2/video/%s.json" % video_id + self.log.info("SpiffyTitles: looking up vimeo info: %s", api_url) + agent = self.get_user_agent() + headers = { + "User-Agent": agent + } + + request = requests.get(api_url, headers=headers) + + ok = request.status_code == requests.codes.ok + + if ok: + response = json.loads(request.text) + + if response is not None and "title" in response[0]: + video = response[0] + vimeo_template = Template(self.registryValue("vimeoTitleTemplate")) + + """ + Some videos do not have this information available + """ + if "stats_number_of_plays" in video: + video["stats_number_of_plays"] = "{:,}".format(int(video["stats_number_of_plays"])) + else: + video["stats_number_of_plays"] = 0 + + if "stats_number_of_comments" in video: + video["stats_number_of_comments"] = "{:,}".format(int(video["stats_number_of_comments"])) + else: + video["stats_number_of_comments"] = 0 + + video["duration"] = self.get_duration_from_seconds(video["duration"]) + + title = vimeo_template.render(video) + else: + self.log.info("SpiffyTitles: received unexpected payload from video: %s" % api_url) + else: + self.log.error("SpiffyTitles: vimeo handler returned %s: %s" % (request.status_code, request.text[:200])) + + if title is None: + self.log.info("SpiffyTitles: could not get vimeo info for %s" % url) + + return self.handler_default(url) + else: + return title + def handler_coub(self, url, domain): """ Handles coub.com links @@ -475,14 +542,7 @@ class SpiffyTitles(callbacks.Plugin): #23 - If duration is zero, then it"s a LIVE video """ if duration_seconds > 0: - m, s = divmod(duration_seconds, 60) - h, m = divmod(m, 60) - - duration = "%02d:%02d" % (m, s) - - # Only include hour if the video is at least 1 hour long - if h > 0: - duration = "%02d:%s" % (h, duration) + duration = self.get_duration_from_seconds(duration_seconds) else: duration = "LIVE" @@ -518,6 +578,18 @@ class SpiffyTitles(callbacks.Plugin): return self.handler_default(url) + def get_duration_from_seconds(self, duration_seconds): + m, s = divmod(duration_seconds, 60) + h, m = divmod(m, 60) + + duration = "%02d:%02d" % (m, s) + + """ Only include hour if the video is at least 1 hour long """ + if h > 0: + duration = "%02d:%s" % (h, duration) + + return duration + def get_youtube_logo(self): colored_letters = [ "%s" % ircutils.mircColor("You", fg="red", bg="white"),