forked from mobeets/sheriff-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathurbandictionary_api.py
73 lines (65 loc) · 2.37 KB
/
urbandictionary_api.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
66
67
68
69
70
71
72
73
import sys
import json
if sys.version < '3':
from urllib2 import urlopen
from urllib import quote as urlquote
else:
from urllib.request import urlopen
from urllib.parse import quote as urlquote
UD_DEFID_URL = 'https://api.urbandictionary.com/v0/define?defid='
UD_DEFINE_URL = 'https://api.urbandictionary.com/v0/define?term='
UD_RANDOM_URL = 'https://api.urbandictionary.com/v0/random'
class UrbanDefinition(object):
def __init__(self, word, definition, example, upvotes, downvotes):
self.word = word
self.definition = definition
self.example = example
self.upvotes = upvotes
self.downvotes = downvotes
def __str__(self):
return '%s: %s%s (%d, %d)' % (
self.word,
self.definition[:50],
'...' if len(self.definition) > 50 else '',
self.upvotes,
self.downvotes
)
def _get_urban_json(url):
f = urlopen(url)
data = json.loads(f.read().decode('utf-8'))
f.close()
return data
def _parse_urban_json(json, check_result=True):
result = []
if json is None or any(e in json for e in ('error', 'errors')):
raise ValueException('UD: Invalid input for Urban Dictionary API')
if check_result and ('list' not in json or len(json['list']) == 0):
return result
for definition in json['list']:
d = UrbanDefinition(
definition['word'],
definition['definition'],
definition['example'],
int(definition['thumbs_up']),
int(definition['thumbs_down'])
)
result.append(d)
return result
def define(term):
"""Search for term/phrase and return list of UrbanDefinition objects.
Keyword arguments:
term -- term or phrase to search for (str)
"""
json = _get_urban_json(UD_DEFINE_URL + urlquote(term))
return _parse_urban_json(json)
def defineID(defid):
"""Search for UD's definition ID and return list of UrbanDefinition objects.
Keyword arguments:
defid -- definition ID to search for (int or str)
"""
json = _get_urban_json(UD_DEFID_URL + urlquote(str(defid)))
return _parse_urban_json(json)
def random():
"""Return random definitions as a list of UrbanDefinition objects."""
json = _get_urban_json(UD_RANDOM_URL)
return _parse_urban_json(json, check_result=False)