From 5ceaf94169d6a41e09d07e2beefb2b264b09f62f Mon Sep 17 00:00:00 2001 From: jxxghp Date: Sat, 8 Jul 2023 16:30:45 +0800 Subject: [PATCH] fix download --- app/api/endpoints/download.py | 23 ++++++++++++++++++++++- app/chain/download.py | 2 +- app/core/context.py | 7 +++++-- app/core/meta/metabase.py | 1 + app/modules/slack/slack.py | 2 +- app/modules/telegram/telegram.py | 2 +- app/modules/wechat/wechat.py | 2 +- app/schemas/context.py | 2 ++ 8 files changed, 34 insertions(+), 7 deletions(-) diff --git a/app/api/endpoints/download.py b/app/api/endpoints/download.py index 9150d7b4..b3393993 100644 --- a/app/api/endpoints/download.py +++ b/app/api/endpoints/download.py @@ -6,9 +6,11 @@ from app import schemas from app.chain.douban import DoubanChain from app.chain.download import DownloadChain from app.chain.media import MediaChain -from app.core.context import MediaInfo +from app.core.context import MediaInfo, Context, TorrentInfo from app.core.metainfo import MetaInfo from app.core.security import verify_token +from app.db.models.user import User +from app.db.userauth import get_current_active_superuser from app.schemas import NotExistMediaInfo, MediaType router = APIRouter() @@ -23,6 +25,25 @@ async def read_downloading( return DownloadChain().downloading() +@router.post("/", summary="添加下载", response_model=schemas.Response) +async def add_downloading( + media_in: schemas.MediaInfo, + torrent_in: schemas.TorrentInfo, + current_user: User = Depends(get_current_active_superuser)) -> Any: + """ + 添加下载任务 + """ + context = Context( + meta_info=MetaInfo(title=torrent_in.title, subtitle=torrent_in.description), + media_info=MediaInfo().from_dict(media_in.dict()), + torrent_info=TorrentInfo(**torrent_in.dict()) + ) + did = DownloadChain().download_single(context=context, userid=current_user.name) + return schemas.Response(success=True if did else False, data={ + "download_id": did + }) + + @router.post("/notexists", summary="查询缺失媒体信息", response_model=List[NotExistMediaInfo]) async def exists(media_in: schemas.MediaInfo, _: schemas.TokenPayload = Depends(verify_token)) -> Any: diff --git a/app/chain/download.py b/app/chain/download.py index 6ec8c32a..622ba8e1 100644 --- a/app/chain/download.py +++ b/app/chain/download.py @@ -42,7 +42,7 @@ class DownloadChain(ChainBase): msg_text = f"{msg_text}\n种子:{torrent.title}" if torrent.seeders: msg_text = f"{msg_text}\n做种数:{torrent.seeders}" - msg_text = f"{msg_text}\n促销:{torrent.get_volume_factor_string()}" + msg_text = f"{msg_text}\n促销:{torrent.volume_factor}" if torrent.hit_and_run: msg_text = f"{msg_text}\nHit&Run:是" if torrent.description: diff --git a/app/core/context.py b/app/core/context.py index c28d3b27..84abfb84 100644 --- a/app/core/context.py +++ b/app/core/context.py @@ -62,7 +62,7 @@ class TorrentInfo: setattr(self, key, value) @staticmethod - def get_free_string(upload_volume_factor, download_volume_factor): + def get_free_string(upload_volume_factor: float, download_volume_factor: float) -> str: """ 计算促销类型 """ @@ -80,7 +80,8 @@ class TorrentInfo: } return free_strs.get('%.1f %.1f' % (upload_volume_factor, download_volume_factor), "未知") - def get_volume_factor_string(self): + @property + def volume_factor(self): """ 返回促销信息 """ @@ -90,6 +91,8 @@ class TorrentInfo: """ 返回字典 """ + dicts = asdict(self) + dicts["volume_factor"] = self.volume_factor return asdict(self) diff --git a/app/core/meta/metabase.py b/app/core/meta/metabase.py index b86970bb..48998491 100644 --- a/app/core/meta/metabase.py +++ b/app/core/meta/metabase.py @@ -447,4 +447,5 @@ class MetaBase(object): dicts = asdict(self) dicts["type"] = self.type.value if self.type else None dicts["season_episode"] = self.season_episode + dicts["edtion"] = self.edtion return dicts diff --git a/app/modules/slack/slack.py b/app/modules/slack/slack.py index c060d269..6c1cb19c 100644 --- a/app/modules/slack/slack.py +++ b/app/modules/slack/slack.py @@ -274,7 +274,7 @@ class Slack: f"{meta.resource_term} " \ f"{meta.release_group}" title = re.sub(r"\s+", " ", title).strip() - free = torrent.get_volume_factor_string() + free = torrent.volume_factor seeder = f"{torrent.seeders}↑" description = torrent.description text = f"{index}. 【{site_name}】<{link}|{title}> " \ diff --git a/app/modules/telegram/telegram.py b/app/modules/telegram/telegram.py index 438e5f10..79747f2c 100644 --- a/app/modules/telegram/telegram.py +++ b/app/modules/telegram/telegram.py @@ -152,7 +152,7 @@ class Telegram(metaclass=Singleton): f"{meta.resource_term} " \ f"{meta.release_group}" title = re.sub(r"\s+", " ", title).strip() - free = torrent.get_volume_factor_string() + free = torrent.volume_factor seeder = f"{torrent.seeders}↑" description = torrent.description caption = f"{caption}\n{index}.【{site_name}】[{title}]({link}) " \ diff --git a/app/modules/wechat/wechat.py b/app/modules/wechat/wechat.py index f561b603..fa38d617 100644 --- a/app/modules/wechat/wechat.py +++ b/app/modules/wechat/wechat.py @@ -218,7 +218,7 @@ class WeChat(metaclass=Singleton): f"{meta.resource_term} " \ f"{meta.release_group} " \ f"{StringUtils.str_filesize(torrent.size)} " \ - f"{torrent.get_volume_factor_string()} " \ + f"{torrent.volume_factor} " \ f"{torrent.seeders}↑" title = re.sub(r"\s+", " ", title).strip() articles.append({ diff --git a/app/schemas/context.py b/app/schemas/context.py index 8317fb3c..acccdbc6 100644 --- a/app/schemas/context.py +++ b/app/schemas/context.py @@ -177,6 +177,8 @@ class TorrentInfo(BaseModel): labels: Optional[list] = [] # 种子优先级 pri_order: Optional[int] = 0 + # 促销 + volume_factor: Optional[str] = None class Context(BaseModel):