check.py 2.1 KB

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