-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwiki_to_json.py
87 lines (72 loc) · 3.18 KB
/
wiki_to_json.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
#!/usr/bin/python3.4
#
# wiki_to_json.py - Wiki to json exporter to for COBHUNI project
#
# Copyright (C) 2016 Alicia González Martínez, [email protected]
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
######################################################################################
#
# dependencies:
# * exporthandler.py
# * wikiparser.py
# * offsetsbuilder.py
# * util.py
# * config.ini
# +------------------+ +---------------+
# +-----------------+ | | -----> | |
# | | -------> | exporthandler.py | str | wikiparser.py |
# | | <------- | | <----- | |
# | | json +------------------+ json +---------------+
# | wiki_to_json.py |
# | | +-------------------+
# | | -------> | |
# | | json | offsetsbuilder.py | ----> json output files
# +-----------------+ | |
# +-------------------+
# usage:
# $ python wiki_to_json.py --title Example_for_DjVu_manual_cz-book_color.djvu
#
#######################################################################################
import os
import sys
import json
import argparse
from configparser import ConfigParser
CURRENT_PATH = os.path.dirname(os.path.realpath(__file__))
# append current directory to sys.path
sys.path.insert(0, CURRENT_PATH)
from exporthandler.exporthandler import ExportHandler
from offsetsbuilder.offsetsbuilder import OffsetsBuilder
config = ConfigParser(inline_comment_prefixes=('#'))
config.read(os.path.join(CURRENT_PATH, 'config.ini'))
OUTPATH = config.get('json output', 'path')
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Download scan transliterations from the wiki and and dumps them into json.')
parser.add_argument('--title', help='download only the texts belonging to this title')
args = parser.parse_args()
handler = ExportHandler()
try:
data = handler.export(args.title) # calls WikiParser
except Exception as e:
print('Fatal error in ExportHandler: %s' % e, file=sys.stderr)
sys.exit(1)
#print(data) #DEBUG
for scan in json.loads(data):
fname, fext = os.path.splitext(scan['title'])
with open(os.path.join(OUTPATH, fname+'.json'), 'w') as fp:
offbr = OffsetsBuilder(json.dumps(scan['content']))
out = offbr.build()
fp.write(out)