#!/usr/bin/env python # -*- coding: utf-8 -*- """ @Time : 2024/10/15 14:06 @Author : cao @File : botloader.py @Desc : """ import collections import os from typing import List,Text,Dict,Any import json from config import get_logger logger = get_logger() class Speech(object): @staticmethod def transform(content: List, keyword: Text) -> Dict[Any, Any]: result = collections.OrderedDict() for i, element in enumerate(content): code = element[keyword] result[str(code)] = element return result @staticmethod def get_json_content_from_file(file_path=None): def read_file(file_dir=None): with open(file_dir, 'r', encoding='utf-8') as fr: content = json.load(fr) return content res = [] file_lst = os.listdir(file_path) for files in file_lst: old_dir = os.path.join(file_path, files) if os.path.isdir(old_dir): continue filename = os.path.splitext(files) if '.json' in filename: res.append(read_file(old_dir)) return res @classmethod def get_speech(cls, file_path=None): content = cls.get_json_content_from_file(file_path) res = {} for cell in content: res[cell['botId']] = cls.transform(cell['questions'], 'id') botid, nums = cell['botId'], len(res[cell['botId']].keys()) logger.info(f"botid:{botid} has {nums} records") return res if __name__ == "__main__": Speech().get_speech("./data")