-
Notifications
You must be signed in to change notification settings - Fork 5
/
urban.py
65 lines (50 loc) · 1.42 KB
/
urban.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import json
import re
import urllib
from sopel import module
def format_defn(defn, num, max):
lines = []
pieces = re.split(r'\n+', defn['definition'])
lines.append('%s: definition %i of %i' % (defn['word'], num, max))
lines.extend(pieces)
lines.append('Example: %s' % defn['example'].split('\n')[0])
return lines
def urban(bot, input):
baseurl = 'http://api.urbandictionary.com/v0/'
if not input.group(2):
bot.say('Usage: .urban <word> [definition number]')
return
input = input.group(2).strip()
words = input.split()
try:
num = int(words[-1])
input = ' '.join(words[:-1])
except ValueError:
num = 1
if input == 'random':
url = baseurl + 'random'
else:
url = baseurl + 'define?term=' + input
try:
result = json.loads(urllib.urlopen(url).read())
except:
return
if not result or "list" not in result:
return
idx = num - 1
max = len(result['list'])
try:
defn = result['list'][idx]
except IndexError:
bot.say('No definition %i of %i for %s' % (idx, max, input))
return
lines = format_defn(defn, num, max)
for line in lines:
bot.say(line)
urban.commands = ['urban']
if __name__ == '__main__':
import sys
class Sopel(object):
def say(self, msg):
print msg
urban(Sopel(), re.match('()(.*)', sys.argv[1]))