-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInternetArchiveInterface.py
47 lines (39 loc) · 1.47 KB
/
InternetArchiveInterface.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
import time
from datetime import datetime
import requests
from exceptions import ArchiveLastCacheNotFoundError
class InternetArchiveInterface:
def __init__(self, s3_keys: str):
self.s3_keys = s3_keys
def get_website_info_data(self, source_url):
website_info = requests.get(
f"https://archive.org/wayback/available?url={source_url}",
timeout=10
)
website_info_data = website_info.json()
return website_info_data
def get_website_info_data_last_cached(self, source_url):
website_info_data = self.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 save_to_internet_archive(self, 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,
headers={"Authorization": f"LOW {self.s3_keys}"},
timeout=10
)
# Update the last_cached date if cache is successful
entry["last_cached"] = datetime.now().strftime("%Y-%m-%d")