This repository has been archived by the owner on Aug 2, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
import-galkwi-django.py
96 lines (78 loc) · 2.97 KB
/
import-galkwi-django.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
import json
import sys
import bot
import datetime
import tzlocal
def build_records_from_json(filename, licensed):
records = []
jsonobj = json.load(open(filename))
for e in jsonobj['entries']:
e['license'] = licensed
return jsonobj['entries']
def make_title(entryid, word):
title = '%s (갈퀴 Django %d)' % (word, entryid)
return title
def format_record(rec, datetimestr):
assert('id' in rec)
assert('word' in rec)
assert('pos' in rec)
title = make_title(rec['id'], rec['word'])
lines = []
lines += ['{{#set:사전:원본 라이선스=%s}}' % rec['license']]
lines += ['{{#set:사전:원본=갈퀴 Django}}']
lines += ['{{#set:']
lines += ['갈퀴 Django:가져온 시각=%s' % datetimestr]
for k in sorted(rec.keys()):
knames = {'contributors': '기여자',
'description': '설명',
'etym': '어원',
'id': '항목ID',
'license': '라이선스',
'orig': '본딧말',
'pos': '품사',
'props': '속성',
'stem': '어근',
'word': '표제어'}
kname = knames[k]
v = rec[k]
if type(v) == int:
lines += ['|갈퀴 Django:%s=%d' % (kname, v)]
elif type(v) == str:
lines += ['|갈퀴 Django:%s=%s' % (kname, v)]
elif type(v) == list:
for subv in v:
lines += ['|갈퀴 Django:%s=%s' % (kname, subv)]
else:
raise Exception('No idea how to format ' + k)
lines += ['}}']
content = '\n'.join(lines)
return title, content
def import_page(bot, title, content):
#resp = bot.get(action='query', prop='revisions', titles=title, rvprop='content')
#rev = list(resp['query']['pages'].keys())[0]
content = '{{사전 항목}}\n' + content
token = bot.get_edit_token();
resp = bot.post(action='edit', title=title, text=content, token=token)
assert(resp['edit']['result'] == 'Success')
if __name__ == '__main__':
if len(sys.argv) != 3:
sys.stderr.write('Usage: %s <ccby.json> <mplgpllgpl.json>\n' % sys.argv[0])
sys.exit(1)
filename1 = sys.argv[1]
filename2 = sys.argv[2]
records = build_records_from_json(filename1, 'CC BY 4.0')
records += build_records_from_json(filename2, 'MPL 1.1/GPL 2.0/LGPL 2.1')
records.sort(key=lambda x: x['word'])
botsession = bot.Bot()
botsession.login()
magic = 'IMPORT 갈퀴 Django'
new_page_prefix = '{{사전 항목}}'
tz = tzlocal.get_localzone()
dt = tz.localize(datetime.datetime.now())
datetimestr = dt.isoformat()
datetimestr = '2017-10-20T21:46:15.309170+09:00'
for i, record in zip(range(0, len(records)), records):
if i % 100 == 0:
print('Progress: %d/%d' % (i, len(records)))
title, content = format_record(record, datetimestr)
botsession.insert_text(title, magic, content, new_page_prefix, False)