diff --git a/__init__.py b/__init__.py index 785e581..161b14c 100644 --- a/__init__.py +++ b/__init__.py @@ -1,5 +1,5 @@ ### -# Copyright (c) 2012, spline +# Copyright (c) 2012-2013, spline # All rights reserved. # # diff --git a/config.py b/config.py index 65946ef..0245e7f 100644 --- a/config.py +++ b/config.py @@ -1,5 +1,5 @@ ### -# Copyright (c) 2012, spline +# Copyright (c) 2012-2013, spline # All rights reserved. # # diff --git a/plugin.py b/plugin.py index 0de7a29..1c5a4ad 100644 --- a/plugin.py +++ b/plugin.py @@ -1,18 +1,16 @@ # -*- coding: utf-8 -*- ### -# Copyright (c) 2012, spline +# Copyright (c) 2012-2013, spline # All rights reserved. # # ### - # my libs import json import urllib2 import urllib import string import unicodedata - # supybot libs import supybot.utils as utils from supybot.commands import * @@ -28,7 +26,7 @@ class UrbanDictionary(callbacks.Plugin): """Add the help for "@plugin help UrbanDictionary" here This should describe *how* to use this plugin.""" threaded = True - + # helper methods. def _remove_accents(self, data): nkfd_form = unicodedata.normalize('NFKD', unicode(data)) @@ -45,17 +43,17 @@ class UrbanDictionary(callbacks.Plugin): # main urbandict function. def urbandictionary(self, irc, msg, args, optlist, optterm): """[--disableexamples|--showvotes|--num #] - + Fetches definition for on UrbanDictionary.com - + Use --disableexamples to not display examples. Use --showvotes to show votes [default: off] Use --num # to limit the number of definitions. [default:10] """ - + # default args for output. args = {'showExamples': True, 'numberOfDefinitions':'10', 'showVotes': False } - + # optlist to change args. if optlist: for (key, value) in optlist: @@ -63,13 +61,13 @@ class UrbanDictionary(callbacks.Plugin): args['showExamples'] = False if key == 'showvotes': args['showVotes'] = True - if key == 'num': # number of definitions. max 10 default but also is enforced + if key == 'num': # number of definitions. max 10 default but also is enforced if value > self.registryValue('maxNumberOfDefinitions') or value <= 0: args['numberOfDefinitions'] = '10' else: args['numberOfDefinitions'] = value - - # url + + # url url = 'http://api.urbandictionary.com/v0/define?term=%s' % (urllib.quote(optterm)) # try fetching url. @@ -81,25 +79,25 @@ class UrbanDictionary(callbacks.Plugin): self.log.debug("Error fetching: {0} :: {1}".format(url, e)) return - # try parsing json. + # try parsing json. try: jsondata = response.read().decode('utf-8') jsondata = json.loads(jsondata.replace(r'\r','').replace(r'\n','')) # odds chars in UD. except: irc.reply("Failed to parse json data. Check logs for error") return - + # handle output based on what comes back. 2 different results, fallback on else. # asshole - "has_related_words": true, "result_type": "exact" # assmole - "has_related_words": true, "result_type": "exact", total: 1 # asswole - "has_related_words": false, "result_type": "no_results" - list->term - definitions = jsondata.get('list', None) + definitions = jsondata.get('list', None) result_type = jsondata.get('result_type', None) total = jsondata.get('total', None) - + if result_type == "exact" and len(jsondata['list']) > 0: # exact, means we found, and we have definitions. output = [] # container to put all def/ex in. - for item in jsondata['list'][0:int(args['numberOfDefinitions'])]: + for item in jsondata['list'][0:int(args['numberOfDefinitions'])]: outputstring = "{0}".format(item['definition'].encode('utf-8').strip()) # start outputstring. if args['showExamples']: # if we're showing examples if self.registryValue('disableANSI'): @@ -108,9 +106,9 @@ class UrbanDictionary(callbacks.Plugin): outputstring += " {0} {1} {2}".format(self._bu("[ex:]"), item['example'].encode('utf-8').strip(), self._bu("[/ex]")) if args['showVotes']: # if we're showing votes outputstring += " (+{0}/-{1})".format(item['thumbs_up'], item['thumbs_down']) - + output.append(outputstring) # finally add to output - + #output. if self.registryValue('disableANSI'): irc.reply("{0} ({1}): {2}".format(optterm, total, string.join([item for item in output], " | "))) @@ -119,7 +117,7 @@ class UrbanDictionary(callbacks.Plugin): elif result_type == "no_results" and len(jsondata['list']) > 0: outrelated = string.join([item['term'].encode('utf-8') for item in jsondata['list']], " | ") - + if self.registryValue('disableANSI'): irc.reply("{0}: {1} not found. {2}: {3}".format("ERROR", optterm, "Related terms", outrelated)) else: diff --git a/test.py b/test.py index 48ad0e9..2406a13 100644 --- a/test.py +++ b/test.py @@ -1,5 +1,5 @@ ### -# Copyright (c) 2012, spline +# Copyright (c) 2012-2013, spline # All rights reserved. # #