Skip to content

Instantly share code, notes, and snippets.

@mazeto
Created March 24, 2018 19:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mazeto/c21408596ba8c84faff6772eff56429c to your computer and use it in GitHub Desktop.
Save mazeto/c21408596ba8c84faff6772eff56429c to your computer and use it in GitHub Desktop.
Terminal URban Dictionary
#!/bin/python
import sys
import json
import requests
# concatenate args
args = " "
for arg in sys.argv:
# skip the 1st arg, the program's path+name
if arg == sys.argv[0]:
continue
args = args + arg
r = requests.get("https://api.urbandictionary.com/v0/define?term=" + args)
d = json.loads(r.text)
if d['tags']:
print("tags: ")
for tag in d['tags']:
print("\x1b[33m" + tag + "\x1b[0m ", sep=" ", end="")
print("\n")
# print the list backwards
i = len(d['list'])
K = "\x1b[1;30m"
R = "\x1b[1;31m"
G = "\x1b[1;32m"
Y = "\x1b[1;33m"
B = "\x1b[1;34m"
M = "\x1b[1;35m"
W = "\x1b[1;37m"
rs = "\x1b[0m"
for entry in range(0, i):
i = i - 1
up = d['list'][i]['thumbs_up']
down = d['list'][i]['thumbs_down']
author = d['list'][i]['author']
id = str(d['list'][i]['defid'])
pl = str(d['list'][i]['permalink'])
_def = d['list'][i]['definition']
ex = d['list'][i]['example']
score = str(up - down)
print("# " + Y + score + K + " (" + R + str(up) + K + "/"+ B + str(down) + K + ") " +
"by " + G + author + K + " id: " + M + id + K + " permalink: " + M + pl)
print(rs + _def + "\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment