-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate_known_rules.py
More file actions
213 lines (183 loc) · 8.16 KB
/
update_known_rules.py
File metadata and controls
213 lines (183 loc) · 8.16 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
from datetime import datetime, timedelta, timezone
from os import getenv
from rule_scout import (
NOTION_RULE_DATABASE,
Docket,
NotionApi,
RegulationsGovApi,
notion_rich_text,
notion_rich_text_url_list,
)
import time
from typing import Any
# If true, checks for changes to metadata associated with Dockets (e.g. RINs,
# keywords) on every run, instead of only if new dockets were found.
ALWAYS_UPDATE_DOCKET_DATA = False
# Basic rate limit is 1,000/hour, or 1 request every 3.6 seconds. Based on:
# https://api.data.gov/docs/developer-manual/
REGULATIONS_GOV_REQUEST_INTERVAL = 3.6
# TODO: consider how to better implement this. The easy thing is to put
# @lru_cache on the RegulationsGovApi.get_docket method, but that could
# introduce problems if the way we use it changes.
DOCKET_CACHE: dict[str, Docket] = {}
def parse_rich_text_list(notion_object: dict) -> list[str]:
text = NotionApi.cell_as_text(notion_object)
if text:
return [item.strip() for item in text.split(',')]
else:
return []
def parse_multiselect_set(notion_object: dict) -> set[str]:
if 'multi_select' not in notion_object:
raise TypeError(f'Object is not a multi_select, it is "{notion_object.get('type')}"')
return set([item['name'] for item in notion_object['multi_select']])
def get_page_updates(regulations_gov: RegulationsGovApi, page: dict) -> dict[str, Any]:
updates = {}
fr_number = NotionApi.cell_as_text(page['properties']['FR Document Number'])
fr_date = NotionApi.cell_as_datetime(page['properties']['FR Publication Date'])
print(f'{fr_number}: {fr_date}')
old_comment_deadline_iso = page['properties'].get('Comment End Date')
old_comment_deadline = NotionApi.cell_as_datetime(old_comment_deadline_iso) if old_comment_deadline_iso else None
old_docket_docs = parse_rich_text_list(page['properties']['Docket Documents'])
old_dockets = parse_rich_text_list(page['properties']['Dockets'])
time.sleep(REGULATIONS_GOV_REQUEST_INTERVAL)
doc_infos = regulations_gov.find_documents_by_register_id(fr_number)
found_docs = []
found_dockets = []
latest_comment_date = old_comment_deadline
for found in doc_infos:
found_id = found['id']
found_docs.append(found_id)
found_docket = found['attributes']['docketId']
if found_docket:
found_dockets.append(found_docket)
if found['attributes']['commentEndDate']:
found_comment_date = datetime.fromisoformat(found['attributes']['commentEndDate'])
if not found_comment_date.tzinfo:
found_comment_date = found_comment_date.astimezone(timezone.utc)
# Notion dates only have minute-level precision.
found_comment_date = found_comment_date.replace(second=0, microsecond=0)
if (not latest_comment_date) or found_comment_date > latest_comment_date:
latest_comment_date = found_comment_date
if set(old_docket_docs) != set(found_docs):
print(f' Docket Docs (Old): {sorted(old_docket_docs)}')
print(f' (New): {sorted(found_docs)}')
updates['Docket Documents'] = {
'type': 'rich_text',
'rich_text': notion_rich_text_url_list(
(d, f'https://www.regulations.gov/document/{d}')
for d in sorted(found_docs)
)
}
if set(old_dockets) != set(found_dockets):
print(f' Dockets (Old): {sorted(old_dockets)}')
print(f' (New): {sorted(found_dockets)}')
updates['Dockets'] = {
'type': 'rich_text',
'rich_text': notion_rich_text_url_list(
(d, f'https://www.regulations.gov/docket/{d}')
for d in sorted(found_dockets)
)
}
if ALWAYS_UPDATE_DOCKET_DATA or 'Dockets' in updates:
new_keywords = set()
new_rins = set()
for docket_id in found_dockets:
docket = DOCKET_CACHE.get(docket_id)
if not docket:
time.sleep(REGULATIONS_GOV_REQUEST_INTERVAL)
docket = Docket.from_api(regulations_gov.get_docket(docket_id))
DOCKET_CACHE[docket_id] = docket
new_keywords.update(docket.keywords)
if docket.rin:
new_rins.add(docket.rin)
old_keywords = parse_multiselect_set(page['properties']['Docket Keywords'])
if old_keywords != new_keywords:
print(f' KW (old): {sorted(old_keywords)}')
print(f' (new): {sorted(new_keywords)}')
updates['Docket Keywords'] = {
'type': 'multi_select',
'multi_select': [
{'name': keyword}
for keyword in sorted(new_keywords)
]
}
fr_topics = parse_multiselect_set(page['properties']['FR Topics'])
updates['Tags'] = {
'type': 'multi_select',
'multi_select': [
{'name': tag}
for tag in sorted([*fr_topics, *new_keywords])
]
}
# RINs are a little complicated; they belong to Dockets, but sometimes
# a document on regulations.gov does not have a user-visible docket.
# The correct RINs are often listed on the Federal Register, which we
# don't re-query here, so we only add to the list of known RINs and
# never remove.
old_rins = set(parse_rich_text_list(page['properties']['RINs']))
# TODO: remove this old "Not Assigned" check after remediating old
# data. These always should have been skipped and this is here to help
# clear them out.
if new_rins.difference(old_rins) or 'Not Assigned' in old_rins:
new_rins.update([
rin
for rin in old_rins
if rin.lower() != 'not assigned'
])
print(f' RIN (old): {sorted(old_rins)}')
print(f' (new): {sorted(new_rins)}')
updates['RINs'] = notion_rich_text(', '.join(sorted(new_rins)))
if old_comment_deadline != latest_comment_date:
print(f' New comment deadline: {latest_comment_date} (old: {old_comment_deadline})')
updates['Comment End Date'] = {
'type': 'date',
'date': {
'start': latest_comment_date.isoformat()
} if latest_comment_date else None
}
return updates
def main() -> None:
COMMIT = True
DEBUG = True
with NotionApi(getenv('NOTION_API_KEY')) as notion:
with RegulationsGovApi(getenv(key='REGULATIONS_GOV_API_KEY')) as regulations_gov:
active_as_of_date = (datetime.now(tz=timezone.utc) - timedelta(days=14)).isoformat()
rule_rows = notion.query_db(
NOTION_RULE_DATABASE,
{
'or': [
{
'property': 'Comment End Date',
'date': {
'on_or_after': active_as_of_date
}
},
{
'and': [
{
'property': 'Comment End Date',
'date': {
'is_empty': True
}
},
{
'property': 'FR Publication Date',
'date': {
'on_or_after': active_as_of_date
}
}
]
}
]
},
sort={'FR Publication Date': 'ascending'}
)
for page in rule_rows:
updates = get_page_updates(regulations_gov, page)
if updates:
if DEBUG:
print(f' Updates: {updates}')
if COMMIT:
notion.update_page(page['id'], updates)
if __name__ == '__main__':
main()