赞
踩
功能1:将项目的所有者,名称,版本信息,本机保存的地址。保存到json文件中。
功能2:json中可收录多个项目的信息。点击即可更新多个项目。
功能3:将json文件中的版本信息与api.github中的最新版进行比较,若有更新的版本,就会下载最新的release,并同步更新json。功能4:指定下载的文件名称。
python、json和打包的exe,已压缩,可点击下载https://wooo1.lanzoul.com/iO0Ui1z3wvcf
以下是Python代码
- import os
- import sys
- import json
- import py7zr
- import zipfile
- import urllib3
- import logging
- import requests
-
-
- PROJECTS_JSON_FILE = "projects.json"
-
- # 禁用urllib3库产生的不安全请求警告
- urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
-
- # 配置日志输出格式和级别,并记录到文件和控制台
- log_file = "log.txt"
- logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s', filename=log_file)
- console_handler = logging.StreamHandler(sys.stdout)
- console_handler.setLevel(logging.INFO)
- logging.getLogger().addHandler(console_handler)
-
-
- def download_release_files(owner, repo, version, save_path, file=None):
- logging.info("开始下载发布文件...")
- # 获取最新发布版本的下载链接
- url = f"https://api.github.com/repos/{owner}/{repo}/releases/latest"
- response = requests.get(url, verify=False)
-
- if response.status_code == 200:
- release = response.json()
- latest_version = release['tag_name']
-
- logging.info(f"当前版本号 {version}")
- logging.info(f"最新版本号 {latest_version}")
-
- if version == latest_version:
- logging.info("当前版本已是最新版本,无需下载。")
- return True
-
- assets = release['assets']
- for asset in assets:
- file_url = asset['browser_download_url']
- file_name = asset['name']
-
- if file and file_name != file:
- continue
-
- file_save_path = os.path.join(save_path, file_name)
-
- # 下载文件时添加进度日志
- with requests.get(file_url, stream=True, verify=False) as r:
- try:
- r.raise_for_status()
- except requests.exceptions.HTTPError:
- logging.error(f"无法下载文件 {file_name}")
- continue
-
- logging.info(f"正在下载文件 {file_name}")
-
- with open(file_save_path, 'wb') as f:
- total_size = int(r.headers.get('content-length', 0))
- downloaded_size = 0
-
- for chunk in r.iter_content(chunk_size=8192):
- if chunk:
- f.write(chunk)
- downloaded_size += len(chunk)
- progress = int(downloaded_size / total_size * 100)
- logging.info(f"下载进度 {progress}%")
-
- logging.info(f"成功下载文件 {file_name}")
-
- # 检查文件是否是压缩文件并进行解压
- if file_name.endswith(".zip"):
- logging.info(f"开始解压文件 {file_name}")
- with zipfile.ZipFile(file_save_path, 'r') as zip_ref:
- zip_ref.extractall(save_path)
-
- # 解压成功后删除压缩文件
- if os.path.exists(file_save_path):
- os.remove(file_save_path)
- logging.info(f"成功删除压缩文件 {file_name}")
-
- elif file_name.endswith(".7z"):
- logging.info(f"开始解压文件 {file_name}")
- with py7zr.SevenZipFile(file_save_path, mode='r') as z:
- z.extractall(save_path)
-
- # 解压成功后删除压缩文件
- if os.path.exists(file_save_path):
- os.remove(file_save_path)
- logging.info(f"成功删除压缩文件 {file_name}")
-
- return True
- else:
- logging.error("无法获取发布信息。")
-
- return False
-
-
- def get_latest_version(owner, repo):
- logging.info("开始获取最新版本号...")
- # 获取最新发布版本号
- url = f"https://api.github.com/repos/{owner}/{repo}/releases/latest"
- response = requests.get(url, verify=False)
-
- if response.status_code == 200:
- release = response.json()
-
- if 'tag_name' in release:
- latest_version = release['tag_name']
- logging.info(f"最新版本号 {latest_version}")
- return latest_version
- else:
- logging.error("无法获取最新版本号。")
- else:
- logging.error("无法获取发布信息。")
-
-
- def update_projects(json_file):
- logging.info("开始更新项目列表...")
- # 更新项目列表中的每个项目
- with open(json_file, 'r', encoding='utf-8') as f:
- data = json.load(f)
-
- updated_projects = []
-
- for project in data["projects"]:
- owner = project["owner"]
- name = project["name"]
- version = project["version"]
- save_path = project["save_path"]
- file = project.get("file")
-
- logging.info(f"处理项目 {owner}/{name}")
-
- if not os.path.exists(save_path):
- os.makedirs(save_path)
-
- project_file = os.path.join(save_path, name)
- if not os.path.isfile(project_file):
- # 如果项目文件不存在,则尝试下载
- if download_release_files(owner, name, version, save_path, file):
- logging.info(f"成功下载项目 {owner}/{name}")
- version = get_latest_version(owner, name)
- else:
- logging.error(f"无法下载项目 {owner}/{name}")
-
- updated_projects.append({
- "owner": owner,
- "name": name,
- "version": version,
- "save_path": save_path,
- "file": file
- })
-
- updated_data = {
- "projects": updated_projects
- }
-
- save_projects_to_json(updated_data, json_file)
-
-
- def save_projects_to_json(data, json_file):
- logging.info("保存更新后的项目列表...")
- # 将更新后的项目列表保存到JSON文件中
- with open(json_file, 'w', encoding='utf-8') as f:
- json.dump(data, f, indent=4, ensure_ascii=False)
-
-
- if __name__ == "__main__":
- logging.info("开始更新项目...")
- update_projects(PROJECTS_JSON_FILE)
- logging.info("项目更新完成.")
- logging.shutdown()
以下是JSON文件
- {
- "projects": [
- {
- "owner": "2dust",
- "name": "v2rayN",
- "version": "6.33",
- "save_path": "D:\\加速",
- "file": "v2rayN.zip"
- },
- {
- "owner": "6yy66yy",
- "name": "legod-auto-pause",
- "version": "v2.2.1",
- "save_path": "D:\\加速\\雷神加速器\\自动暂停",
- "file": ""
- },
- {
- "owner": "R3nzTheCodeGOD",
- "name": "R3nzSkin",
- "version": "v3.2.7",
- "save_path": "D:\\Game\\GameTools\\LOL\\R3nzSkin",
- "file": ""
- },
- {
- "owner": "rumi-chan",
- "name": "Sakura",
- "version": "v1.0.2",
- "save_path": "D:\\Game\\GameTools\\LOL\\Sakura",
- "file": ""
- },
- {
- "owner": "cangzhang",
- "name": "champ-r",
- "version": "v2.0.2-b7",
- "save_path": "D:\\Game\\GameTools\\LOL\\ChampR\\V2",
- "file": ""
- },
- {
- "owner": "H4rry217",
- "name": "AimRobotLite",
- "version": "V2.0.7",
- "save_path": "D:\\Game\\GameTools\\BFV\\ARL",
- "file": ""
- }
- ]
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。