Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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!

Expand Down
5 changes: 4 additions & 1 deletion config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
60 changes: 53 additions & 7 deletions plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@
except ImportError:
pass

import json
import requests
import PIL.Image as Image
import StringIO

import supybot.utils as utils
from supybot.commands import *
import supybot.plugins as plugins
Expand Down Expand Up @@ -82,7 +87,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 = 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")
irc.queueMsg(ircmsgs.privmsg(channel, message))
return


def fetch_url(self, irc, channel, url):
bold = ircutils.bold
Expand Down Expand Up @@ -161,17 +192,32 @@ 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 'image/' in contenttype:
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)))

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

Expand Down Expand Up @@ -206,4 +252,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: