check.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import json
  2. import os
  3. import ipaddress
  4. from urllib.parse import urlparse
  5. from config import base_path, file_type_map
  6. from tools.oss_client import MinioClient
  7. REPLY = {'html': None, 'cookies': None, 'token': None}
  8. def extract_domain_or_ip(url):
  9. """提取网页中的域名或 IP+端口"""
  10. try:
  11. parsed_url = urlparse(url)
  12. host = parsed_url.hostname # 提取主机部分(域名或 IP)
  13. port = parsed_url.port # 提取端口
  14. result = host
  15. # 如果是 IP 地址,则保留端口
  16. if is_ip_address(host):
  17. result = f"u{host}:{port}" if port else 'u' + host
  18. return result.replace('.', '_').replace(':', '_') # 仅返回域名
  19. except Exception as e:
  20. print(f"解析错误: {e}")
  21. return None
  22. def is_ip_address(host):
  23. """判断是否是 IP 地址"""
  24. try:
  25. ipaddress.ip_address(host)
  26. return True
  27. except ValueError:
  28. return False
  29. def detect_oss_file_changes(oss_client, file_info):
  30. cloud_file_md5 = oss_client.get_cloud_file_md5(file_info['bucket_name'], file_info['file_path'])
  31. with open(os.path.join(base_path, file_info['file_name'] + '.metadata'), 'r', encoding='utf-8') as f:
  32. metadata = json.load(f)
  33. return cloud_file_md5 == metadata['md5']
  34. def detect_local_file_changes(file_path):
  35. with open(os.path.join(base_path, file_path + '.metadata'), 'r', encoding='utf-8') as f:
  36. metadata = json.load(f)
  37. current_file_md5 = MinioClient.get_file_md5(file_path)
  38. return metadata['md5'] == current_file_md5