init_load_source.py 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import asyncio
  2. import json
  3. import time
  4. from settings import HLM_HOST
  5. import aiohttp
  6. import os
  7. from utils.utils_func import get_md5, get_modified_time, compare_two_times, check_path
  8. from utils.common import message_queue
  9. class init_load_source:
  10. async def load_source(self):
  11. await self.down_resouce()
  12. async def down_resouce(self):
  13. response_data = await self.get_update_file()
  14. if response_data:
  15. for relative_file_path, value in response_data['data'].items():
  16. file_path = f"{os.getcwd()}/{relative_file_path}"
  17. if os.path.exists(file_path):
  18. file_md5 = get_md5(file_path)
  19. if file_md5 != value["file_md5"]:
  20. file_modified_time = get_modified_time(file_path)
  21. if compare_two_times(file_modified_time, response_data["update_time"]) == "left_new":
  22. continue
  23. else:
  24. print(f"md5 不同 开始下载:{file_path}")
  25. await self.async_download_file(value["url"], file_path)
  26. else:
  27. pass
  28. else:
  29. print(f"文件不存在 开始下载:{file_path}")
  30. await self.send_message(f"开始下载:{file_path}")
  31. await self.async_download_file(value["url"], file_path)
  32. else:
  33. print("获取更新文件内容失败")
  34. async def send_message(self, msg):
  35. message = {
  36. 'code': 0,
  37. 'msg_code': 'down_source',
  38. 'message': msg
  39. }
  40. await message_queue.put(json.dumps(message))
  41. async def get_update_file(self, type="client_camera", plugins_name="plugins_A"):
  42. """异步获取指定类型的插件文件更新信息
  43. Args:
  44. type (str, optional): 设备类型标识,默认为"client_camera"
  45. plugins_name (str, optional): 需要查询的插件名称,默认为"plugins_A"
  46. Returns:
  47. dict or None: 成功时返回插件文件的JSON数据,失败或未找到时返回None
  48. """
  49. url = HLM_HOST + "/api/openai/query_client_addons"
  50. params = {"type": type}
  51. try:
  52. async with aiohttp.ClientSession() as session:
  53. # 发送初始请求获取插件列表数据
  54. async with session.get(url, params=params, timeout=10) as response:
  55. response.raise_for_status()
  56. raw_data = await response.json()
  57. if not raw_data or not raw_data.get("data"):
  58. return None
  59. # 从插件列表中查找匹配的插件项
  60. item = next(
  61. (item for item in raw_data["data"]["list"] if item["name"] == plugins_name),
  62. None
  63. )
  64. if item:
  65. # 根据插件项中的URL获取具体文件内容
  66. url = item["url"]
  67. async with session.get(url, timeout=10) as item_response:
  68. item_response.raise_for_status()
  69. return await item_response.json()
  70. return None
  71. except Exception as e:
  72. print(f"An error occurred: {e}")
  73. return None
  74. async def async_download_file(self, url: str, file_path: str):
  75. """异步下载文件"""
  76. async with aiohttp.ClientSession() as session:
  77. async with session.get(url) as response:
  78. root_path, file_name = os.path.split(file_path)
  79. check_path(root_path)
  80. with open(file_path, 'wb') as f:
  81. async for chunk in response.content.iter_chunked(1024):
  82. f.write(chunk)