init_load_source.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import asyncio
  2. import json
  3. import time
  4. from settings import HLM_HOST
  5. import requests
  6. import os
  7. from utils.utils_func import get_md5,get_modified_time,compare_two_times,download_file
  8. class init_load_source:
  9. def __init__(self):
  10. pass
  11. async def load_source(self):
  12. # asyncio.sleep()
  13. # await self.down_resouce()
  14. for i in range(100):
  15. print(1)
  16. await asyncio.sleep(1)
  17. async def down_resouce(self):
  18. response_data = self.get_update_file()
  19. if response_data:
  20. for relative_file_path, value in response_data.items():
  21. file_path = "{}\{}".format(os.getcwd(), relative_file_path)
  22. if os.path.exists(file_path):
  23. file_md5 = get_md5(file_path)
  24. if file_md5 != value["file_md5"]:
  25. # print(file_path, "md5 不同")
  26. file_modified_time = get_modified_time(file_path)
  27. text = "md5 不同 开始下载:{}".format(file_path)
  28. print(text)
  29. download_file(url=value["url"], file_path=file_path)
  30. else:
  31. # print(file_path, "md5 相同")
  32. pass
  33. else:
  34. text = "文件不存在 开始下载:{}".format(file_path)
  35. print(text)
  36. time.sleep(1)
  37. download_file(url=value["url"], file_path=file_path)
  38. else:
  39. print("获取更新文件内容失败")
  40. def get_update_file(self, type="client_camera", plugins_name="plugins_A"):
  41. """
  42. 根据类型和插件名称获取更新文件内容。
  43. 调用外部API查询客户端插件信息,并尝试下载指定名称的插件文件内容。
  44. 参数:
  45. - type: 插件类型,默认为"client_camera"。
  46. - plugins_name: 插件名称,默认为"plugins_A"。
  47. 返回:
  48. - 插件文件内容字符串,如果获取成功。
  49. - None,如果获取失败或出现错误。
  50. """
  51. url = HLM_HOST + "/api/openai/query_client_addons"
  52. params = {"type": type}
  53. try:
  54. response = requests.get(url, params=params, timeout=10)
  55. response.raise_for_status()
  56. raw_data = response.json()
  57. # 检查 raw_data 和 raw_data["data"] 是否存在
  58. if not raw_data or not raw_data.get("data"):
  59. return None
  60. # 使用 next 函数简化查找逻辑
  61. item = next((item for item in raw_data["data"]["list"] if item["name"] == plugins_name), None)
  62. if item:
  63. url = item["url"]
  64. response = requests.get(url, timeout=10)
  65. response.raise_for_status()
  66. return json.loads(response.text)
  67. return None
  68. except requests.RequestException as e:
  69. print(f"An error occurred: {e}")
  70. return None