-
Notifications
You must be signed in to change notification settings - Fork 2
/
json_data.py
61 lines (50 loc) · 1.81 KB
/
json_data.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
#!/usr/local/opt/python@3.10/bin/python3
# -*- coding: utf-8 -*-
import json
import requests
import sys
"""
Getting a existing JSON file with all HSK words as a base
and adding the information about strokes and radicals
"""
def main():
try:
base_response = requests.get(
"https://raw.githubusercontent.com/gigacool/hanyu-shuiping-kaoshi/master/hsk.json"
).json()
except requests.RequestException:
sys.exit("Can not fetch base json")
else:
with open("base.json", "w", encoding="utf-8") as json_file:
json.dump(
base_response, json_file, indent=4, sort_keys=True, ensure_ascii=False
)
try:
strokes_response = requests.get(
"https://raw.githubusercontent.com/pwxcoo/chinese-xinhua/master/data/word.json"
).json()
except requests.RequestException:
sys.exit("Can not fetch json with strokes and radicals")
else:
with open("strokes.json", "w", encoding="utf-8") as json_file:
json.dump(
strokes_response,
json_file,
indent=4,
sort_keys=True,
ensure_ascii=False,
)
with open("./strokes.json", "r") as sf:
strokes_data = json.load(sf)
with open("./base.json", "r") as bf:
base_data = json.load(bf)
for b_obj in base_data:
for s_obj in strokes_data:
if b_obj["hanzi"] == s_obj["word"]:
b_obj["strokes"] = s_obj["strokes"]
b_obj["radicals"] = s_obj["radicals"]
b_obj["translations"] = {"eng": b_obj["translations"]}
with open("hsk_json.json", "w", encoding="utf-8") as json_file:
json.dump(base_data, json_file, indent=4, sort_keys=True, ensure_ascii=False)
if __name__ == "__main__":
main()