import requests,os,time
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
from pathlib import Path
from tqdm import tqdm
from threading import Thread
from time import time
downloadPath =Path.cwd().parent.parent.parent.joinpath("apk","download")
def Interrupt_exception(func):
'''
定义一个装饰器:
捕捉手动停止错误
'''
def wrapper(*args, **kwargs):
try:
result = func(*args, **kwargs)
return result
except KeyboardInterrupt:
print("手动停止")
os._exit(0)
return wrapper
class myloadApk():
def __init__(self,which,version=None,bulid=None,changelog="Inner"):
self.which = which
self.version = version
self.build = bulid
self.changelog = changelog
self.thread_num = 3
self.url = None
@Interrupt_exception
def findApk(self):
'''
查找下载APP id
'''
for i in range(1,10):
print("正在查询第" +str(i)+"页")
url = "http://fir-api.fircli.cn/apps/" + self.id + "/releases?api_token=" + self.api_token + "&page="+str(i)
respones = requests.get(url, verify=False)
allHistory = respones.json()['datas']
for build_version in allHistory:
print(build_version["version"])
print(build_version["build"])
print(build_version["changelog"])
print(build_version["id"])
print("--------********---------")
if (self.version == None or build_version["version"] == self.version) and (self.build == None or build_version["build"] == self.build) and self.changelog in build_version["changelog"]:
self.version = build_version["version"]
self.build = build_version["build"]
return build_version["id"]
def download_token(self):
'''
获取download_token
'''
url ="http://api.bq04.com/apps/"+self.id+"/download_token?api_token="+self.api_token
respones = requests.get(url, verify=False)
return respones.json()['download_token']
def bytes_format(self, value):
'''
文件大小自转换
'''
units = ["B", "KB", "MB", "GB", "TB", "PB"]
size = 1024.0
for i in range(len(units)):
if (value / size) < 1:
return "%.2f%s" % (value, units[i])
value = value / size
def get_url(self):
'''
获取下载url
'''
down_token = self.download_token()
releaseid = self.findApk()
print("%s : %s(%s) %s" % (self.which, self.version, self.build, self.changelog))
url = "http://download.bq04.com/apps/"+self.id+"/install?download_token="+down_token+"&release_id="+releaseid
return url
def get_total_size(self):
'''
获取下载文件大小
'''
Response = requests.get(self.url, stream=True, verify=False)
# 总大小
total_size = int(Response.headers.get('content-length', 0))
print(self.bytes_format(total_size))
return total_size
@Interrupt_exception
def loadAPK(self):
'''
单线程下载ing
'''
start = time()
self.url = self.get_url()
self.total_size = self.get_total_size()
paths = str(downloadPath) + "/HelloTalk_" + self.version + "_" + self.build + "_" + self.which + ".apk"
# stream=True的作用是仅让响应头被下载,连接保持打开状态,
if Path(paths).exists():
temp_size = os.path.getsize(paths)
print("已下载文件 "+ self.bytes_format(temp_size))
else: temp_size = 0
if temp_size >= self.total_size:
print("下载完成")
return paths
header = {"Range": f"bytes={temp_size}-{self.total_size}"}
Req = requests.get(self.url, headers=header, stream=True, verify=False, timeout=10)
with open(paths, "ab") as file, tqdm(
initial=temp_size,
desc="正在下载",
total=self.total_size,
unit='',
unit_scale=True,
unit_divisor=1024,
) as bar:
for data in Req.iter_content(chunk_size=1024): # 边下载边存硬盘
size = file.write(data)
bar.update(size)
end = time()
print('总共耗费了%.2f秒.' % (end - start))
return paths
@Interrupt_exception
def adb_install(self):
'''
安装ing
'''
file_names = self.loadAPK()
print("正在安装...")
cmd_shell = "adb -d install -r -d " + file_names
openApp = os.system(cmd_shell)
if openApp == 0:
os.system("adb -d shell am start -n com.hellotalk/.lib.main.launch.ui.LaunchActivity")
标签:fir,Python,self,url,单线程,version,build,print,size
From: https://www.cnblogs.com/QingshanY/p/17087815.html