-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcache_url.py
228 lines (190 loc) · 6.18 KB
/
cache_url.py
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import json
from datetime import datetime, timedelta
from dataclasses import dataclass
from tqdm import tqdm
import requests
import os
import time
# How long to wait in between archive requests, in seconds
# Too many requests will result in the IP being temporarily blocked: https://archive.org/details/toomanyrequests_20191110
ARCHIVE_WAIT_TIME = 7
class ArchiveLastCacheNotFoundError(Exception):
pass
@dataclass
class ArchiveEntry:
url: str
last_archived: datetime
broken_source_url_as_of: datetime | None
source_url: str
update_delta: timedelta
@classmethod
def from_dict(cls, dict_entry: dict):
return cls(**dict_entry)
API_KEY = "Bearer " + os.getenv("VUE_APP_PDAP_API_KEY")
UPDATE_FREQUENCY_MAPPING = {
"Incident-based": 7,
"< Hourly": 1 / 24,
"Hourly": 1 / 24,
"Daily": 1,
"Weekly": 7,
"Bi-weekly": 14,
"Monthly": 30,
"Quarterly": 90,
"Annually": 365,
"> Annually": 730,
"On request": None,
"No updates / rarely updated": None,
"Other": None,
}
def archive_url(entry: dict):
"""
:param entry:
:return:
"""
entry["broken_source_url_as_of"] = None
source_url = entry.get("source_url")
try:
wait_then_post(entry, source_url, ARCHIVE_WAIT_TIME)
except Exception as error:
try:
wait_then_post(entry, source_url, 10)
except Exception as error:
print(str(error))
# Send updated data to Data Sources
update_pdap_archives(entry)
def wait_then_post(entry: dict, source_url: str, wait_time: int):
"""
Wait then post to Internet Archive
:param entry:
:param source_url:
:param wait_time: The amount of time to wait
:return:
"""
api_url = f"http://web.archive.org/save/{source_url}"
time.sleep(wait_time)
requests.post(api_url)
# Update the last_cached date if cache is successful
entry["last_cached"] = datetime.now().strftime("%Y-%m-%d")
def handle_missing_source_url(entry: dict):
"""
Record when url was found to be missing,
update PDAP archives, and throw exception
:param entry:
:return:
"""
entry["broken_source_url_as_of"] = datetime.now().strftime("%Y-%m-%d")
update_pdap_archives(entry)
raise Exception("No source_url")
def update_pdap_archives(entry: dict):
"""
Update data in PDAP archives
:param entry:
:return:
"""
entry_json = json.dumps(entry)
response = requests.put(
f"{os.getenv('VITE_VUE_APP_BASE_URL')}/archives",
json=entry_json,
headers={"Authorization": API_KEY},
)
response.raise_for_status()
def get_update_delta(update_frequency: str | None) -> timedelta:
"""
Calculate update delt based on entry's update frequency
:param entry:
:return:
"""
try:
update_delta = UPDATE_FREQUENCY_MAPPING[update_frequency]
except KeyError:
return datetime.max - datetime.today()
if update_delta is None:
return datetime.max - datetime.today()
return timedelta(days=int(update_delta))
def get_website_info_data_last_cached(source_url) -> datetime:
website_info_data = get_website_info_data(source_url)
if not website_info_data["archived_snapshots"]:
raise ArchiveLastCacheNotFoundError
return datetime.strptime(
website_info_data["archived_snapshots"]["closest"]["timestamp"],
"%Y%m%d%H%M%S",
)
def get_last_archived(last_archived: str | None, source_url: str) -> datetime:
"""
Get last archived date of website from Internet Archive.
:param entry:
:param source_url:
:return:
"""
if last_archived is not None:
try:
return datetime.strptime(last_archived, "%Y-%m-%d")
except ValueError:
return datetime.min
# Check if website exists in archive and compare archived website to current site
last_archived = datetime.min
try:
website_info_data_last_cached = get_website_info_data_last_cached(source_url)
except ArchiveLastCacheNotFoundError:
return last_archived
if website_info_data_last_cached > last_archived:
return website_info_data_last_cached
return last_archived
def get_website_info_data(source_url):
website_info = requests.get(
f"https://archive.org/wayback/available?url={source_url}"
)
website_info_data = website_info.json()
return website_info_data
def main():
data = get_from_archives()
extract_url_info_and_archived_if_needed(data)
def extract_url_info_and_archived_if_needed(data: list[dict]):
"""
:param data:
:return:
"""
# Create a tuple of entries with missing source URLs
missing_source_url_entries = tuple(filter(missing_source_url, data))
# Handle entries with missing source URLs
print("Handling missing source urls")
for entry in tqdm(missing_source_url_entries):
handle_missing_source_url(entry)
print("\nFinding entries that need updates")
non_missing_source_url_entries = tuple(filter(lambda e: not missing_source_url(e), data))
entries_needing_updates = []
for entry in tqdm(non_missing_source_url_entries):
if needs_updated(entry):
entries_needing_updates.append(entry)
print(f"Updating {len(entries_needing_updates)} entries that need updates")
# Handle entries that need to be updated
for entry in tqdm(entries_needing_updates):
try:
archive_url(entry)
except Exception as error:
print(str(error))
def missing_source_url(entry: dict):
return entry['source_url'] is None
def needs_updated(entry: dict) -> bool:
"""
Check if entry needs to be updated
:param entry:
:return:
"""
last_archived = get_last_archived(entry["last_cached"], entry["source_url"])
update_delta = get_update_delta(entry["update_frequency"])
return last_archived + update_delta < datetime.now()
def get_from_archives() -> list[dict]:
"""
Get data from PDAP Archive.
:param url:
:return:
"""
response = requests.get(
f"{os.getenv('VITE_VUE_APP_BASE_URL')}/archives",
headers={"Authorization": API_KEY},
)
response.raise_for_status()
return response.json()
if __name__ == "__main__":
main()