yes-man-today
commited on
Commit
•
67ab0aa
1
Parent(s):
428d060
most updated; will clean repo later.
Browse files- api_call_mod.py +152 -0
api_call_mod.py
ADDED
@@ -0,0 +1,152 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import requests
|
2 |
+
import json
|
3 |
+
|
4 |
+
def get_docket_ids(search_term):
|
5 |
+
url = f"https://api.regulations.gov/v4/dockets"
|
6 |
+
params = {
|
7 |
+
'filter[searchTerm]': search_term,
|
8 |
+
'api_key': "IsH1c1CAB0CR8spovnnx2INbLz8gQlVkbmXYII2z"
|
9 |
+
}
|
10 |
+
response = requests.get(url, params=params)
|
11 |
+
if response.status_code == 200:
|
12 |
+
data = response.json()
|
13 |
+
dockets = data['data']
|
14 |
+
docket_ids = [docket['id'] for docket in dockets]
|
15 |
+
return docket_ids
|
16 |
+
else:
|
17 |
+
return f"Error: {response.status_code}"
|
18 |
+
|
19 |
+
class RegulationsDataFetcher:
|
20 |
+
API_KEY = "IsH1c1CAB0CR8spovnnx2INbLz8gQlVkbmXYII2z" #'4T29l93SvmnyNCVFZUFzSfUqTq6k7S0Wqn93sLcH'
|
21 |
+
BASE_COMMENT_URL = 'https://api.regulations.gov/v4/comments'
|
22 |
+
BASE_DOCKET_URL = 'https://api.regulations.gov/v4/dockets/'
|
23 |
+
HEADERS = {
|
24 |
+
'X-Api-Key': API_KEY,
|
25 |
+
'Content-Type': 'application/json'
|
26 |
+
}
|
27 |
+
|
28 |
+
def __init__(self, docket_id):
|
29 |
+
self.docket_id = docket_id
|
30 |
+
self.docket_url = self.BASE_DOCKET_URL + docket_id
|
31 |
+
self.dataset = []
|
32 |
+
|
33 |
+
def fetch_comments(self):
|
34 |
+
"""Fetch a single page of 25 comments."""
|
35 |
+
url = f'{self.BASE_COMMENT_URL}?filter[docketId]={self.docket_id}&page[number]=1&page[size]=25'
|
36 |
+
response = requests.get(url, headers=self.HEADERS)
|
37 |
+
|
38 |
+
if response.status_code == 200:
|
39 |
+
return response.json()
|
40 |
+
else:
|
41 |
+
print(f'Failed to retrieve comments: {response.status_code}')
|
42 |
+
return None
|
43 |
+
|
44 |
+
def get_docket_info(self):
|
45 |
+
"""Get docket information."""
|
46 |
+
response = requests.get(self.docket_url, headers=self.HEADERS)
|
47 |
+
|
48 |
+
if response.status_code == 200:
|
49 |
+
docket_data = response.json()
|
50 |
+
return (docket_data['data']['attributes']['agencyId'],
|
51 |
+
docket_data['data']['attributes']['title'],
|
52 |
+
docket_data['data']['attributes']['modifyDate'],
|
53 |
+
docket_data['data']['attributes']['docketType'],
|
54 |
+
docket_data['data']['attributes']['keywords'])
|
55 |
+
else:
|
56 |
+
print(f'Failed to retrieve docket info: {response.status_code}')
|
57 |
+
return None
|
58 |
+
|
59 |
+
def fetch_comment_details(self, comment_url):
|
60 |
+
"""Fetch detailed information of a comment."""
|
61 |
+
response = requests.get(comment_url, headers=self.HEADERS)
|
62 |
+
if response.status_code == 200:
|
63 |
+
return response.json()
|
64 |
+
else:
|
65 |
+
print(f'Failed to retrieve comment details: {response.status_code}')
|
66 |
+
return None
|
67 |
+
|
68 |
+
def collect_data(self):
|
69 |
+
"""Collect data and reshape into nested dictionary format."""
|
70 |
+
data = self.fetch_comments()
|
71 |
+
docket_info = self.get_docket_info()
|
72 |
+
|
73 |
+
# Initialize the nested dictionary structure
|
74 |
+
nested_data = {
|
75 |
+
"id": self.docket_id,
|
76 |
+
"title": docket_info[1] if docket_info else "Unknown Title",
|
77 |
+
"context": docket_info[2] if docket_info else "Unknown Context",
|
78 |
+
"purpose": docket_info[3],
|
79 |
+
"keywords": docket_info[4],
|
80 |
+
"comments": []
|
81 |
+
}
|
82 |
+
|
83 |
+
if data and 'data' in data:
|
84 |
+
for comment in data['data']:
|
85 |
+
comment_details = self.fetch_comment_details(comment['links']['self'])
|
86 |
+
|
87 |
+
if comment_details and 'data' in comment_details and 'attributes' in comment_details['data']:
|
88 |
+
comment_data = comment_details['data']['attributes']
|
89 |
+
nested_comment = {
|
90 |
+
"text": comment_data.get('comment', ''),
|
91 |
+
"comment_id": comment['id'],
|
92 |
+
"comment_url": comment['links']['self'],
|
93 |
+
"comment_date": comment['attributes']['postedDate'],
|
94 |
+
"comment_title": comment['attributes']['title'],
|
95 |
+
"commenter_fname": comment_data.get('firstName', ''),
|
96 |
+
"commenter_lname": comment_data.get('lastName', ''),
|
97 |
+
"comment_length": len(comment_data.get('comment', '')) if comment_data.get('comment') is not None else 0
|
98 |
+
}
|
99 |
+
nested_data["comments"].append(nested_comment)
|
100 |
+
|
101 |
+
if len(nested_data["comments"]) >= 10:
|
102 |
+
break
|
103 |
+
|
104 |
+
return nested_data
|
105 |
+
|
106 |
+
|
107 |
+
# CREATING DATASET
|
108 |
+
|
109 |
+
opioid_related_terms = [
|
110 |
+
# Types of Opioids
|
111 |
+
"opioids",
|
112 |
+
"heroin",
|
113 |
+
"morphine",
|
114 |
+
"fentanyl",
|
115 |
+
"methadone",
|
116 |
+
"oxycodone",
|
117 |
+
"hydrocodone",
|
118 |
+
"codeine",
|
119 |
+
"tramadol",
|
120 |
+
"prescription opioids",
|
121 |
+
# Withdrawal Support
|
122 |
+
"lofexidine",
|
123 |
+
"buprenorphine",
|
124 |
+
"naloxone",
|
125 |
+
# Related Phrases
|
126 |
+
"opioid epidemic",
|
127 |
+
"opioid abuse",
|
128 |
+
"opioid crisis",
|
129 |
+
"opioid overdose"
|
130 |
+
"opioid tolerance",
|
131 |
+
"opioid treatment program",
|
132 |
+
"medication assisted treatment",
|
133 |
+
]
|
134 |
+
|
135 |
+
docket_ids = set()
|
136 |
+
all_data = []
|
137 |
+
|
138 |
+
for term in opioid_related_terms:
|
139 |
+
docket_ids.update(get_docket_ids(term))
|
140 |
+
|
141 |
+
|
142 |
+
for docket_id in docket_ids:
|
143 |
+
fetcher = RegulationsDataFetcher(docket_id)
|
144 |
+
docket_data = fetcher.collect_data()
|
145 |
+
if len(docket_data["comments"])!=0:
|
146 |
+
print(f'{docket_id} has comments')
|
147 |
+
all_data.append(docket_data)
|
148 |
+
|
149 |
+
json_file_path = 'docket_comments.json'
|
150 |
+
|
151 |
+
with open(json_file_path, 'w') as f:
|
152 |
+
json.dump(all_data, f)
|