check.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import json
  2. import os
  3. import ipaddress
  4. from urllib.parse import urlparse
  5. import pythoncom
  6. import win32com
  7. from config import base_path, file_type_map
  8. from tools.oss_client import MinioClient
  9. REPLY = {'html': None, 'cookies': None, 'token': None}
  10. def extract_domain_or_ip(url):
  11. """提取网页中的域名或 IP+端口"""
  12. try:
  13. parsed_url = urlparse(url)
  14. host = parsed_url.hostname # 提取主机部分(域名或 IP)
  15. port = parsed_url.port # 提取端口
  16. result = host
  17. # 如果是 IP 地址,则保留端口
  18. if is_ip_address(host):
  19. result = f"u{host}:{port}" if port else 'u' + host
  20. return result.replace('.', '_').replace(':', '_') # 仅返回域名
  21. except Exception as e:
  22. print(f"解析错误: {e}")
  23. return None
  24. def is_ip_address(host):
  25. """判断是否是 IP 地址"""
  26. try:
  27. ipaddress.ip_address(host)
  28. return True
  29. except ValueError:
  30. return False
  31. def detect_oss_file_changes(oss_client, file_info):
  32. cloud_file_md5 = oss_client.get_cloud_file_md5(file_info['bucket_name'], file_info['file_path'])
  33. with open(os.path.join(base_path, file_info['file_name'] + '.metadata'), 'r', encoding='utf-8') as f:
  34. metadata = json.load(f)
  35. return cloud_file_md5 == metadata['md5']
  36. def detect_local_file_changes(file_path):
  37. with open(os.path.join(base_path, file_path + '.metadata'), 'r', encoding='utf-8') as f:
  38. metadata = json.load(f)
  39. current_file_md5 = MinioClient.get_file_md5(file_path)
  40. return metadata['md5'] == current_file_md5
  41. def is_file_open_in_wps(file_path: str) -> bool:
  42. abs_path = os.path.abspath(file_path).lower()
  43. suffix = file_path.split('.')[-1]
  44. try:
  45. pythoncom.CoInitialize()
  46. app = win32com.client.GetActiveObject(file_type_map[suffix][0])
  47. for wb in getattr(app, file_type_map[suffix][1]):
  48. if wb.FullName.lower() == abs_path:
  49. return True
  50. except Exception:
  51. return False
  52. finally:
  53. pythoncom.CoUninitialize()
  54. return False