-
Notifications
You must be signed in to change notification settings - Fork 0
/
dumps.py
54 lines (41 loc) · 1.27 KB
/
dumps.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
from pathlib import Path
import re
import bz2
import xml.sax
DUMP_DIR = Path('/public/dumps/public/enwiki/latest')
class Handler(xml.sax.handler.ContentHandler):
def __init__(self):
super().__init__()
self.in_title = False
self.in_text = False
self.title = None
def startElement(self, name, attrs):
if name == 'title':
self.in_title = True
elif name == 'text':
self.in_text = True
def endElement(self, name):
if name == 'title':
self.in_title = False
elif name == 'text':
self.in_text = False
def characters(self, content):
if self.in_title:
self.title = content
print()
print('------------------------------------')
print(self.title)
if self.in_text:
print(content, end='')
def main():
paths = []
for path in DUMP_DIR.glob('enwiki-latest-pages-articles[123456789]*.xml-p*bz2'):
m = re.match(r'.*xml-p([0-9]*)p([0-9]*)', path.name)
key = int(m.group(1))
paths.append((key, path))
paths.sort()
key, path = paths[0]
handler = Handler()
xml.sax.parse(bz2.open(path), handler)
if __name__ == '__main__':
main()