主要包含逻辑
- 支持进度条展示
- 支持断点续传
- 支持检测文件是否已经存在
- 支持检测老版本问题存在并做删除处理
代码实现
| import os |
| import requests |
| |
| import fnmatch |
| from tqdm import tqdm |
| import time |
| import re |
| |
| |
| def get_cookie(): |
| """ |
| 从明道云拉取cookie信息 |
| """ |
| apiurl = 'https://cooperation.XXX.com:443/api/v2/open/worksheet/getFilterRows' |
| headers = { |
| "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.54 Safari/537.36 Edg/101.0.1210.39"} |
| body = { |
| "appKey": "f7b8e6816d39a931", |
| "sign": "ODlkNTVhNDdjZDhjNmYwODhjYjExZTU4NGFmODFkZDQzZjRlNGMzMzJlODdhNzhmNTVjYmQ4ZTA1MzAzNzc3Mw==", |
| "worksheetId": "hxggzjxx", |
| "viewId": "634509562b5b0f8d7a182f39", |
| "pageSize": 5000, |
| "pageIndex": 1, |
| "filters": [ |
| { |
| "controlId": "leibie", |
| "dataType": 2, |
| "spliceType": 1, |
| "filterType": 2, |
| "values": ["应用商店审核平台"]}] |
| } |
| |
| res_data = requests.post(apiurl, json=body, headers=headers).json()['data']['rows'] |
| for i in res_data: |
| if i['leibie'] == '应用商店审核平台' and i['jianmingcheng'] == 'cookie': |
| cookie = i['quzhi'] |
| elif i['leibie'] == '应用商店审核平台' and i['jianmingcheng'] == 'Authorization': |
| Authorization = i['quzhi'] |
| |
| headers = {"Authorization": Authorization, |
| "Connection": "keep-alive", |
| "Cookie": cookie, |
| "Host": "appstore.XXX.com", |
| "Referer": "https://appstore.XXX.com/cms/", |
| "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.54 Safari/537.36 Edg/101.0.1210.39" |
| } |
| return headers |
| |
| |
| headers = get_cookie() |
| |
| |
| def getv23app_info(): |
| """ |
| 获取V23上上架的应用 |
| """ |
| url = f'https://appstore.XXX.com/prod-api/store-admin-app/getAppList?pageNum=1&pageSize=4000&pkgInstallMode=1&app_name=&searchAppIds=&app_status=101&store_version=11&app_arch=4&appid=®ion=&app_mode=1&package_mode=&supWayland=0&appFrom=' |
| res = requests.get(url, headers=headers).json()['rows'] |
| res = sorted(res, key=lambda x: x['urge_upt_times'], reverse=True) |
| return res |
| |
| |
| def download_file_with_progress(url, output_path): |
| """ |
| 下载并提供进度展示功能,支持断点续传 |
| """ |
| local_size = 0 |
| if os.path.exists(output_path): |
| local_size = os.path.getsize(output_path) |
| |
| headers = {"Range": f"bytes={local_size}-"} |
| response = requests.get(url, headers=headers, stream=True) |
| total_size = int(response.headers.get('content-length', 0)) + local_size |
| block_size = 1024 |
| progress_bar = tqdm(total=total_size, initial=local_size, unit='iB', unit_scale=True) |
| |
| with open(output_path, 'ab') as file: |
| for data in response.iter_content(block_size): |
| progress_bar.update(len(data)) |
| file.write(data) |
| progress_bar.close() |
| |
| if total_size != 0 and progress_bar.n != total_size: |
| print("ERROR: Something went wrong") |
| |
| |
| def download_app(appinfo): |
| """ |
| Check for apps that meet the community test criteria and get download links and app names |
| """ |
| |
| app_name = appinfo['app_name'] |
| appid = appinfo['appid'] |
| |
| appinfoid = appinfo['app_info_id'] |
| appinfo_url = f'https://appstore.XXX.com/prod-api/store-admin-app/getAppDetail/{appinfoid}' |
| ressrc = requests.get(appinfo_url, headers=headers).json()['datas'] |
| res = ressrc['appPackages'] |
| for j in res: |
| if j['pkgArch'] in ['X86', 'x86', '全架构'] and '社区版V23' in j[ |
| 'supSysStr']: |
| download_url = f'https://home-store-img.XXX.com/{j["fileSaveKey"]}' |
| pkgVersion = j['pkgVersion'] |
| |
| download_name = f'{appid}_{app_name}_{pkgVersion}.deb' |
| download_name = re.sub(r'[:]', ".",download_name) |
| |
| |
| if os.path.exists(download_name): |
| |
| head_response = requests.head(download_url) |
| total_size = int(head_response.headers.get('content-length', 0)) |
| |
| |
| local_size = os.path.getsize(download_name) |
| |
| if local_size == total_size: |
| print(f'{download_name} 已存在且完整,跳过下载。') |
| continue |
| else: |
| time.sleep(1) |
| local_size_now = os.path.getsize(download_name) |
| |
| if local_size_now != local_size: |
| print(f'{download_name} 有其他程序在下载……') |
| continue |
| |
| print(f'{download_name} 已存在但不完整,准备断点续传……') |
| else: |
| print(f'准备下载 {download_name} 应用') |
| |
| download_file_with_progress(download_url, download_name) |
| print(f'{download_name} 应用下载完成!') |
| |
| |
| def main(): |
| appinfo = getv23app_info() |
| for i in appinfo: |
| download_app(i) |
| |
| |
| if __name__ == '__main__': |
| main() |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)