File size: 3,841 Bytes
457e1ea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import requests
import pandas as pd

class RegulationsDataFetcher:
    API_KEY = '4T29l93SvmnyNCVFZUFzSfUqTq6k7S0Wqn93sLcH'
    BASE_COMMENT_URL = 'https://api.regulations.gov/v4/comments'
    BASE_DOCKET_URL = 'https://api.regulations.gov/v4/dockets/'
    HEADERS = {
        'X-Api-Key': API_KEY,
        'Content-Type': 'application/json'
    }

    def __init__(self, docket_id):
        self.docket_id = docket_id
        self.docket_url = self.BASE_DOCKET_URL + docket_id
        self.dataset = []

    def fetch_comments(self, page_number):
        """Fetch a page of comments."""
        url = f'{self.BASE_COMMENT_URL}?filter[docketId]={self.docket_id}&page[number]={page_number}&page[size]=100'
        response = requests.get(url, headers=self.HEADERS)
        
        if response.status_code == 200:
            return response.json()
        else:
            print(f'Failed to retrieve comments: {response.status_code}')
            return None

    def get_docket_info(self):
        """Get docket information."""
        response = requests.get(self.docket_url, headers=self.HEADERS)
        
        if response.status_code == 200:
            docket_data = response.json()
            return (docket_data['data']['attributes']['agencyId'],
                    docket_data['data']['attributes']['title'],
                    docket_data['data']['attributes']['modifyDate'])
        else:
            print(f'Failed to retrieve docket info: {response.status_code}')
            return None

    def fetch_comment_details(self, comment_url):
        """Fetch detailed information of a comment."""
        response = requests.get(comment_url, headers=self.HEADERS)
        if response.status_code == 200:
            return response.json()
        else:
            print(f'Failed to retrieve comment details: {response.status_code}')
            return None

    def collect_data(self):
        """Collect data by iterating through all comment pages."""
        initial_data = self.fetch_comments(1)
        total_pages = initial_data['meta']['totalPages'] if initial_data else 0
        docket_info = self.get_docket_info()

        for page_number in range(1, total_pages + 1):
            data = self.fetch_comments(page_number)
            
            if data:
                for comment in data['data']:
                    comment_details = self.fetch_comment_details(comment['links']['self'])

                    if comment_details:
                        comment_data = comment_details['data']['attributes']
                        self.dataset.append({
                            'docket_agency': docket_info[0],
                            'docket_title': docket_info[1],
                            'docket_date': docket_info[2],
                            'comment_id': comment['id'],
                            'comment_url': comment['links']['self'],
                            'comment_date': comment['attributes']['postedDate'],
                            'comment_title': comment['attributes']['title'],
                            'commenter_name': comment_data.get('firstName', '') + " " + comment_data.get('lastName', ''),
                            'comment_length': len(comment_data.get('comment', '')),
                            'comment_text': comment_data.get('comment', '')
                        })

                    if len(self.dataset) == 10: #temporary for runtime
                        break 
                    

# Example usage
docket_ids = ['SAMHSA-2016-0001', 'SAMHSA-2023-0001', 'DEA-2020-0031', 'CMS-2021-0167', 'DOD-2015-HA-0109']
all_data = []

for docket_id in docket_ids:
    fetcher = RegulationsDataFetcher(docket_id)
    fetcher.collect_data()
    all_data.extend(fetcher.dataset)

combined_df = pd.DataFrame(all_data)
combined_df.to_csv('temp.csv', index=False)