Delete api_call.py
Browse files- api_call.py +0 -93
api_call.py
DELETED
|
@@ -1,93 +0,0 @@
|
|
| 1 |
-
import requests
|
| 2 |
-
import pandas as pd
|
| 3 |
-
|
| 4 |
-
class RegulationsDataFetcher:
|
| 5 |
-
API_KEY = '4T29l93SvmnyNCVFZUFzSfUqTq6k7S0Wqn93sLcH'
|
| 6 |
-
BASE_COMMENT_URL = 'https://api.regulations.gov/v4/comments'
|
| 7 |
-
BASE_DOCKET_URL = 'https://api.regulations.gov/v4/dockets/'
|
| 8 |
-
HEADERS = {
|
| 9 |
-
'X-Api-Key': API_KEY,
|
| 10 |
-
'Content-Type': 'application/json'
|
| 11 |
-
}
|
| 12 |
-
|
| 13 |
-
def __init__(self, docket_id):
|
| 14 |
-
self.docket_id = docket_id
|
| 15 |
-
self.docket_url = self.BASE_DOCKET_URL + docket_id
|
| 16 |
-
self.dataset = []
|
| 17 |
-
|
| 18 |
-
def fetch_comments(self, page_number):
|
| 19 |
-
"""Fetch a page of comments."""
|
| 20 |
-
url = f'{self.BASE_COMMENT_URL}?filter[docketId]={self.docket_id}&page[number]={page_number}&page[size]=100'
|
| 21 |
-
response = requests.get(url, headers=self.HEADERS)
|
| 22 |
-
|
| 23 |
-
if response.status_code == 200:
|
| 24 |
-
return response.json()
|
| 25 |
-
else:
|
| 26 |
-
print(f'Failed to retrieve comments: {response.status_code}')
|
| 27 |
-
return None
|
| 28 |
-
|
| 29 |
-
def get_docket_info(self):
|
| 30 |
-
"""Get docket information."""
|
| 31 |
-
response = requests.get(self.docket_url, headers=self.HEADERS)
|
| 32 |
-
|
| 33 |
-
if response.status_code == 200:
|
| 34 |
-
docket_data = response.json()
|
| 35 |
-
return (docket_data['data']['attributes']['agencyId'],
|
| 36 |
-
docket_data['data']['attributes']['title'],
|
| 37 |
-
docket_data['data']['attributes']['modifyDate'])
|
| 38 |
-
else:
|
| 39 |
-
print(f'Failed to retrieve docket info: {response.status_code}')
|
| 40 |
-
return None
|
| 41 |
-
|
| 42 |
-
def fetch_comment_details(self, comment_url):
|
| 43 |
-
"""Fetch detailed information of a comment."""
|
| 44 |
-
response = requests.get(comment_url, headers=self.HEADERS)
|
| 45 |
-
if response.status_code == 200:
|
| 46 |
-
return response.json()
|
| 47 |
-
else:
|
| 48 |
-
print(f'Failed to retrieve comment details: {response.status_code}')
|
| 49 |
-
return None
|
| 50 |
-
|
| 51 |
-
def collect_data(self):
|
| 52 |
-
"""Collect data by iterating through all comment pages."""
|
| 53 |
-
initial_data = self.fetch_comments(1)
|
| 54 |
-
total_pages = initial_data['meta']['totalPages'] if initial_data else 0
|
| 55 |
-
docket_info = self.get_docket_info()
|
| 56 |
-
|
| 57 |
-
for page_number in range(1, total_pages + 1):
|
| 58 |
-
data = self.fetch_comments(page_number)
|
| 59 |
-
|
| 60 |
-
if data:
|
| 61 |
-
for comment in data['data']:
|
| 62 |
-
comment_details = self.fetch_comment_details(comment['links']['self'])
|
| 63 |
-
|
| 64 |
-
if comment_details:
|
| 65 |
-
comment_data = comment_details['data']['attributes']
|
| 66 |
-
self.dataset.append({
|
| 67 |
-
'docket_agency': docket_info[0],
|
| 68 |
-
'docket_title': docket_info[1],
|
| 69 |
-
'docket_date': docket_info[2],
|
| 70 |
-
'comment_id': comment['id'],
|
| 71 |
-
'comment_url': comment['links']['self'],
|
| 72 |
-
'comment_date': comment['attributes']['postedDate'],
|
| 73 |
-
'comment_title': comment['attributes']['title'],
|
| 74 |
-
'commenter_name': comment_data.get('firstName', '') + " " + comment_data.get('lastName', ''),
|
| 75 |
-
'comment_length': len(comment_data.get('comment', '')),
|
| 76 |
-
'comment_text': comment_data.get('comment', '')
|
| 77 |
-
})
|
| 78 |
-
|
| 79 |
-
if len(self.dataset) == 10: #temporary for runtime
|
| 80 |
-
break
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
# Example usage
|
| 84 |
-
docket_ids = ['SAMHSA-2016-0001', 'SAMHSA-2023-0001', 'DEA-2020-0031', 'CMS-2021-0167', 'DOD-2015-HA-0109']
|
| 85 |
-
all_data = []
|
| 86 |
-
|
| 87 |
-
for docket_id in docket_ids:
|
| 88 |
-
fetcher = RegulationsDataFetcher(docket_id)
|
| 89 |
-
fetcher.collect_data()
|
| 90 |
-
all_data.extend(fetcher.dataset)
|
| 91 |
-
|
| 92 |
-
combined_df = pd.DataFrame(all_data)
|
| 93 |
-
combined_df.to_csv('temp.csv', index=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|