-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_req.py
50 lines (38 loc) · 1.45 KB
/
api_req.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
# This file makes API calls to the Dictionary API
import requests
import json
import os
from dotenv import load_dotenv
from scripts.utils import exit_program, read_and_process_json
load_dotenv()
DICT_API_KEY = os.getenv('DICT_API_KEY')
def get_word_data(word_param):
base_url = 'https://www.dictionaryapi.com'
endpoint = '/api/v3/references/collegiate/json/'
try:
response = requests.get(f'{base_url}{endpoint}{word_param}?key={DICT_API_KEY}')
response.raise_for_status()
data = response.json()
return data
except requests.exceptions.RequestException as e:
return {'error': str(e)}
def output_resp_json(input_json_file, output_dir):
json_obj = read_and_process_json(input_json_file)
for item in json_obj:
word_param = item['text']
output = get_word_data(word_param=word_param)
with open(f'{output_dir}{word_param}.json', 'w') as json_file:
json.dump(output, json_file, indent=4)
'''
Define the output_filepath parameter, then call the function to make the request and save the output
'''
# input_json = 'data/words.json'
# output_dir = 'data/dict/'
# output_resp_json(input_json, output_dir)
# Output for phrases
# data/dict/json/phrases/
# Output for words
# data/dict/json/words/
input_json = 'new-batch.json'
output_dir = 'data/dict/'
output_resp_json(input_json, output_dir)