Last active
October 3, 2017 21:04
-
-
Save avalanchy/b3c80c145656dba0859330f3164607b5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Sloppy but colorful thesaurus.com for CLI | |
Requires Python 3.6 | |
Usage: | |
pip install ansicolors pyquery | |
python thesaurus.py private | |
""" | |
from json import loads | |
from sys import argv | |
from urllib.error import HTTPError | |
from colors import color | |
from pyquery import PyQuery | |
try: | |
pq = PyQuery(url=f'http://www.thesaurus.com/browse/{argv[1]}') | |
except IndexError: | |
exit('Provide the search word as an argument') | |
except HTTPError: | |
exit('The search word was not found') | |
synonyms = [(x.text, loads(x.getparent().get('data-category'))['color']) for x in pq('#filters-0').find('.text')] | |
antonyms = [(x.text, loads(x.getparent().get('data-category'))['color']) for x in pq('.antonyms').find('.text')] | |
print() | |
print(color(' Synonyms ', fg='#000', bg='#fff', style='bold')) | |
for i in range(5): | |
for j in range(i, len(synonyms), 5): | |
s, c = synonyms[j] | |
print(color(f' {s} ', fg='#000', bg=c), end='') | |
print() | |
print() | |
print(color(' Antonyms ', fg='#000', bg='#fff', style='bold')) | |
for i in range(5): | |
for j in range(i, len(antonyms), 5): | |
s, c = antonyms[j] | |
print(color(f' {s} ', fg='#000', bg=c), end='') | |
print() | |
print() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment