-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
135 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
* text eol=lf | ||
*.png binary | ||
*.ico binary |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,42 @@ | ||
import aiohttp | ||
import aiofiles | ||
import asyncio | ||
|
||
class AsyncDownloader: | ||
def __init__(self): | ||
def __init__(self, retries=2, retry_delay=2): | ||
self.session = None | ||
self.retries = retries | ||
self.retry_delay = retry_delay | ||
|
||
async def __aenter__(self): | ||
self.session = await aiohttp.ClientSession().__aenter__() | ||
timeout = aiohttp.ClientTimeout(total=10) # 设置总超时 | ||
self.session = await aiohttp.ClientSession( | ||
connector=aiohttp.TCPConnector(ssl=False), | ||
timeout=timeout | ||
).__aenter__() | ||
return self | ||
|
||
async def __aexit__(self, exc_type, exc, tb): | ||
await self.session.__aexit__(exc_type, exc, tb) | ||
|
||
async def download_file(self, url, output_path): | ||
async with self.session.get(url) as response: | ||
with open(output_path, "wb") as f: | ||
while chunk := await response.content.read(1024): | ||
f.write(chunk) | ||
attempts = 0 | ||
while attempts < self.retries: | ||
try: | ||
async with self.session.get(url) as response: | ||
async with aiofiles.open(output_path, "wb") as f: | ||
async for chunk in response.content.iter_chunked(1024*64): | ||
await f.write(chunk) | ||
print(f"文件下载成功: {output_path}") | ||
break # 下载成功,退出循环 | ||
except Exception: | ||
attempts += 1 | ||
if attempts >= self.retries: | ||
print(f"文件下载失败,跳过: {output_path}") | ||
else: | ||
print(f"下载失败,尝试重新下载 ({attempts}/{self.retries})...") | ||
await asyncio.sleep(self.retry_delay) | ||
|
||
async def fetch_json(self, url): | ||
async with self.session.get(url) as response: | ||
return await response.json() | ||
return await response.json() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,68 @@ | ||
import os | ||
import shutil | ||
from zipfile import ZIP_DEFLATED, ZipFile | ||
|
||
from feedtheforge.const import * | ||
|
||
|
||
async def create_directory(path): | ||
""" | ||
创建目录,如果目录不存在则创建 | ||
""" | ||
os.makedirs(path, exist_ok=True) | ||
|
||
async def is_recent_file(filepath, days=7): | ||
""" | ||
检查文件最后修改时间是否在指定天数内 | ||
:param filepath: 文件路径 | ||
:param days: 距离当前的天数间隔,默认值为7天 | ||
:return: 如果文件存在且最后修改时间在指定天数内,返回 True 否则返回 False | ||
""" | ||
from datetime import datetime, timedelta | ||
|
||
if not os.path.exists(filepath): | ||
return False | ||
modification_date = datetime.fromtimestamp(os.path.getmtime(filepath)).date() | ||
current_date = datetime.now().date() | ||
if current_date - modification_date < timedelta(days=days): | ||
return True | ||
return False | ||
|
||
def zip_modpack(modpack_name): | ||
""" | ||
压缩整合包文件夹为一个zip文件 | ||
:param modpack_name: 整合包的名称 | ||
""" | ||
print(lang.t("feedtheforge.main.zipping_modpack")) | ||
|
||
with ZipFile(f"{modpack_name}.zip", "w", ZIP_DEFLATED) as zf: | ||
for dirname, _, files in os.walk(modpack_path): | ||
for filename in files: | ||
file_path = os.path.join(dirname, filename) | ||
zf.write(file_path, os.path.relpath(file_path, modpack_path)) | ||
|
||
print(lang.t("feedtheforge.main.modpack_created", modpack_name=f"{modpack_name}.zip")) | ||
shutil.rmtree(modpack_path, ignore_errors=True) | ||
|
||
def clean_temp(): | ||
""" | ||
清理缓存目录中的临时文件 | ||
""" | ||
size = 0 | ||
for root, _, files in os.walk(cache_dir): | ||
size += sum([os.path.getsize(os.path.join(root, name)) for name in files]) | ||
shutil.rmtree(cache_dir, ignore_errors=True) | ||
print(lang.t("feedtheforge.main.clean_temp", size=int(size / 1024))) | ||
|
||
def pause(): | ||
""" | ||
退出程序 | ||
""" | ||
if os.name == 'nt': | ||
os.system('pause') | ||
else: | ||
input(lang.t("feedtheforge.main.pause")) | ||
import os | ||
import shutil | ||
|
||
from feedtheforge.const import * | ||
|
||
|
||
async def create_directory(path): | ||
""" | ||
创建目录,如果目录不存在则创建 | ||
""" | ||
os.makedirs(path, exist_ok=True) | ||
|
||
async def is_recent_file(filepath, days=7): | ||
""" | ||
检查文件最后修改时间是否在指定天数内 | ||
:param filepath: 文件路径 | ||
:param days: 距离当前的天数间隔,默认值为7天 | ||
:return: 如果文件存在且最后修改时间在指定天数内,返回 True 否则返回 False | ||
""" | ||
from datetime import datetime, timedelta | ||
|
||
if not os.path.exists(filepath): | ||
return False | ||
modification_date = datetime.fromtimestamp(os.path.getmtime(filepath)).date() | ||
current_date = datetime.now().date() | ||
if current_date - modification_date < timedelta(days=days): | ||
return True | ||
return False | ||
|
||
def zip_modpack(modpack_name): | ||
""" | ||
压缩整合包文件夹为一个zip文件 | ||
:param modpack_name: 整合包的名称 | ||
""" | ||
from zipfile import ZIP_DEFLATED, ZipFile | ||
|
||
print(lang.t("feedtheforge.main.zipping_modpack")) | ||
|
||
with ZipFile(f"{modpack_name}.zip", "w", ZIP_DEFLATED) as zf: | ||
for dirname, _, files in os.walk(modpack_path): | ||
for filename in files: | ||
file_path = os.path.join(dirname, filename) | ||
zf.write(file_path, os.path.relpath(file_path, modpack_path)) | ||
|
||
print(lang.t("feedtheforge.main.modpack_created", modpack_name=f"{modpack_name}.zip")) | ||
shutil.rmtree(modpack_path, ignore_errors=True) | ||
|
||
def clean_temp(): | ||
""" | ||
清理缓存目录中的临时文件 | ||
""" | ||
size = 0 | ||
for root, _, files in os.walk(cache_dir): | ||
size += sum([os.path.getsize(os.path.join(root, name)) for name in files]) | ||
shutil.rmtree(cache_dir, ignore_errors=True) | ||
print(lang.t("feedtheforge.main.clean_temp", size=int(size / 1024))) | ||
|
||
def pause(): | ||
""" | ||
退出程序 | ||
""" | ||
if os.name == 'nt': | ||
os.system('pause') | ||
else: | ||
input(lang.t("feedtheforge.main.pause")) | ||
exit(0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
aiohttp | ||
aiofiles | ||
pick |