From 0a514e8cc0adc189478eb77ef84a0e186fb8a980 Mon Sep 17 00:00:00 2001 From: spline Date: Tue, 17 Jul 2012 07:31:23 -0400 Subject: [PATCH 1/3] Add in option to show link (for shortUrlsnarfer) and youtube support. --- __init__.py | 1 + config.py | 5 ++++- plugin.py | 42 ++++++++++++++++++++++++++++++++++++++---- 3 files changed, 43 insertions(+), 5 deletions(-) diff --git a/__init__.py b/__init__.py index 8f56cb5..46804d3 100644 --- a/__init__.py +++ b/__init__.py @@ -51,6 +51,7 @@ import config import plugin reload(plugin) # In case we're being reloaded. +reload(config) # Add more reloads here if you add third-party modules and want them to be # reloaded when this plugin is reloaded. Don't forget to import them as well! diff --git a/config.py b/config.py index 9b0c6af..2437c85 100644 --- a/config.py +++ b/config.py @@ -43,6 +43,9 @@ def configure(advanced): # This is where your configuration variables (if any) should go. For example: # conf.registerGlobalValue(Detroll, 'someConfigVariableName', # registry.Boolean(False, """Help for someConfigVariableName.""")) - +conf.registerGlobalValue(Detroll, 'showLinkPlainText', registry.Boolean\ + (False, """Show plaintext link infront of title.""")) +conf.registerGlobalValue(Detroll, 'parseYouTube', registry.Boolean\ + (True, """Parse and report specific YT info with links.""")) # vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79: diff --git a/plugin.py b/plugin.py index c9448b6..7cf901d 100644 --- a/plugin.py +++ b/plugin.py @@ -36,6 +36,9 @@ except ImportError: pass +import json +import requests + import supybot.utils as utils from supybot.commands import * import supybot.plugins as plugins @@ -82,7 +85,33 @@ def doPrivmsg(self, irc, msg): else: text = msg.args[1] for url in utils.web.urlRe.findall(text): - self.fetch_url(irc, channel, url) + + parseYouTube = self.registryValue('parseYouTube') + + if(url.find("youtube.com") != -1 or url.find("youtu.be") != -1) and parseYouTube: + youtube_pattern = re.compile('(?:www\.)?youtu(?:be\.com/watch\?v=|\.be/)([\w\?=\-]*)(&(amp;)?[\w\?=]*)?') + m = youtube_pattern.search(url); + if m: + self.log.info(m.group(1)) + self.ytlink(irc, channel, m.group(1)) + else: + self.fetch_url(irc, channel, url) + else: + self.fetch_url(irc, channel, url) + + def ytlink(self, irc, channel, url): + r = requests.get('http://gdata.youtube.com/feeds/api/videos/%s?v=2&alt=json' % url) + data = json.loads(r.content) + likes = float(data['entry']["yt$rating"]['numLikes']) + dislikes = float(data['entry']["yt$rating"]['numDislikes']) + rating = (likes/(likes+dislikes))*100 + shortlink = 'http://youtu.be/' + url + message = '%s - Title: %s, Views: %s, Rating: %s%%' % (shortlink, ircutils.bold(data['entry']['title']['$t']),\ + ircutils.bold(data['entry']['yt$statistics']['viewCount']), ircutils.bold(round(float(rating)))) + message = message.encode("utf-8", "replace") + irc.queueMsg(ircmsgs.privmsg(channel, message)) + return + def fetch_url(self, irc, channel, url): bold = ircutils.bold @@ -161,8 +190,13 @@ def parse(self, url, response, html): except AttributeError: title = 'Error reading title' - reply = '{0}Title: [{1}]'.format(statusstring, - bold(title.encode(ENCODING))) + showLink = self.registryValue('showLinkPlainText') + + if showLink: + reply = '{0} - {1}Title: [{2}]'.format(url, statusstring, bold(title.encode(ENCODING))) + else: + reply = '{0}Title: [{1}]'.format(statusstring, bold(title.encode(ENCODING))) + else: if size: extradata.append('Size: [{0}]'.format(bold(size))) @@ -206,4 +240,4 @@ def sizeof_fmt(self, num): Class = Detroll -# vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79: +# vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=250: From c4b33545363099395f921e819dd079d1996b0471 Mon Sep 17 00:00:00 2001 From: spline Date: Tue, 17 Jul 2012 08:55:22 -0400 Subject: [PATCH 2/3] Add rough code for handling images and finding dimensions of. --- plugin.py | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/plugin.py b/plugin.py index 7cf901d..1983d0b 100644 --- a/plugin.py +++ b/plugin.py @@ -38,6 +38,8 @@ import json import requests +import PIL.Image as Image +import StringIO import supybot.utils as utils from supybot.commands import * @@ -105,7 +107,7 @@ def ytlink(self, irc, channel, url): likes = float(data['entry']["yt$rating"]['numLikes']) dislikes = float(data['entry']["yt$rating"]['numDislikes']) rating = (likes/(likes+dislikes))*100 - shortlink = 'http://youtu.be/' + url + shortlink = ircutils.mircColor('http://youtu.be/' + url,'blue') message = '%s - Title: %s, Views: %s, Rating: %s%%' % (shortlink, ircutils.bold(data['entry']['title']['$t']),\ ircutils.bold(data['entry']['yt$statistics']['viewCount']), ircutils.bold(round(float(rating)))) message = message.encode("utf-8", "replace") @@ -196,16 +198,28 @@ def parse(self, url, response, html): reply = '{0} - {1}Title: [{2}]'.format(url, statusstring, bold(title.encode(ENCODING))) else: reply = '{0}Title: [{1}]'.format(statusstring, bold(title.encode(ENCODING))) - + else: + if 'image/' in contenttype: + try: + r = requests.get(url) + image = Image.open(StringIO.StringIO(r.content)) + width, height = image.size + extradata.append(' Dimensions: [{0}x{1}] '.format(bold(width), bold(height))) + except: + continue if size: extradata.append('Size: [{0}]'.format(bold(size))) if extradata: extradatastring = ' '.join(extradata) - reply = '{0}Content type: [{1}] {2}'.format(statusstring, - bold(contenttype), - extradatastring) + + showLink = self.registryValue('showLinkPlainText') + + if showLink: + reply = '{0} - {1}Content type: [{2}] {3}'.format(url, statusstring, bold(contenttype), extradatastring) + else: + reply = '{0}Content type: [{1}] {2}'.format(statusstring, bold(contenttype), extradatastring) return reply From 5686f5362b8b435c0b9a7550953ef5c644f63968 Mon Sep 17 00:00:00 2001 From: spline Date: Tue, 17 Jul 2012 15:39:00 -0400 Subject: [PATCH 3/3] Fixed broken try block. --- plugin.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/plugin.py b/plugin.py index 1983d0b..c8f7901 100644 --- a/plugin.py +++ b/plugin.py @@ -201,13 +201,11 @@ def parse(self, url, response, html): else: if 'image/' in contenttype: - try: - r = requests.get(url) - image = Image.open(StringIO.StringIO(r.content)) - width, height = image.size - extradata.append(' Dimensions: [{0}x{1}] '.format(bold(width), bold(height))) - except: - continue + r = requests.get(url) + image = Image.open(StringIO.StringIO(r.content)) + width, height = image.size + extradata.append(' Dimensions: [{0}x{1}] '.format(bold(width), bold(height))) + if size: extradata.append('Size: [{0}]'.format(bold(size)))