94 lines
3.0 KiB
Python
94 lines
3.0 KiB
Python
# coding:utf-8
|
|
import json
|
|
import os
|
|
|
|
import requests
|
|
|
|
from core.config import settings
|
|
from .make_tool import MakeTool
|
|
from .put_file import put_file
|
|
|
|
|
|
class Bale:
|
|
status = 'idle'
|
|
|
|
@classmethod
|
|
def _post(cls, url, data, try_num=3):
|
|
resp = requests.post(url, data=data)
|
|
if resp.status_code != 200:
|
|
if try_num > 0:
|
|
return cls._post(url, data, try_num - 1)
|
|
|
|
@classmethod
|
|
def run_bale_apk(cls, dir_, id_):
|
|
try:
|
|
pkg_num = 0
|
|
with open(os.path.join(settings.ROOT_DIR, 'ApkTool/channel.json'), 'r') as f:
|
|
props = json.load(f)
|
|
pkg_num = int(props.get('num', {}).get('value', 0))
|
|
|
|
cls.status = 'busy'
|
|
make_tool = MakeTool(os.path.join(settings.ROOT_DIR, 'ApkTool/channel.json'))
|
|
a, b = make_tool.run()
|
|
if not a:
|
|
data = {
|
|
'id': id_,
|
|
'url': '',
|
|
'apkname': '',
|
|
'total_pkg': pkg_num,
|
|
'current_pkg': 0,
|
|
'done': 0,
|
|
'code': -1,
|
|
'msg': b
|
|
}
|
|
cls._post('http://gamesdk.legu.cc/api/subpackageRecord/getRecord', data=data)
|
|
return
|
|
|
|
path = os.path.join(settings.ROOT_DIR, 'ApkTool/bin')
|
|
if not os.path.exists(path):
|
|
return
|
|
cls.status = 'upload'
|
|
print('开始上传包')
|
|
count = 0
|
|
for item in os.listdir(path):
|
|
if item.endswith('.apk'):
|
|
count += 1
|
|
print(f'开始上传:{item}')
|
|
is_ok, msg = put_file(f'{dir_}/{item}', os.path.join(path, item))
|
|
os.remove(os.path.join(path, item))
|
|
if not is_ok:
|
|
data = {
|
|
'id': id_,
|
|
'url': msg,
|
|
'apkname': item,
|
|
'total_pkg': pkg_num,
|
|
'current_pkg': count,
|
|
'done': int(count >= pkg_num),
|
|
'code': -1,
|
|
'msg': msg
|
|
}
|
|
cls._post('http://gamesdk.legu.cc/api/subpackageRecord/getRecord', data=data)
|
|
data = {
|
|
'id': id_,
|
|
'url': msg,
|
|
'apkname': item,
|
|
'total_pkg': pkg_num,
|
|
'current_pkg': count,
|
|
'done': int(count >= pkg_num),
|
|
'code': 0
|
|
}
|
|
cls._post('http://gamesdk.legu.cc/api/subpackageRecord/getRecord', data=data)
|
|
print(f'上传:{item} 完成')
|
|
|
|
|
|
|
|
except Exception as e:
|
|
with open('log.log', 'w') as f:
|
|
f.write(str(e))
|
|
|
|
finally:
|
|
cls.status = 'idle'
|
|
|
|
|
|
bale = Bale()
|