utils_func.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import os
  2. from hashlib import sha256, md5
  3. from datetime import datetime
  4. import requests
  5. from natsort import ns, natsorted
  6. def get_md5(file_path):
  7. data_md5 = None
  8. if os.path.isfile(file_path):
  9. f = open(file_path, 'rb')
  10. md5_obj = md5()
  11. md5_obj.update(f.read())
  12. hash_code = md5_obj.hexdigest()
  13. f.close()
  14. data_md5 = str(hash_code).lower()
  15. return data_md5
  16. def get_modified_time(file_path):
  17. # 获取文件最后修改的时间戳
  18. timestamp = os.path.getmtime(file_path)
  19. # 将时间戳转换为datetime对象,并格式化为指定格式的字符串
  20. modified_time = datetime.fromtimestamp(timestamp).strftime('%Y-%m-%d %H:%M:%S')
  21. return modified_time
  22. def compare_two_times(time_str1, time_str2):
  23. # 定义时间格式
  24. time_format = "%Y-%m-%d %H:%M:%S"
  25. # 将时间字符串解析为datetime对象
  26. time1 = datetime.strptime(time_str1, time_format)
  27. time2 = datetime.strptime(time_str2, time_format)
  28. # 比较两个时间
  29. if time1 > time2:
  30. # print(f"{time_str1} 比 {time_str2} 新。")
  31. return "left_new"
  32. elif time1 < time2:
  33. # print(f"{time_str2} 比 {time_str1} 新。")
  34. return "right_new"
  35. else:
  36. # print("两个时间相等。")
  37. return "is_same"
  38. async def download_file(url, file_path):
  39. try:
  40. root_path, file_name = os.path.split(file_path)
  41. check_path(root_path)
  42. response = requests.get(url)
  43. _content = response.content
  44. with open(file_path, 'wb') as f:
  45. f.write(_content)
  46. print("下载成功:{}".format(file_path))
  47. except:
  48. print("下载失败:{}".format(file_path))
  49. def check_path(_path):
  50. if not os.path.exists(_path):
  51. # 创建多级目录
  52. os.makedirs(_path, exist_ok=True)
  53. # os.mkdir(_path)
  54. return True
  55. # from module.view_control.MineQWidget import MineQWidget
  56. def list_dir(path):
  57. listdir = os.listdir(path)
  58. return natsorted(listdir, alg=ns.PATH)
  59. def get_folder(path):
  60. folder_list = []
  61. for _file in list_dir(path):
  62. file_path = "{}/{}".format(path, _file)
  63. if os.path.isdir(file_path):
  64. folder_list.append(
  65. {
  66. "folder_path": file_path,
  67. "folder_name": _file,
  68. "root_path": path,
  69. "label": "待处理", # 是否需要继续处理
  70. }
  71. )
  72. return folder_list
  73. def get_images(path):
  74. _Type = [".png", ".PNG", ".jpg", ".JPG", ".gif", ".GIF", ".jpge", ".JPGE", ".CR2"]
  75. image_list = [] # 过滤非图片数据
  76. for _file in list_dir(path):
  77. file_name, e = os.path.splitext(_file)
  78. file_path = "{}/{}".format(path, _file)
  79. if os.path.isdir(file_path):
  80. continue
  81. if e in _Type and "mask" not in file_name:
  82. image_list.append(
  83. {
  84. "file_path": file_path,
  85. "file_name": file_name,
  86. "file": _file,
  87. "root_path": path,
  88. "e": e,
  89. }
  90. )
  91. return image_list
  92. def httpGetHandler(url, params=None, headers=None):
  93. """
  94. 发送GET请求
  95. :param endpoint: 请求的端点
  96. :param params: 查询参数
  97. :param headers: 请求头
  98. :return: 响应对象
  99. """
  100. try:
  101. response = requests.get(url, params=params, headers=headers)
  102. response.raise_for_status() # 如果响应状态码不是200,抛出异常
  103. return response
  104. except requests.exceptions.RequestException as e:
  105. print(f"GET请求失败: {e}")
  106. return None
  107. def httpPosthandler(url, data=None, json=None, headers=None):
  108. """
  109. 发送POST请求
  110. :param endpoint: 请求的端点
  111. :param data: 表单数据
  112. :param json: JSON数据
  113. :param headers: 请求头
  114. :return: 响应对象
  115. """
  116. try:
  117. response = requests.post(url, data=data, json=json, headers=headers)
  118. response.raise_for_status() # 如果响应状态码不是200,抛出异常
  119. return response
  120. except requests.exceptions.RequestException as e:
  121. print(f"POST请求失败: {e}")
  122. return None