osc/research/nuofang-db/nfm/mstruct/parse_rsp_json.py
2025-05-28 19:16:17 +08:00

108 lines
3.4 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import hashlib
import json
import os
import pymysql
key_set = {}
db = pymysql.connect(host='39.101.194.63', port=23306,
user='root', passwd='passok123A', db='nfm', charset='utf8mb4')
cursor = db.cursor()
root_node_id = ''
def md5(content=None):
if content is None:
return ''
md5gen = hashlib.md5()
md5gen.update(content.encode())
md5code = md5gen.hexdigest()
return md5code
def get_data_item(data):
if data is not None and len(data) > 0:
for sub_data in data:
save_data_to_db(sub_data)
if 'ChildNodes' in sub_data:
child_data = sub_data['ChildNodes']
get_data_item(child_data)
def save_data_to_db(data):
global root_node_id
col_list = []
val_list = []
for key in data:
if key != "ChildNodes":
col_list.append(key)
val = data[key]
if type(val) == str:
val = val.replace("'", "")
val = "'%s'" % val
elif type(val) == bool:
if val == True:
val = '1'
else:
val = '0'
elif type(val) == int:
val = str(val)
elif type(val) == dict:
val = json.dumps(val, ensure_ascii=False)
val = "'%s'" % val
if key == 'id':
if val == "'0'" or val == "'-1'":
val = "'0000" + md5(data['title'])[:6] + "'"
root_node_id = val
if val == "'-2'":
val = "'ffff" + md5(data['value'])[:6] + "'"
data['identifier_parent'] = data['value'].replace("_sub", "")
if key == 'identifier_parent' and len(val) == 2:
val = root_node_id
val_list.append(val)
cols = ', '.join(col_list)
vals = ', '.join(val_list)
sql_insert = 'INSERT INTO m_struct_content (%s) VALUES (%s)' % (cols, vals)
# sql_insert = 'INSERT INTO m_struct_tree (%s) VALUES (%s)' % (cols, vals)
print(sql_insert)
cursor.execute(sql_insert)
def get_key_set(array):
if array is not None and len(array) > 0:
for item in array:
for key in item:
if key != "ChildNodes":
l_key = 0
if type(item[key]) == dict:
l_key = len(json.dumps(item[key]))
if type(item[key]) == bool:
l_key = 1
if type(item[key]) == str:
l_key = len(item[key])
if type(item[key]) == int:
l_key = len(str(item[key]))
if key in key_set:
if key_set[key] < l_key:
key_set[key] = l_key
else:
key_set[key] = l_key
else:
get_key_set(item[key])
json_file_path = r"E:\yuxin\nuofang-data\structure\0420\content"
if __name__ == "__main__":
for file_name in os.listdir(json_file_path):
if file_name != 'content-usa.json':
c_file = open(json_file_path + '\\' + file_name, 'r', encoding='utf-8')
file_json = json.loads(c_file.read())
c_file.close()
# get_key_set(file_json['tree'])
root_node_id = ''
get_data_item(file_json['tree'])
# get_data_item(file_json)
db.commit()
# print(key_set)