39 lines
1.1 KiB
Python
39 lines
1.1 KiB
Python
import sys
|
|
import requests
|
|
|
|
BASE_URL = 'https://www.dictionaryapi.com/api/v3/references/collegiate/json/' # <1>
|
|
|
|
API_KEY = 'b619b55d-faa3-442b-a119-dd906adc79c8' # <2>
|
|
|
|
|
|
def main(args):
|
|
if len(args) < 1:
|
|
print("Please specify a search term")
|
|
sys.exit(1)
|
|
|
|
response = requests.get(
|
|
BASE_URL + args[0],
|
|
params={'key': API_KEY},
|
|
) # <3>
|
|
|
|
if response.status_code == requests.codes.OK:
|
|
data = response.json() # <4>
|
|
for entry in data: # <5>
|
|
if isinstance(entry, dict):
|
|
meta = entry.get("meta")
|
|
if meta:
|
|
part_of_speech = '({})'.format(entry.get('fl'))
|
|
word_id = meta.get("id")
|
|
print("{} {}".format(word_id.upper(), part_of_speech))
|
|
if "shortdef" in entry:
|
|
print('\n'.join(entry['shortdef']))
|
|
print()
|
|
else:
|
|
print(entry)
|
|
|
|
else:
|
|
print("Sorry, HTTP response", response.status_code)
|
|
|
|
if __name__ == '__main__':
|
|
main(sys.argv[1:])
|