-
Notifications
You must be signed in to change notification settings - Fork 0
/
dl_wiki.py
177 lines (127 loc) · 4.42 KB
/
dl_wiki.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
import asyncio
from datetime import datetime
import json
import os
import sys
import aiohttp
async def get_page_list(session, url, ns):
# get the page list
pages = []
params = {
'action': 'query',
'format': 'json',
'list': 'allpages',
'apnamespace': ns,
'aplimit': 500
}
lastContinue = {}
while True:
p = {**params}
p.update(lastContinue)
async with session.get(url, params=p) as response:
result = await response.json()
if 'query' in result:
pages += result['query']['allpages']
print('.', end='', file=sys.stderr)
if 'continue' not in result:
break
lastContinue = result['continue']
return pages
async def get_time(session, url, title, direction, times):
params = {
'action': 'query',
'format': 'json',
'titles': title,
'prop': 'revisions',
'rvlimit': '1',
'rvprop': 'timestamp',
'rvdir': direction
}
async with session.get(url, params=params) as response:
r = await response.json()
for k, v in r['query']['pages'].items():
try:
times[title] = datetime.fromisoformat(v['revisions'][0]['timestamp']).isoformat()
print('.', end='', file=sys.stderr)
except KeyError:
print(v, file=sys.stderr)
break
async def get_times(session, url, direction, pages):
times = {}
async with asyncio.TaskGroup() as tg:
for i, p in enumerate(pages):
tg.create_task(get_time(session, url, p['title'], direction, times))
return times
async def get_ctimes(session, url, pages):
return await get_times(session, url, 'newer', pages)
async def get_mtimes(session, url, pages):
return await get_times(session, url, 'older', pages)
async def get_wikitext(session, url, title, outpath):
params = {
'page': title,
'prop': 'wikitext',
'action': 'parse',
'format': 'json'
}
async with session.get(url, params=params) as response:
page = await response.json()
with open(outpath, 'w') as f:
print(json.dumps(page), file=f)
print(title, file=sys.stderr)
async def get_all_wikitext(session, url, pages):
async with asyncio.TaskGroup() as tg:
for i, p in enumerate(pages):
tg.create_task(get_wikitext(session, url, p['title'], f"data/wikitext/{i}"))
async def get_file_meta(session, url, title, file_meta):
params = {
'action': 'query',
'format': 'json',
'titles': title,
'prop': 'imageinfo',
'iiprop': 'url|user'
}
async with session.get(url, params=params) as response:
r = await response.json()
for k, v in r['query']['pages'].items():
try:
file_meta[title] = {
'url': v['imageinfo'][0]['url'],
'user': v['imageinfo'][0]['user']
}
print('.', end='', file=sys.stderr)
except KeyError:
print(v, file=sys.stderr)
break
async def get_all_file_meta(session, url, files):
file_meta = {}
async with asyncio.TaskGroup() as tg:
for i, p in enumerate(files):
tg.create_task(get_file_meta(session, url, p['title'], file_meta))
return file_meta
async def run():
os.makedirs('data', exist_ok=True)
url = 'https://vassalengine.org/w/api.php'
conn = aiohttp.TCPConnector(limit=50)
async with aiohttp.ClientSession(connector=conn) as session:
#
# files
#
files = await get_page_list(session, url, 6)
file_meta = await get_all_file_meta(session, url, files)
with open('data/files.json', 'w') as f:
json.dump(file_meta, f, indent=2)
# the mtime of a file is the ctime of the current version
file_ctimes = await get_mtimes(session, url, files)
with open('data/file_ctimes.json', 'w') as f:
json.dump(file_ctimes, f, indent=2)
#
# pages
#
pages = await get_page_list(session, url, 100)
page_ctimes = await get_ctimes(session, url, pages)
with open('data/page_ctimes.json', 'w') as f:
json.dump(page_ctimes, f, indent=2)
os.mkdir('data/wikitext')
await get_all_wikitext(session, url, pages)
if __name__ == '__main__':
asyncio.run(run())