-
Notifications
You must be signed in to change notification settings - Fork 0
/
urband.py
57 lines (38 loc) · 1.2 KB
/
urband.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
import sys # Just for getting command line args.
import requests # For requesting the appropriate API stuff.
# -- Global Vars --
failedReq = 'Could not get response from Urban Dictionary API.'
noDefs = 'No definitions could be found.'
reqLink = 'http://api.urbandictionary.com/v0/define?term='
def quit(msg):
print('[!] ' + msg)
sys.exit(1)
def getWord():
word = ''
for words in sys.argv[1:]:
word += words + ' '
word.strip()
return word
def parseResp():
resp = requests.get(reqLink + word)
sizeOfDefs = len(resp.json()['list'])
if (sizeOfDefs < 1):
quit(noDefs)
numOfDefs = min(sizeOfDefs, 3)
keys = ['author', 'definition', 'example']
for defs in range(0, numOfDefs):
for key in keys:
val = resp.json()['list'][defs][key]
if (key == 'author'):
print('Author --> ' + val)
elif (key == 'definition'):
print('Def --> ' + val)
elif (key == 'example'):
print('Example --> ' + val)
print('\n')
if __name__ == "__main__":
word = getWord()
try:
parseResp()
except:
quit(failedReq)