yes-man-today
commited on
Commit
•
82b195a
1
Parent(s):
36e6da4
with cleaning
Browse files- regulatory_comments_api.py +28 -17
regulatory_comments_api.py
CHANGED
@@ -94,39 +94,50 @@ class RegulationsDataFetcher:
|
|
94 |
"""Collect data and reshape into nested dictionary format."""
|
95 |
data = self.fetch_comments()
|
96 |
if data is None:
|
97 |
-
return None
|
98 |
|
99 |
docket_info = self.get_docket_info()
|
100 |
if docket_info is None:
|
101 |
return None
|
102 |
|
103 |
-
|
|
|
|
|
104 |
nested_data = {
|
105 |
"id": self.docket_id,
|
|
|
106 |
"title": docket_info[1] if docket_info else "Unknown Title",
|
107 |
-
"
|
|
|
108 |
"purpose": docket_info[3],
|
109 |
"keywords": docket_info[4],
|
110 |
"comments": []
|
111 |
}
|
112 |
|
113 |
-
if
|
114 |
for comment in data['data']:
|
115 |
comment_details = self.fetch_comment_details(comment['links']['self'])
|
116 |
-
|
117 |
-
if comment_details and 'data' in comment_details and 'attributes' in comment_details['data']:
|
118 |
comment_data = comment_details['data']['attributes']
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
"
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
130 |
|
131 |
if len(nested_data["comments"]) >= 10:
|
132 |
break
|
|
|
94 |
"""Collect data and reshape into nested dictionary format."""
|
95 |
data = self.fetch_comments()
|
96 |
if data is None:
|
97 |
+
return None
|
98 |
|
99 |
docket_info = self.get_docket_info()
|
100 |
if docket_info is None:
|
101 |
return None
|
102 |
|
103 |
+
agency_id = self.docket_id[:3]
|
104 |
+
agency = "VA" if agency_id == "VA-" else agency_id
|
105 |
+
|
106 |
nested_data = {
|
107 |
"id": self.docket_id,
|
108 |
+
"agency": agency,
|
109 |
"title": docket_info[1] if docket_info else "Unknown Title",
|
110 |
+
"update_date": docket_info[2].split('T')[0] if docket_info and docket_info[2] else "Unknown Update Date",
|
111 |
+
"update_time": docket_info[2].split('T')[1].strip('Z') if docket_info and docket_info[2] and 'T' in docket_info[2] else "Unknown Update Time",
|
112 |
"purpose": docket_info[3],
|
113 |
"keywords": docket_info[4],
|
114 |
"comments": []
|
115 |
}
|
116 |
|
117 |
+
if 'data' in data:
|
118 |
for comment in data['data']:
|
119 |
comment_details = self.fetch_comment_details(comment['links']['self'])
|
120 |
+
if 'data' in comment_details and 'attributes' in comment_details['data']:
|
|
|
121 |
comment_data = comment_details['data']['attributes']
|
122 |
+
comment_text = (comment_data.get('comment', '') or '').strip()
|
123 |
+
|
124 |
+
# Data cleaning: check for non-empty text, absence of "see attachment", and text not being "N/A"
|
125 |
+
if (comment_text and "see attachment" not in comment_text.lower() and comment_text.lower() != "n/a"):
|
126 |
+
# Replace empty commenter_fname with "Anonymous"
|
127 |
+
commenter_fname = (comment_data.get('firstName', '').split(',')[0]).strip()
|
128 |
+
commenter_fname = commenter_fname.capitalize() if commenter_fname else "Anonymous"
|
129 |
+
|
130 |
+
nested_comment = {
|
131 |
+
"text": comment_text,
|
132 |
+
"comment_id": comment['id'],
|
133 |
+
"comment_url": comment['links']['self'],
|
134 |
+
"comment_date": comment['attributes']['postedDate'].split('T')[0],
|
135 |
+
"comment_time": comment['attributes']['postedDate'].split('T')[1].strip('Z'),
|
136 |
+
"commenter_fname": commenter_fname,
|
137 |
+
"commenter_lname": (comment_data.get('lastName', '').split(',')[0]).capitalize(),
|
138 |
+
"comment_length": len(comment_text)
|
139 |
+
}
|
140 |
+
nested_data["comments"].append(nested_comment)
|
141 |
|
142 |
if len(nested_data["comments"]) >= 10:
|
143 |
break
|