Skip to content

Instantly share code, notes, and snippets.

@innateessence
Last active February 15, 2020 07:29
Show Gist options
  • Save innateessence/778f0351e3d74772f94823ae65a014b5 to your computer and use it in GitHub Desktop.
Save innateessence/778f0351e3d74772f94823ae65a014b5 to your computer and use it in GitHub Desktop.
cli tool to define a word
#!/usr/bin/env python
import requests
from bs4 import BeautifulSoup
from argparse import ArgumentParser
def get_args():
parser = ArgumentParser()
parser.add_argument('query', default=None, type=str, help="lookup query")
parser.add_argument('-u', '--urban', action='store_true', default=False, help="urban dictionary lookup")
parser.add_argument('-w', '--websters', action='store_true', default=False, help="websters dictionary lookup")
parser.add_argument('-v', '--verbose', action='store_true', default=False, help="verbose output")
return parser.parse_args()
def urban_dictionary(query, verbose=False):
url = "https://www.urbandictionary.com/define.php?term={}".format(query.replace(' ', '+'))
r = requests.get(url)
soup = BeautifulSoup(r.content, 'html.parser')
result = soup.find('div', class_='meaning').text.strip()
if verbose:
print("[+] {} - Urban Dictionary\n".format(query))
return result
def websters_dictionary(query, verbose=False):
url = 'https://www.merriam-webster.com/dictionary/{}'.format(query.replace(' ', '%20'))
r = requests.get(url)
soup = BeautifulSoup(r.content, 'html.parser')
if verbose:
print("[+] {} - Websters Dictionary\n".format(query))
return (soup.find('div', id='dictionary-entry-1').find(class_='dtText').text.strip()[2:])
def Main():
args = get_args()
if args.query is None:
args.query = input("Seach Query: ")
if args.urban:
result = urban_dictionary(args.query, args.verbose)
elif args.websters:
result = websters_dictionary(args.query, args.verbose)
print(result)
if __name__ == '__main__':
Main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment