-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Robin Walsh
committed
Mar 9, 2011
1 parent
a55b226
commit 935311e
Showing
1 changed file
with
48 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |