-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfetch.py
executable file
·357 lines (297 loc) · 10.6 KB
/
fetch.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
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
#!/usr/bin/env python3
import asyncio
import itertools
import json
import os
import re
import sqlite3
import sys
import time
import traceback
from asyncio.exceptions import CancelledError
from functools import partial
import aiohttp
from dateutil.parser import isoparse
from config import Configuration
REGEX_CROM_RATE_LIMIT = re.compile(r"(?:in|for) (\d+) seconds?")
REGEX_WIKIDOT_URL = re.compile(r'^https?://([\w\-]+)\.wikidot\.com/(.+)$')
REGEX_MODULE_CSS = re.compile(r'\[\[module +css\]\]\n(.+?)\n\[\[/module\]\]', re.IGNORECASE | re.DOTALL)
REGEX_INLINE_CSS = re.compile(r'style="(.+?)"[^\]]*?\]\]', re.MULTILINE | re.IGNORECASE)
REGEX_INCLUDES = re.compile(r'\[\[include +([a-z0-9:\-_]+?)(?: |\]\])', re.MULTILINE | re.IGNORECASE)
REGEX_CLASSES = re.compile(r'class="([^\]]+?)"', re.MULTILINE | re.IGNORECASE)
CROM_ENDPOINT = "https://api.crom.avn.sh/graphql"
CROM_RETRIES = 3
CROM_HEADERS = {
"Accept-Encoding": "gzip, deflate, br",
"Content-Type": "application/json",
"Accept": "application/json",
}
CROM_QUERY = """
{
pages(
filter: {
anyBaseUrl: $anyBaseUrl,
wikidotInfo: {
createdAt: {
gte: $lastCreatedAt,
},
},
},
sort: {
order: ASC,
key: CREATED_AT,
},
first: 100,
after: $cursor,
) {
edges {
node {
url,
wikidotInfo {
title,
category,
createdAt,
wikidotId,
source,
}
}
},
pageInfo {
hasNextPage,
endCursor,
}
}
}
"""
SQLITE_SEED_PATH = os.path.join(os.path.dirname(__file__), "schema.sql")
with open(SQLITE_SEED_PATH) as file:
SQLITE_SEED = file.read()
def format_date(iso_date):
if iso_date is None:
return 'None'
date = isoparse(iso_date)
return f"{date.year}/{date.month}/{date.day}"
# Always open files using UTF-8
open = partial(open, encoding='utf-8')
class Container:
__slots__ = ("value",)
def __init__(self, value=None):
self.value = value
def get(self):
return self.value
def set(self, value):
self.value = value
def __str__(self):
return str(self.value)
class CromError(RuntimeError):
def __init__(self, errors):
super().__init__(self._get_message(errors))
self.errors = errors
self.ratelimit = self._get_ratelimit()
@staticmethod
def _get_message(errors):
if len(errors) == 1:
return errors[0]
else:
return errors
def _get_ratelimit(self):
for error in self.errors:
match = REGEX_CROM_RATE_LIMIT.search(error["message"])
if match is not None:
return int(match[1])
return None
class Crawler:
def __init__(self, config):
self.config = config
self.path = config.output_path
self.connect()
def connect(self):
database_exists = os.path.exists(config.output_path)
self.conn = sqlite3.connect(self.path)
if database_exists:
print("Loaded previous crawler state")
with self.conn as cur:
result = cur.execute("SELECT cursor_state, last_created_at FROM crawler_state")
self.cursor, self.last_created_at = result.fetchone()
else:
print("No previous crawler state, starting fresh")
with self.conn as cur:
cur.executescript(SQLITE_SEED)
self.cursor = None
self.last_created_at = None
def close(self):
with self.conn as cur:
cur.execute("DELETE FROM crawler_state")
cur.execute(
"""
INSERT INTO crawler_state
(cursor_state, last_created_at)
VALUES
(?, ?)
""",
(self.cursor, self.last_created_at),
)
self.conn.close()
self.conn = None
def write_page(self, page):
with self.conn as cur:
cur.execute(
"""
INSERT INTO pages
(url, slug, title, category, created_at, wikidot_page_id, source)
VALUES
(?, ?, ?, ?, ?, ?, ?)
ON CONFLICT (url)
DO UPDATE
SET
slug = ?,
title = ?,
category = ?,
created_at = ?,
wikidot_page_id = ?,
source = ?
""",
(
page['url'],
page['slug'],
page['title'],
page['category'],
page['created_at'],
page['wikidot_page_id'],
page['source'],
page['slug'],
page['title'],
page['category'],
page['created_at'],
page['wikidot_page_id'],
page['source'],
),
)
cur.execute("DELETE FROM extracts WHERE page_url = ?", (page['url'],))
def add_extracts(extract_type, extracts):
for idx, extract in enumerate(extracts):
cur.execute(
"""
INSERT INTO extracts
(page_url, extract_index, extract_type, source)
VALUES
(?, ?, ?, ?)
""",
(
page['url'],
idx,
extract_type,
extract,
),
)
add_extracts("module_style", page["module_styles"])
add_extracts("inline_style", page["inline_styles"])
add_extracts("include", page["includes"])
add_extracts("class", page["classes"])
async def raw_request(self, session, query, variables):
for key, value in variables.items():
query = query.replace(key, json.dumps(value))
payload = json.dumps({ 'query': query }).encode('utf-8')
try:
async with session.post(CROM_ENDPOINT, data=payload, headers=CROM_HEADERS) as r:
json_body = await r.json()
if 'errors' in json_body:
raise CromError(json_body['errors'])
return json_body['data']
except CromError as error:
if error.ratelimit is None:
raise error
# Otherwise, try again
print(f"Ratelimited, trying again after {error.ratelimit} seconds")
time.sleep(error.ratelimit)
return await self.raw_request(session, query, variables)
async def next_pages(self, session):
variables = {
"$anyBaseUrl": self.config.crom_base_urls,
"$lastCreatedAt": self.last_created_at,
"$cursor": self.cursor,
}
json_body = await self.raw_request(session, CROM_QUERY, variables)
pages = json_body['pages']
page_info = pages['pageInfo']
has_next_page = page_info['hasNextPage']
if has_next_page:
self.cursor = page_info['endCursor']
return pages['edges'], has_next_page
@staticmethod
def process_edge(edge):
# Extract fields
node = edge['node']
url = node['url']
slug = REGEX_WIKIDOT_URL.match(url)[2]
# Scrape styling from page source
wikidot_info = node['wikidotInfo']
source = wikidot_info['source']
if source is None:
# This can happen sometimes, for some reason
# It's obviously a problem, so let's just catch it here explicitly
return None, slug
module_styles = REGEX_MODULE_CSS.findall(source)
inline_styles = REGEX_INLINE_CSS.findall(source)
includes = REGEX_INCLUDES.findall(source)
classes = Crawler.get_css_classes(source)
# Build and page object
page = {
'url': url,
'slug': slug,
'title': wikidot_info['title'],
'category': wikidot_info['category'],
'created_at': wikidot_info['createdAt'],
'wikidot_page_id': wikidot_info['wikidotId'],
'source': source,
'module_styles': module_styles,
'inline_styles': inline_styles,
'includes': includes,
'classes': classes,
}
return page, slug
@staticmethod
def get_css_classes(source):
# For each classes field, it splits along spaces to get each one separately.
# It then uses itertools.chain() to effectively flatten this 'list of lists'.
# We then explicitly remove any blank entries, and then cast to list.
classes_lists = [classes[1].split(' ') for classes in REGEX_CLASSES.finditer(source)]
return list(filter(None, itertools.chain(*classes_lists)))
async def retry(self, coro):
# Retry loop
for _ in range(CROM_RETRIES):
try:
return await coro()
except (KeyboardInterrupt, GeneratorExit, SystemExit, CancelledError):
self.close()
sys.exit(1)
except:
print("Error fetching pages from Crom:")
print(traceback.format_exc())
print()
print("Making another attempt...")
print("Giving up...")
async def fetch_all(self):
has_next_page = True
last_slug = Container()
async with aiohttp.ClientSession() as session:
async def pull_pages():
created_at = format_date(self.last_created_at)
print(f"+ Requesting next batch of pages (last page '{last_slug}', created {created_at})")
# Make request
edges, has_next_page = await self.next_pages(session)
# Parse out results
for edge in edges:
page, slug = self.process_edge(edge)
last_slug.set(slug)
if page is not None:
self.last_created_at = page['created_at']
self.write_page(page)
return has_next_page
while has_next_page:
has_next_page = await self.retry(pull_pages)
print("Hit the end, finished!")
if __name__ == '__main__':
config = Configuration()
crawler = Crawler(config)
asyncio.run(crawler.fetch_all())