This repository has been archived by the owner on Jan 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
converter_util.py
79 lines (69 loc) · 2.49 KB
/
converter_util.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
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import json
import hashlib
payload = {}
def converte (data):
for word in data['2']:
c_word = {
'title': _first(word['0']),
'subtitle': ''.join(word['5']) + '\t' + '%s %d-%d %s' % (_first(word['1']),
word['3'], word['2'], _first(word['4'])),
'audio': _first(word['6']),
'type': {}
}
c_word['uid'] = _hash_json(c_word)
payload[c_word['uid']] = c_word
for btype in word['9']:
for explain in btype['1']:
c_type = {
'title': _first(btype['0']),
'subtitle': _first(explain['0']),
'example': {}
}
c_type['uid'] = _hash_json(c_type)
c_word['type'][c_type['uid']] = c_type
for g_exp in explain['1']:
for exp in g_exp:
c_exp = {
'audio': ''
}
for index in range(0, len(exp)):
if index == 0:
c_exp['title'] = exp[index]
if index == 1:
c_exp['subtitle'] = exp[index]
c_exp['uid'] = _hash_json(c_exp)
c_type['example'][c_exp['uid']] = c_exp
for exp in explain['2']:
c_exp = {
'audio': ''
}
for index in range(0, len(exp)):
if index == 0:
c_exp['title'] = exp[index]
if index == 1:
c_exp['subtitle'] = exp[index]
if index == 2:
c_exp['audio'] = exp[index]
c_exp['uid'] = _hash_json(c_exp)
c_type['example'][c_exp['uid']] = c_exp
if not payload.keys():
raise Exception
return payload
def _stringify_json (json_data, decode = False):
tmp = json.dumps(json_data)
if decode:
tmp = tmp.decode('unicode-escape')
return tmp
def _hash_json (json_data):
tmp = _stringify_json(json_data)
return hashlib.md5(tmp).hexdigest()
def _first (list):
if len(list) > 0:
return list[0]
return ''
def _big_type_get_title (big_type):
_print_json(big_type)
def _print_json (json_data):
print _stringify_json(json_data, True)