forked from Glaceon31/LLhelper
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathupdatemetadata.py
128 lines (101 loc) · 4.29 KB
/
updatemetadata.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
# -*- coding: utf-8 -*-
import argparse
import sqlite3
import sys
from lib.JsonUtil import JsonFile
from lib.Translator import Translator
if sys.version[0] == '2':
reload(sys)
sys.setdefaultencoding('utf-8')
translate_cn_file = 'translate-cn.properties'
translate_missing_file = 'translate-missing.properties'
jpdbpath = 'unitnewjp.db_'
def translateCn(cur_data, key, translator):
cur_data[key] = cur_data[key].replace('<br>', ' ')
jp_text = cur_data[key]
cn_text = translator.translate(jp_text)
if jp_text and cn_text:
cur_data['cn' + key] = cn_text
def commonHandleMetadataArray(dbconn, metadata, json_key, main_query):
if json_key not in metadata:
count_before = 0
else:
count_before = len(metadata[json_key])
new_arr = []
query = dbconn.execute(main_query)
row = query.fetchone()
while row:
cur_id = row[0]
new_arr.append(cur_id)
row = query.fetchone()
metadata[json_key] = new_arr
print('%s : before %d, after %d' % (json_key, count_before, len(metadata[json_key])))
def commonHandleMetadataTable(dbconn, metadata, json_key, main_query, row_handler, translator):
count_new = 0
count_row = 0
if json_key not in metadata:
metadata[json_key] = {}
mdata = metadata[json_key]
count_before = len(mdata)
query = dbconn.execute(main_query)
row = query.fetchone()
while row:
count_row += 1
cur_id = row[0]
cur_name = row[1]
if str(cur_id) not in mdata:
print('New %s %d : %s' % (json_key, cur_id, cur_name))
count_new += 1
mdata[str(cur_id)] = {}
cur_data = mdata[str(cur_id)]
row_handler(dbconn, cur_data, row, translator)
row = query.fetchone()
print('%s : before %d, after %d, db %d, new %d' % (json_key, count_before, len(mdata), count_row, count_new))
def handleAlbum(dbconn, cur_data, row, translator):
cur_data['name'] = row[1]
translateCn(cur_data, 'name', translator)
def handleMemberTag(dbconn, cur_data, row, translator):
cur_data['name'] = row[1]
translateCn(cur_data, 'name', translator)
unit_arr = []
rel_query = dbconn.execute('SELECT unit_type_id from unit_type_member_tag_m WHERE member_tag_id = %d;' % row[0])
rel_row = rel_query.fetchone()
while rel_row:
unit_arr.append(rel_row[0])
rel_row = rel_query.fetchone()
cur_data['members'] = unit_arr
def handleUnitType(dbconn, cur_data, row, translator):
cur_data['name'] = row[1]
translateCn(cur_data, 'name', translator)
if row[2]:
cur_data['color'] = row[2]
if row[3]:
cur_data['background_color'] = row[3]
def handleLevelLimit(dbconn, cur_data, row, translator):
cur_data[str(row[1])] = -row[2]
def parseArgs():
parser = argparse.ArgumentParser('Update metadata')
parser.add_argument('-d', action='store_true', help='dump strings that need translate', dest='dump_missing')
return parser.parse_args()
if __name__ == "__main__":
args = parseArgs()
json_file = JsonFile('metadata.txt')
print('Updating metadata json: %s ...' % json_file)
metadata = json_file.load()
translator = Translator(translate_cn_file)
jpdbconn = sqlite3.connect(jpdbpath)
# album
commonHandleMetadataTable(jpdbconn, metadata, 'album', 'SELECT album_series_id, name FROM album_series_m;', handleAlbum, translator)
# member tag
commonHandleMetadataTable(jpdbconn, metadata, 'member_tag', 'SELECT member_tag_id, name FROM member_tag_m;', handleMemberTag, translator)
# unit type
commonHandleMetadataTable(jpdbconn, metadata, 'unit_type', 'SELECT unit_type_id, name, original_attribute_id, background_color FROM unit_type_m;', handleUnitType, translator)
# level limit
commonHandleMetadataTable(jpdbconn, metadata, 'level_limit', 'SELECT unit_level_limit_id, unit_level, smile_diff FROM unit_level_limit_pattern_m;', handleLevelLimit, translator)
# cskill types
commonHandleMetadataArray(jpdbconn, metadata, 'cskill_groups', 'SELECT member_tag_id FROM unit_leader_skill_extra_m GROUP BY member_tag_id;')
json_file.save(metadata)
print('Updated %s' % json_file)
if args.dump_missing:
translator.exportMissing(translate_missing_file)
print('Exported missing translator text to %s' % translate_missing_file)