-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollect_historical_data.py
163 lines (129 loc) · 5.23 KB
/
collect_historical_data.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
import gzip
import json
import os
import signal
import time
from contextlib import contextmanager
from hashlib import sha256
from multiprocessing import Pool
import psycopg2 as psycopg2
import requests
from config import PREFIX, DB_HOST, DB_PORT, DB_NAME, DB_USER, DB_PWD, USER_AGENT, STORAGE
""" Archival data used for section 5.3 """
TABLE_NAME = 'historical_data'
DATE = "20230417"
DEBUG = False
def setup():
with psycopg2.connect(host=DB_HOST, port=DB_PORT, database=DB_NAME, user=DB_USER, password=DB_PWD) as connection:
with connection.cursor() as cursor:
cursor.execute(f"""
CREATE TABLE IF NOT EXISTS {TABLE_NAME} (
id SERIAL PRIMARY KEY,
tranco_id INTEGER,
domain VARCHAR(128),
start_url VARCHAR(128),
end_url TEXT DEFAULT NULL,
headers JSONB DEFAULT NULL,
timestamp TIMESTAMP DEFAULT NOW(),
duration NUMERIC DEFAULT NULL,
content_hash VARCHAR(64) DEFAULT NULL,
status_code INT DEFAULT -1
);
""")
print(f'<<< CREATE INDEX ON {TABLE_NAME} >>>')
for column in ['tranco_id', 'domain', 'start_url', 'end_url', 'timestamp', 'duration', 'content_hash',
'status_code']:
cursor.execute(f"CREATE INDEX ON {TABLE_NAME} ({column})")
def raise_timeout(signum, frame):
if DEBUG:
print('Hard kill via signal!')
print(signum)
print(frame)
raise TimeoutError
@contextmanager
def timeout(seconds):
# Register a function to raise a TimeoutError on the signal.
signal.signal(signal.SIGALRM, raise_timeout)
# Schedule the signal to be sent after the specified seconds.
signal.alarm(seconds)
try:
yield
finally:
# Unregister the signal, so it won't be triggered, if the timeout is not reached.
signal.signal(signal.SIGALRM, signal.SIG_IGN)
def crawl(url, headers=None, user_agent=USER_AGENT, sess=None):
if sess is None:
sess = requests.Session()
if headers is None:
headers = dict()
headers['User-Agent'] = user_agent
try:
with timeout(60):
archive_request_url = url
start = time.time_ns()
r = sess.get(archive_request_url, headers=headers, timeout=30)
duration = time.time_ns() - start
except TimeoutError:
return False, 'Hard kill due to signal timeout!'
except Exception as exp:
return False, str(exp)
content = r.content
content_hash = sha256(content).hexdigest()
file_dir = os.path.join(STORAGE, content_hash[0], content_hash[1])
if not os.path.exists(file_dir):
os.makedirs(file_dir)
file_path = os.path.join(file_dir, f"{content_hash}.gz")
with gzip.open(file_path, "wb") as fh:
fh.write(content)
response_headers = {h.lower(): r.headers[h] for h in r.headers}
response_headers = json.dumps(response_headers)
return True, [r.url, r.status_code, response_headers, content_hash, duration]
def worker(argv):
conn = psycopg2.connect(host=DB_HOST, port=DB_PORT, database=DB_NAME, user=DB_USER, password=DB_PWD)
conn.autocommit = True
cursor = conn.cursor()
sess = requests.Session()
for id_, url in argv:
time.sleep(0.2)
success, data = crawl(url, sess=sess)
if success:
end_url, status_code, headers, content_hash, duration = data
if status_code == 429:
print("got 429ed")
cursor.execute(f"""
INSERT INTO {TABLE_NAME} (tranco_id, domain, start_url, end_url, headers, duration, status_code, content_hash)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s)
""", (id_, url[len(f"https://web.archive.org/web/{DATE}/{PREFIX}"):], url, end_url, headers, duration,
status_code, content_hash))
else:
print(f"{url} failed")
cursor.execute(f"""
INSERT INTO {TABLE_NAME} (tranco_id, domain, start_url, end_url)
VALUES (%s, %s, %s, %s)
""", (id_, url[len(f"https://web.archive.org/web/{DATE}/{PREFIX}"):], url, data))
conn.close()
def collect_data(tranco_file):
# reset failed attempts
conn = psycopg2.connect(host=DB_HOST, port=DB_PORT, database=DB_NAME, user=DB_USER, password=DB_PWD)
conn.autocommit = True
cursor = conn.cursor()
cursor.execute(f"DELETE FROM {TABLE_NAME} WHERE status_code in (-1, 429)")
cursor.execute(f"SELECT start_url FROM {TABLE_NAME}")
worked_urls = set([x[0] for x in cursor.fetchall()])
urls = []
with open(tranco_file) as file:
for line in file:
id_, domain = line.strip().split(',')
url = f"https://web.archive.org/web/{DATE}/{PREFIX}{domain}"
if url in worked_urls:
continue
urls.append((id_, url))
WORKERS = 8
chunks = [urls[i:i + len(urls) // WORKERS] for i in range(0, len(urls), len(urls) // WORKERS)]
with Pool(WORKERS) as p:
p.map(worker, chunks)
def main(tranco_file="live_dataset.csv"):
setup()
collect_data(tranco_file)
if __name__ == '__main__':
main()