mirror of
https://github.com/ClovertaTheTrilobita/SanYeCao-Nonebot.git
synced 2026-04-01 22:04:51 +00:00
feat(jm_comic): 重构下载逻辑,提取公共方法
将下载功能提取为独立方法download_jm,优化代码复用性
This commit is contained in:
parent
77f577404b
commit
8453bb51c6
2 changed files with 37 additions and 39 deletions
|
|
@ -16,18 +16,9 @@ __name__ = "clover | jm_comic"
|
|||
jm_executor = ThreadPoolExecutor(max_workers=3)
|
||||
jm_executor.submit(lambda: None).result()
|
||||
|
||||
async def download_jm_Pemail(album_id: str| None,receiver_email: str| None):
|
||||
# 修改配置文件的下载路径
|
||||
source_path = await get_jm_config(receiver_email)
|
||||
option = jmcomic.JmOption.from_file(jm_config_path)
|
||||
# 还原配置文件
|
||||
await recover_jm_config(source_path)
|
||||
#调用JM下载api
|
||||
try:
|
||||
album_detail,downloader = await asyncio.get_event_loop().run_in_executor(jm_executor,jmcomic.download_album,album_id,option)
|
||||
except Exception as e:
|
||||
logger.error(f"下载失败 :{e}")
|
||||
return "下载失败,请重试"
|
||||
async def jm_email(album_id: str| None,receiver_email: str| None):
|
||||
|
||||
album_detail,downloader = await download_jm(album_id = album_id,file_name = None,receiver_email = receiver_email)
|
||||
# 创建变量
|
||||
folder_path = f"{jm_path}{receiver_email}"
|
||||
zip_path = f"{jm_path}{album_detail.title}.zip"
|
||||
|
|
@ -48,47 +39,54 @@ async def download_jm_Pemail(album_id: str| None,receiver_email: str| None):
|
|||
await delete_file(zip_path)
|
||||
return "发送邮件失败,请重试!"
|
||||
|
||||
async def download_jm_qr(album_id: str| None):
|
||||
# 修改配置文件的下载路径
|
||||
async def jm_qr(album_id: str| None):
|
||||
|
||||
file_name = f"JM-{album_id}-{datetime.now().date()}@{uuid.uuid4().hex}"
|
||||
source_path = await get_jm_config(file_name)
|
||||
option = jmcomic.JmOption.from_file(jm_config_path)
|
||||
# 还原配置文件
|
||||
await recover_jm_config(source_path)
|
||||
#调用JM下载api
|
||||
album_detail,downloader = await asyncio.get_event_loop().run_in_executor(jm_executor,jmcomic.download_album,album_id,option)
|
||||
if album_detail.title is None:
|
||||
return {
|
||||
"msg":"下载失败,请检查JM ID 是否正确"
|
||||
}
|
||||
album_detail,downloader = await download_jm(album_id = album_id,file_name = file_name,receiver_email = None)
|
||||
# 创建变量
|
||||
folder_path = f"{jm_path}{album_detail.title}"
|
||||
pdf_path = f"{jm_path}{file_name}.pdf"
|
||||
# 转为pdf
|
||||
pdf_status = await webp_to_pdf(folder_path,pdf_path)
|
||||
if not pdf_status:
|
||||
folder_path = f"{jm_path}{file_name}"
|
||||
zip_path = f"{jm_path}{file_name}{album_detail.title}.zip"
|
||||
# 压缩文件
|
||||
zip_status = await folder_zip(file_name,zip_path)
|
||||
if not zip_status:
|
||||
await delete_folder(folder_path)
|
||||
return {
|
||||
"msg":"PDF转换失败"
|
||||
}
|
||||
return "压缩文件失败"
|
||||
|
||||
# 发送文件
|
||||
send_status = await anonfile.upload_file(pdf_path)
|
||||
if send_status["success"]== "true":
|
||||
send_status = await anonfile.upload_file(zip_path)
|
||||
if send_status["success"]:
|
||||
file_code=send_status["code"]
|
||||
# 删除文件
|
||||
await delete_folder(pdf_path)
|
||||
await delete_folder(folder_path)
|
||||
await delete_file(zip_path)
|
||||
return {
|
||||
"msg":"获取成功~!码上下载!~",
|
||||
"qr_code": f"{qrserver_url}?size={qrserver_size}&data={anonfile_download_url}{file_code}"
|
||||
}
|
||||
else:
|
||||
await delete_folder(pdf_path)
|
||||
await delete_folder(folder_path)
|
||||
await delete_file(zip_path)
|
||||
return {
|
||||
"msg":"发送失败,请重试!"
|
||||
}
|
||||
|
||||
async def download_jm(album_id: str| None,file_name :str | None,receiver_email: str| None):
|
||||
# 修改配置文件的下载路径
|
||||
if not receiver_email:
|
||||
source_path = await get_jm_config(file_name)
|
||||
else:
|
||||
source_path = await get_jm_config(receiver_email)
|
||||
|
||||
option = jmcomic.JmOption.from_file(jm_config_path)
|
||||
# 还原配置文件
|
||||
await recover_jm_config(source_path)
|
||||
# 调用JM下载api
|
||||
try:
|
||||
return await asyncio.get_event_loop().run_in_executor(jm_executor, jmcomic.download_album,album_id, option)
|
||||
except Exception as e:
|
||||
logger.error(f"下载失败 :{e}")
|
||||
return "下载失败,请重试"
|
||||
|
||||
async def get_jm_config(file_name: str):
|
||||
|
||||
with open(jm_config_path, 'r', encoding='utf-8') as f:
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ from nonebot import logger
|
|||
from nonebot.rule import to_me
|
||||
from nonebot.plugin import on_command
|
||||
from nonebot.adapters.qq import MessageEvent, MessageSegment,Message
|
||||
from src.clover_jm.jm_comic import download_jm_qr, download_jm_Pemail
|
||||
from src.clover_jm.jm_comic import jm_qr, jm_email
|
||||
from nonebot.exception import FinishedException
|
||||
|
||||
__name__ = "JM_Download"
|
||||
|
|
@ -15,13 +15,13 @@ async def handle_email_download(album_id: str, email: str):
|
|||
if not validate_email(email):
|
||||
await jm.finish("邮箱格式不正确!")
|
||||
await jm.send("正在发送中,请稍等~")
|
||||
msg = await download_jm_Pemail(album_id=album_id, receiver_email=email)
|
||||
msg = await jm_email(album_id=album_id, receiver_email=email)
|
||||
await jm.finish(msg)
|
||||
|
||||
async def handle_qrcode_download(album_id: str):
|
||||
"""处理二维码发送载逻辑"""
|
||||
await jm.send("正在下载中,请稍等~")
|
||||
msgs = await download_jm_qr(album_id=album_id)
|
||||
msgs = await jm_qr(album_id=album_id)
|
||||
if "qr_code" not in msgs:
|
||||
await jm.finish(msgs["msg"])
|
||||
msg = Message([
|
||||
|
|
|
|||
Loading…
Reference in a new issue