-
Notifications
You must be signed in to change notification settings - Fork 0
/
mburban.py
39 lines (32 loc) · 1.14 KB
/
mburban.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
# coding: utf-8
from mbclient import mb
from random import choice
import json,urllib,urllib2,traceback
urban = "urban(?:(?P<r>\s+random)|(?P<s>\s+sound))?\s+(?P<what>.+)"
def urban_func(nick,match,target):
what = urllib.urlencode({"term":match.group("what")})
s=match.group('s')
r=match.group('r')
req = urllib2.Request('https://mashape-community-urban-dictionary.p.mashape.com/define?'+what)
req.add_header("X-Mashape-Key","6SRh5ZIyhhmshOjLLIEVlfRzZR3Mp1KJgLsjsny2Vq36opmhI6")
req.add_header("Accept", "text/plain")
try:
response = json.loads(urllib2.urlopen(req).read().decode('utf-8'))
if response['result_type'] == "no_results":
mb.tell(nick+": none of that", target)
return
if s:
mb.tell(choice(response['sounds']),target)
else:
if r:
result = choice(response['list'])
else:
result = response['list'][0]
mb.tell(" ".join(result['definition'].splitlines())[:200],target)
mb.tell("example: "+" ".join(result['example'].splitlines())[:200],target)
except:
mb.tell(nick+": whoops.",target)
traceback.print_exc()
print("loaded urban")
mb.add_command(urban,urban_func)
mb.help['urban']="mb urban <something>"