download.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import os
  2. import platform
  3. import requests
  4. import gzip
  5. import shutil
  6. from zipfile import ZipFile
  7. import stat
  8. # 定义常量
  9. BASE_URL = 'https://github.com/MetaCubeX/mihomo/releases/download'
  10. VERSION = 'v1.18.8'
  11. ASSETS_DIR = './assets/bin'
  12. SYSTEM = platform.system().lower()
  13. ARCH = platform.machine()
  14. # 如果目录不存在,创建下载目录
  15. if not os.path.exists(ASSETS_DIR):
  16. os.makedirs(ASSETS_DIR)
  17. def get_download_url():
  18. """
  19. 根据系统和架构生成下载链接
  20. """
  21. if SYSTEM == 'darwin' and ARCH == 'x86_64':
  22. file_name = f'mihomo-darwin-amd64-{VERSION}.gz'
  23. elif SYSTEM == 'darwin' and ARCH == 'arm64':
  24. file_name = f'mihomo-darwin-arm64-{VERSION}.gz'
  25. elif SYSTEM == 'linux' and ARCH == 'x86_64':
  26. file_name = f'mihomo-linux-amd64-{VERSION}.gz'
  27. elif SYSTEM == 'linux' and ARCH == 'arm64':
  28. file_name = f'mihomo-linux-arm64-{VERSION}.gz'
  29. elif SYSTEM == 'windows' and ARCH == 'x86_64':
  30. file_name = f'mihomo-windows-amd64-{VERSION}.zip'
  31. else:
  32. raise ValueError(f"Unsupported system/architecture: {SYSTEM} {ARCH}")
  33. return f"{BASE_URL}/{VERSION}/{file_name}", file_name
  34. def download_file(url, output_path):
  35. """
  36. 下载文件
  37. """
  38. print(f"Downloading {url} ...")
  39. response = requests.get(url, stream=True)
  40. if response.status_code == 200:
  41. with open(output_path, 'wb') as file:
  42. shutil.copyfileobj(response.raw, file)
  43. print(f"Download complete: {output_path}")
  44. else:
  45. print(f"Failed to download {url}, status code: {response.status_code}")
  46. def extract_gz_file(file_path, output_path):
  47. """
  48. 解压 .gz 文件
  49. """
  50. print(f"Extracting {file_path} ...")
  51. with gzip.open(file_path, 'rb') as f_in:
  52. with open(output_path, 'wb') as f_out:
  53. shutil.copyfileobj(f_in, f_out)
  54. print(f"Extracted to {output_path}")
  55. def extract_zip_file(file_path, output_dir):
  56. """
  57. 解压 .zip 文件
  58. """
  59. print(f"Extracting {file_path} ...")
  60. with ZipFile(file_path, 'r') as zip_ref:
  61. zip_ref.extractall(output_dir)
  62. print(f"Extracted to {output_dir}")
  63. def add_executable_permission(file_path):
  64. """
  65. 添加可执行权限(适用于 macOS 和 Linux)
  66. """
  67. if SYSTEM != 'windows':
  68. print(f"Adding executable permission to {file_path}")
  69. st = os.stat(file_path)
  70. os.chmod(file_path, st.st_mode | stat.S_IEXEC)
  71. print(f"Executable permission added to {file_path}")
  72. def main():
  73. # 获取下载链接和文件名
  74. url, file_name = get_download_url()
  75. # 下载文件
  76. download_path = os.path.join(ASSETS_DIR, file_name)
  77. download_file(url, download_path)
  78. # 构建输出路径和重命名文件
  79. service_name = f'ccore-{SYSTEM}-{ARCH}'
  80. output_file = os.path.join(ASSETS_DIR, service_name)
  81. # 解压文件并重命名
  82. if file_name.endswith('.gz'):
  83. extract_gz_file(download_path, output_file)
  84. elif file_name.endswith('.zip'):
  85. extract_zip_file(download_path, ASSETS_DIR)
  86. # 添加可执行权限
  87. add_executable_permission(output_file)
  88. # 删除下载的压缩文件
  89. if os.path.exists(download_path):
  90. os.remove(download_path)
  91. print(f"Deleted temp file: {download_path}")
  92. if __name__ == "__main__":
  93. main()