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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
|
import urllib3
from ftlangdetect import detect
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
import polars as pl
from tqdm import tqdm
import requests
from multiprocessing.pool import ThreadPool
import json
from datetime import datetime, timezone
from lxml import etree
from bs4 import BeautifulSoup, Comment
import re
def is_https_responding(host):
retries = 0
while retries <= 1:
try:
requests.get(f"https://{host}", timeout=15, allow_redirects=False)
return True
except KeyboardInterrupt:
break
except:
retries += 1
return False
def get_location_header(task):
host, responding = task
if not responding:
return None
retries = 0
while retries <= 1:
try:
r = requests.get(f"https://{host}", timeout=15, allow_redirects=False)
return r.headers.get("Location",)
except KeyboardInterrupt:
break
except:
retries += 1
return None
def map_decorator(func):
def _inner(key):
return key, func(key)
return _inner
def parallel_map(series, func):
tasks = set(series)
with ThreadPool(processes=16) as pool:
mapping = {}
mapping.update(tqdm(pool.imap_unordered(map_decorator(func), tasks), total=len(tasks)))
return list(map(mapping.get, series))
def phase1():
df = pl.read_csv("input/ai.txt")
df = df.with_columns(
pl.Series("is_https_responding", parallel_map(df["domain"], is_https_responding))
)
print(df)
df.write_csv("intermediate/phase1.csv")
def phase2():
df = pl.read_csv("intermediate/phase1.csv")
df = df.with_columns(
pl.Series("http_location", parallel_map(list(df.select("domain", "is_https_responding").iter_rows()), get_location_header))
)
print(df)
df.write_csv("intermediate/phase2.csv")
USERNAME, PASSWORD = '', ''
def download_webpage(url):
if url is None:
return None
if not url.startswith(("http://", "https://")):
url = f"https://{url}"
retries = 0
while retries <= 1:
try:
response = requests.get(
url,
verify=False,
proxies={
"http": f"http://{USERNAME}:{PASSWORD}@unblock.oxylabs.io:60000",
"https": f"https://{USERNAME}:{PASSWORD}@unblock.oxylabs.io:60000",
},
headers={
"X-Oxylabs-Render": "html"
},
)
response_headers = dict(response.headers)
response_headers.pop("Set-Cookie", None)
response_headers.pop("X-Oxylabs-Client-Id", None)
response_headers.pop("X-Oxylabs-Job-Id", None)
return json.dumps({
"headers": response_headers,
"body": response.content.decode("utf8"),
"utc_scrape_timestamp": datetime.now(tz=timezone.utc).isoformat()
}, ensure_ascii=False)
except KeyboardInterrupt:
break
except Exception as ex:
print(ex)
retries += 1
return None
def phase3():
df = pl.read_csv("intermediate/phase2.csv")
df = df.with_columns(
url=(
pl.when(pl.col("is_https_responding") == False).then(None)
.when(pl.col("http_location").is_null()).then("https://" + pl.col("domain"))
.when(pl.col("http_location").str.contains(r"\.ai($|/|#)")).then(pl.col("http_location"))
.otherwise(None)
)
)
print(f"Visiting {df['url'].count()} URLs ({df['url'].n_unique()} unique)")
df = df.with_columns(
pl.Series("response", parallel_map(df["url"], download_webpage))
)
df.write_csv("intermediate/phase3.csv")
def phase4():
df = pl.read_csv("intermediate/phase3.csv", infer_schema=False)
df = df.with_columns(
response_headers=pl.col("response").map_elements(lambda x: json.dumps(json.loads(x)["headers"]),
return_dtype=pl.String),
response_body=pl.col("response").map_elements(lambda x: json.loads(x)["body"], return_dtype=pl.String),
utc_scrape_timestamp=pl.col("response").map_elements(lambda x: json.loads(x)["body"], return_dtype=pl.String),
).drop(
"response"
).with_columns(
response_body_length=pl.col("response_body").str.len_bytes(),
response_content_type=pl.col("response_headers").map_elements(lambda x: json.loads(x).get("Content-Type"), return_dtype=pl.String)
).with_columns(
is_valid_response=(
(pl.col("response_body_length").is_not_null()) &
(pl.col("response_body_length") > 0) &
(pl.col("response_content_type").is_not_null()) &
(pl.col("response_content_type").str.starts_with("text/html"))
)
)
df.write_csv("intermediate/phase4.csv")
ELEMENTS_TO_STRIP = [
"style",
"script",
"svg",
"canvas",
"img"
]
def _get_html_text(html):
if html is None or html == "":
return None
# Use html5lib; better at handling invalid HTML
soup = BeautifulSoup(html, "html5lib")
for tag in soup.find_all(ELEMENTS_TO_STRIP):
tag.decompose()
for comment in soup.find_all(text=lambda x: isinstance(x, Comment)):
comment.decompose()
html = soup.prettify()
# Use lxml; better at extracting text
parser = etree.XMLParser(recover=True)
root = etree.fromstring(html, parser)
if root is None:
return None
text = " ".join(etree.XPath("//text()")(root)).strip()
text = re.sub(r"\s+", " ", text)
return text
def phase5():
df = pl.read_csv("intermediate/phase4.csv", infer_schema=False)
df = df.with_columns(
pl.Series("response_body_parsed", parallel_map(df["response_body"], _get_html_text))
)
df.write_csv("intermediate/phase5.csv")
PARKED_STRINGS = [
".ai for sale",
".ai is for sale",
".ai está à venda",
".ai está a la venta",
"this website is for sale",
"this domain is for sale",
"this domain name is for sale",
"domains for sale:",
"premium domain for sale",
".ai may be for sale",
".ai page under construction"
"this domain is registered, but may still be available",
"this site is not published or does not have a domain assigned to it",
"there are a few potential reasons: you haven't deployed an app yet",
"the specified bucket does not exist",
"looks like this domain isn't connected to a website yet",
"there isn't a github pages site here",
"this deployment cannot be found",
"this domain has been mapped to squarespace, but it has not yet been",
".ai is parked free",
"porkbun.com",
"spaceship.com",
"parked domain name",
"0.ai to 9.ai",
"404 not found the resource requested could not be found",
"404 not found 404 not found",
"404 page not found",
]
def phase6():
df = pl.read_csv("intermediate/phase5.csv", infer_schema=False)
df = df.with_columns(
is_parked=pl.col("response_body_parsed")
.str.to_lowercase()
.str.contains_any(PARKED_STRINGS),
is_short=pl.col("response_body_parsed")
.str.to_lowercase()
.str.split(" ")
.list.unique()
.list.len()
.lt(15)
)
df.write_csv("intermediate/phase6.csv")
def phase7():
df = pl.read_csv("intermediate/phase6.csv", infer_schema=False)
detect(text="This will download the model...")
def _detect_lang(text):
if not text:
return None
lang = detect(text=text, low_memory=False)
return lang["lang"]
df = df.with_columns(
pl.Series("response_body_lang", parallel_map(df["response_body_parsed"], _detect_lang))
)
df = df.with_columns(
considered_for_analysis=(
(pl.col("is_https_responding") == "true") &
pl.col("url").is_unique() &
(pl.col("is_valid_response") == "true") &
(pl.col("is_parked") == "false") &
(pl.col("is_short") == "false") &
(pl.col("response_body_lang") == "en")
)
)
df.write_csv("output/output.csv")
def get_sankey_info():
nodes = []
links = []
df = pl.read_csv("intermediate/phase7.csv", infer_schema=False)
nodes.append({ "id": "Initial list", "color": "#7f7f7f" })
nodes.append({ "id": "Connection error", "color": "#e15759" })
links.append({ "source": "Initial list", "target": "Connection error", "value": len(df.filter(pl.col("is_https_responding") == "false")) })
df = df.filter(pl.col("is_https_responding") == "true")
nodes.append({ "id": "HTTPS Connection", "color": "#739d6e" })
links.append({ "source": "Initial list", "target": "HTTPS Connection", "value": len(df) })
nodes.append({ "id": "Duplicate URL after redirect", "color": "#e15759" })
nodes.append({ "id": "Non-HTML response", "color": "#e15759" })
links.append({ "source": "HTTPS Connection", "target": "Duplicate URL after redirect", "value": len(df.filter(~pl.col("url").is_unique())) })
links.append({ "source": "HTTPS Connection", "target": "Non-HTML response", "value": len(df.filter(pl.col("is_valid_response") == "false")) })
df = df.filter(pl.col("is_valid_response") == "true")
nodes.append({ "id": "Valid HTML response", "color": "#739d6e" })
nodes.append({ "id": "Parked domain", "color": "#e15759" })
nodes.append({ "id": "Content too short", "color": "#e15759" })
links.append({ "source": "Valid HTML response", "target": "Parked domain", "value": len(df.filter(pl.col("is_parked") == "true")) })
links.append({ "source": "Valid HTML response", "target": "Content too short", "value": len(df.filter(pl.col("is_short") == "true")) })
df = df.filter((pl.col("is_short") == "false") & (pl.col("is_parked") == "false"))
nodes.append({ "id": "Valid content", "color": "#739d6e" })
nodes.append({ "id": "Non-English content", "color": "#e15759" })
nodes.append({ "id": "Considered for analysis", "color": "#59a14f" })
links.append({ "source": "Valid content", "target": "Non-English content", "value": len(df.filter(pl.col("response_body_lang") != "en")) })
links.append({ "source": "Valid content", "target": "Considered for analysis", "value": len(df.filter(pl.col("considered_for_analysis") == "true")) })
return nodes, links
if __name__ == "__main__":
phase1()
phase2()
phase3()
phase4()
phase5()
phase6()
phase7()
nodes, links = get_sankey_info()
print(nodes)
print(links)
print("Done!")
|