botloader.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. """
  4. @Time : 2024/10/15 14:06
  5. @Author : cao
  6. @File : botloader.py
  7. @Desc :
  8. """
  9. import collections
  10. import os
  11. from typing import List,Text,Dict,Any
  12. import json
  13. from config import get_logger
  14. logger = get_logger()
  15. class Speech(object):
  16. @staticmethod
  17. def transform(content: List, keyword: Text) -> Dict[Any, Any]:
  18. result = collections.OrderedDict()
  19. for i, element in enumerate(content):
  20. code = element[keyword]
  21. result[str(code)] = element
  22. return result
  23. @staticmethod
  24. def get_json_content_from_file(file_path=None):
  25. def read_file(file_dir=None):
  26. with open(file_dir, 'r', encoding='utf-8') as fr:
  27. content = json.load(fr)
  28. return content
  29. res = []
  30. file_lst = os.listdir(file_path)
  31. for files in file_lst:
  32. old_dir = os.path.join(file_path, files)
  33. if os.path.isdir(old_dir):
  34. continue
  35. filename = os.path.splitext(files)
  36. if '.json' in filename:
  37. res.append(read_file(old_dir))
  38. return res
  39. @classmethod
  40. def get_speech(cls, file_path=None):
  41. content = cls.get_json_content_from_file(file_path)
  42. res = {}
  43. for cell in content:
  44. res[cell['botId']] = cls.transform(cell['questions'], 'id')
  45. botid, nums = cell['botId'], len(res[cell['botId']].keys())
  46. logger.info(f"botid:{botid} has {nums} records")
  47. return res
  48. if __name__ == "__main__":
  49. Speech().get_speech("./data")