Skip to content
This repository has been archived by the owner on Feb 1, 2024. It is now read-only.

Commit

Permalink
feat: ✨ add debug loggings
Browse files Browse the repository at this point in the history
  • Loading branch information
AnzhiZhang committed Jul 2, 2022
1 parent 73aef83 commit 10452ef
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 24 deletions.
89 changes: 67 additions & 22 deletions utils/download.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import sys
import json
import shutil
import hashlib
Expand All @@ -14,18 +15,19 @@
from utils.logger import Logger

if TYPE_CHECKING:
from utils.requester import Requester
from utils.factory import Factory


class Download:
def __init__(
self,
requester: 'Requester',
factory: 'Factory',
name: str = None,
zip_file_path: str = None,
avatar_url: str = None
):
self.requester = requester
self.factory = factory
self.requester = self.factory.requester
self.name = name
self.zip_file_path = zip_file_path
self.avatar_url = avatar_url
Expand Down Expand Up @@ -62,11 +64,19 @@ def main(self):

# 新建临时文件夹
os.mkdir(self.dir_path)
self.factory.logger.debug(
'Successfully created temp dir %s for modpack %s',
self.dir_path,
self.name
)

# 工具
self.logger = Logger(self.name, self.log_file_path)
self.thread_pool = ThreadPoolExecutor(4)

if '--debug' in sys.argv:
self.logger.set_debug(True)

def run():
toplevel = Toplevel()
toplevel.title('正在下载模组…………')
Expand All @@ -80,6 +90,7 @@ def run():

# 解压文件
self.unzip()
self.logger.info('Unzipped files')

# 设置进度条
pb['maximum'] = self.mod_count * 2
Expand All @@ -104,6 +115,11 @@ def clear():
if self.avatar_url:
with open(self.avatar_path, 'wb') as f:
f.write(self.requester.get(self.avatar_url).content)
self.logger.debug(
'Downloaded avatar from %s and saved to %s',
self.avatar_url,
self.avatar_path
)

# 获取模组下载链接
download_urls = self.get_download_urls(update)
Expand All @@ -122,6 +138,10 @@ def clear():
'下载地址及问题反馈:\n'
'https://github.com/AnzhiZhang/CurseForgeModpackDownloader'
)
self.factory.logger.info(
'Successfully downloaded %s',
self.name
)

Thread(target=run, name='Download').start()

Expand All @@ -136,12 +156,12 @@ def unzip(self):
zf.extract(i, self.dir_path)

def get_download_urls(self, update: Callable):
# 获取下载链接 API
with open(self.manifest_path) as f:
data = json.load(f)
files = data['files']

count = len(files)
self.logger.info('%d mods were found', count)
result = []
i = 0
for r in self.thread_pool.map(
Expand All @@ -152,13 +172,15 @@ def get_download_urls(self, update: Callable):
):
i += 1
update()
self.logger.info(f'获取模组下载链接({i}/{count})')
data = r.json()['data']
result.append('https://edge.forgecdn.net/files/{}/{}/{}'.format(
url = 'https://edge.forgecdn.net/files/{}/{}/{}'.format(
int(data['id'] / 1000),
data['id'] % 1000,
data['fileName']
))
)
result.append(url)
self.logger.info(f'获取模组下载链接({i}/{count})')
self.logger.debug(f'Got mod download url: %s', url)
return result

def download_mods(self, urls, update: Callable):
Expand All @@ -172,9 +194,17 @@ def download(url):
response = self.requester.get(url)

# 校验
md5 = hashlib.md5(response.content).hexdigest()
if md5 != response.headers['ETag'].replace('"', ''):
calculated_md5 = hashlib.md5(response.content).hexdigest()
server_md5 = response.headers['ETag'].replace('"', '')
if calculated_md5 != server_md5:
failed_mods['verify'].append(f'{mod_name}{url})')
self.logger.debug(
'Mod %s\'s calculated md5 value %s '
'is difference with server provided md5 value %s!',
mod_name,
calculated_md5,
server_md5
)

# 写入文件
with open(mod_path, 'wb') as f:
Expand Down Expand Up @@ -240,20 +270,26 @@ def write_mmc_files(self):

# 写入 mmc-pack.json
mmc_pack_path = os.path.join(self.dir_path, 'mmc-pack.json')
data = {
'components': [
{
'uid': 'net.minecraft',
'version': minecraft_version
},
{
'uid': mod_loader_uid,
'version': mod_loader_version
}
],
'formatVersion': 1
}
with open(mmc_pack_path, 'w', encoding='utf-8') as f:
json.dump({
'components': [
{
'uid': 'net.minecraft',
'version': minecraft_version
},
{
'uid': mod_loader_uid,
'version': mod_loader_version
}
],
'formatVersion': 1
}, f, indent=4)
json.dump(data, f, indent=4)
self.logger.debug(
'Saved following data into %s:\n%s',
mmc_pack_path,
data
)

# 写入 instance.cfg
instance_cfg_path = os.path.join(self.dir_path, 'instance.cfg')
Expand All @@ -262,17 +298,25 @@ def write_mmc_files(self):
content += f'iconKey={self.avatar_name}\n'
with open(instance_cfg_path, 'w', encoding='utf-8') as f:
f.write(content)
self.logger.debug(
'Saved following data into %s:\n%s',
instance_cfg_path,
content
)

def make_zip(self):
# 清理 CF 文件
modlist_path = os.path.join(self.dir_path, 'modlist.html')
if os.path.isfile(modlist_path):
os.remove(modlist_path)
self.logger.debug('Deleted file %s', modlist_path)
os.remove(self.manifest_path)
self.logger.debug('Deleted file %s', self.manifest_path)
os.rename(
self.overrides_dir_path,
os.path.join(self.dir_path, '.minecraft')
)
self.logger.debug('Renamed overrides to .minecraft')

# 压缩
with ZipFile(self.zip_file_path, mode='w', compression=ZIP_STORED) as z:
Expand All @@ -284,6 +328,7 @@ def make_zip(self):
self.dir_path.split(os.sep)[-1]
)
z.write(zf_path, arcname=arcname)
self.logger.debug('Made zip %s', self.zip_file_path)

# Move to out of temp dir
if PATH.DOWNLOADING_DIR_PATH in self.zip_file_path:
Expand Down
3 changes: 3 additions & 0 deletions utils/requester.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ def get(self, url: str, params: Dict[str, Any] = None) -> Response:
:return: Response.
"""
url = quote(url, safe=':/')
self.__factory.logger.debug(
'Sending GET request to %s with params %s', url, params
)

# add params
if params:
Expand Down
9 changes: 7 additions & 2 deletions utils/window/frames/buttons.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def __init__(self, master: 'Main'):
background='white',
command=lambda:
Download(
self.__main_window.factory.requester,
self.__main_window.factory,
zip_file_path=askopenfilename()
).main()
)
Expand Down Expand Up @@ -81,6 +81,11 @@ def run():
avatar_url = self.__main_window.show_frame.selected_avatar_url
file_path = os.path.join(PATH.DOWNLOADING_DIR_PATH, file_name)

self.__main_window.factory.logger.debug(
'Downloading modpack %s',
file_name
)

# Start download file
thread = Thread(target=run, name='Download')
thread.start()
Expand All @@ -91,7 +96,7 @@ def run():

# Other files
Download(
self.__main_window.factory.requester,
self.__main_window.factory,
name=os.path.splitext(file_name)[0],
zip_file_path=file_path,
avatar_url=avatar_url
Expand Down

0 comments on commit 10452ef

Please sign in to comment.