Skip to content

Commit

Permalink
adding a "better" plugin :)
Browse files Browse the repository at this point in the history
  • Loading branch information
Robin Walsh committed Mar 9, 2011
1 parent a55b226 commit 935311e
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions twisted/plugins/better.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from zope.interface import implements, classProvides
from twisted.plugin import IPlugin

from modules.interfaces import *
import json, urllib2

class Better(object):
implements(IPlugin, ICommandWatcher)

def __init__(self):
self.commands = { 'better': self.getBetter }

def provides(self):
return self.commands.keys()

def providesCommand(self, cmd):
if cmd in self.commands:
return True

def gotCmd(self, channel, user, cmd, args, irc=None):
if cmd not in self.commands:
return
c = self.commands[cmd]
return c(channel, user, args, irc=None)

def getBetter(self, channel, user, args, irc=None):
scores = []
url = 'http://sucks-rocks.com/query?term='
original_string = ' '.join(args)
terms = original_string.split(' or ')
if len(terms) <= 1:
return None
for term in terms:
u = url + term
j = json.loads(urllib2.urlopen(u).read())
sucks, rocks = j['sucks'], j['rocks']
total = float(sucks + rocks)
score = ( rocks / total ) * 10
scores.append("{0:25}: {1:.1f}".format(term, score))
return scores


def help(self, cmd):
if cmd == 'better':
h = """Usage: ?better <term> or <term> ..."""
return h

b = Better()

0 comments on commit 935311e

Please sign in to comment.