From 6d9595b643db8ad1191a749c0d4273372d1e0b02 Mon Sep 17 00:00:00 2001 From: InfinityPacer <160988576+InfinityPacer@users.noreply.github.com> Date: Tue, 10 Dec 2024 18:50:50 +0800 Subject: [PATCH 1/2] feat(event): add source tracking in download event --- app/chain/download.py | 9 ++++++--- app/chain/subscribe.py | 22 ++++++++++++++++++++-- app/schemas/event.py | 1 + 3 files changed, 27 insertions(+), 5 deletions(-) diff --git a/app/chain/download.py b/app/chain/download.py index 7066d5ad..9bb01b1f 100644 --- a/app/chain/download.py +++ b/app/chain/download.py @@ -345,7 +345,8 @@ class DownloadChain(ChainBase): username=username, channel=channel.value if channel else None, date=time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), - media_category=media_category + media_category=media_category, + note={"source": source} ) # 登记下载文件 @@ -383,7 +384,8 @@ class DownloadChain(ChainBase): "context": context, "username": username, "downloader": _downloader, - "episodes": episodes + "episodes": episodes, + "source": source }) else: # 下载失败 @@ -489,7 +491,8 @@ class DownloadChain(ChainBase): logger.debug(f"Initial contexts: {len(contexts)} items, Downloader: {downloader}") event_data = ResourceSelectionEventData( contexts=contexts, - downloader=downloader + downloader=downloader, + origin=source ) event = eventmanager.send_event(ChainEventType.ResourceSelection, event_data) # 如果事件修改了上下文数据,使用更新后的数据 diff --git a/app/chain/subscribe.py b/app/chain/subscribe.py index 6067ebb8..d234d25e 100644 --- a/app/chain/subscribe.py +++ b/app/chain/subscribe.py @@ -391,6 +391,15 @@ class SubscribeChain(ChainBase, metaclass=Singleton): continue # 自动下载 + source_keyword = { + 'id': subscribe.id, + 'name': subscribe.name, + 'year': subscribe.year, + 'type': subscribe.type, + 'season': subscribe.season, + 'tmdbid': subscribe.tmdbid + } + source = f"Subscribe|{source_keyword}" downloads, lefts = self.downloadchain.batch_download( contexts=matched_contexts, no_exists=no_exists, @@ -399,7 +408,7 @@ class SubscribeChain(ChainBase, metaclass=Singleton): save_path=subscribe.save_path, media_category=subscribe.media_category, downloader=subscribe.downloader, - source="Subscribe" + source=source ) # 判断是否应完成订阅 @@ -784,6 +793,15 @@ class SubscribeChain(ChainBase, metaclass=Singleton): # 开始批量择优下载 logger.info(f'{mediainfo.title_year} 匹配完成,共匹配到{len(_match_context)}个资源') + source_keyword = { + 'id': subscribe.id, + 'name': subscribe.name, + 'year': subscribe.year, + 'type': subscribe.type, + 'season': subscribe.season, + 'tmdbid': subscribe.tmdbid + } + source = f"Subscribe|{source_keyword}" downloads, lefts = self.downloadchain.batch_download(contexts=_match_context, no_exists=no_exists, userid=subscribe.username, @@ -791,7 +809,7 @@ class SubscribeChain(ChainBase, metaclass=Singleton): save_path=subscribe.save_path, media_category=subscribe.media_category, downloader=subscribe.downloader, - source="Subscribe") + source=source) # 判断是否要完成订阅 self.finish_subscribe_or_not(subscribe=subscribe, meta=meta, mediainfo=mediainfo, downloads=downloads, lefts=lefts) diff --git a/app/schemas/event.py b/app/schemas/event.py index 08e78420..b36e9025 100644 --- a/app/schemas/event.py +++ b/app/schemas/event.py @@ -165,6 +165,7 @@ class ResourceSelectionEventData(BaseModel): # 输入参数 contexts: Any = Field(None, description="待选择的资源上下文列表") downloader: Optional[str] = Field(None, description="下载器") + origin: Optional[str] = Field(None, description="来源") # 输出参数 updated: bool = Field(False, description="是否已更新") From 83798e6823a8c5f0f9eeae868f70cf9bf232be65 Mon Sep 17 00:00:00 2001 From: InfinityPacer <160988576+InfinityPacer@users.noreply.github.com> Date: Tue, 10 Dec 2024 21:23:52 +0800 Subject: [PATCH 2/2] feat(event): add multiple IDs to source with json --- app/chain/subscribe.py | 47 +++++++++++++++++++++++------------------- 1 file changed, 26 insertions(+), 21 deletions(-) diff --git a/app/chain/subscribe.py b/app/chain/subscribe.py index d234d25e..838a0f70 100644 --- a/app/chain/subscribe.py +++ b/app/chain/subscribe.py @@ -1,7 +1,8 @@ import copy +import json import random -import time import threading +import time from datetime import datetime from typing import Dict, List, Optional, Union, Tuple @@ -391,15 +392,6 @@ class SubscribeChain(ChainBase, metaclass=Singleton): continue # 自动下载 - source_keyword = { - 'id': subscribe.id, - 'name': subscribe.name, - 'year': subscribe.year, - 'type': subscribe.type, - 'season': subscribe.season, - 'tmdbid': subscribe.tmdbid - } - source = f"Subscribe|{source_keyword}" downloads, lefts = self.downloadchain.batch_download( contexts=matched_contexts, no_exists=no_exists, @@ -408,7 +400,7 @@ class SubscribeChain(ChainBase, metaclass=Singleton): save_path=subscribe.save_path, media_category=subscribe.media_category, downloader=subscribe.downloader, - source=source + source=self.get_subscribe_source_keyword(subscribe) ) # 判断是否应完成订阅 @@ -793,15 +785,6 @@ class SubscribeChain(ChainBase, metaclass=Singleton): # 开始批量择优下载 logger.info(f'{mediainfo.title_year} 匹配完成,共匹配到{len(_match_context)}个资源') - source_keyword = { - 'id': subscribe.id, - 'name': subscribe.name, - 'year': subscribe.year, - 'type': subscribe.type, - 'season': subscribe.season, - 'tmdbid': subscribe.tmdbid - } - source = f"Subscribe|{source_keyword}" downloads, lefts = self.downloadchain.batch_download(contexts=_match_context, no_exists=no_exists, userid=subscribe.username, @@ -809,7 +792,8 @@ class SubscribeChain(ChainBase, metaclass=Singleton): save_path=subscribe.save_path, media_category=subscribe.media_category, downloader=subscribe.downloader, - source=source) + source=self.get_subscribe_source_keyword(subscribe) + ) # 判断是否要完成订阅 self.finish_subscribe_or_not(subscribe=subscribe, meta=meta, mediainfo=mediainfo, downloads=downloads, lefts=lefts) @@ -1350,3 +1334,24 @@ class SubscribeChain(ChainBase, metaclass=Singleton): if state in ["R", "P"]: return "R,P" return state + + @staticmethod + def get_subscribe_source_keyword(subscribe: Subscribe) -> str: + """ + 构造用于订阅来源的关键字字符串 + :param subscribe: Subscribe 对象 + :return: 格式化的订阅来源关键字字符串,格式为 "Subscribe|{...}" + """ + source_keyword = { + 'id': subscribe.id, + 'name': subscribe.name, + 'year': subscribe.year, + 'type': subscribe.type, + 'season': subscribe.season, + 'tmdbid': subscribe.tmdbid, + 'imdbid': subscribe.imdbid, + 'tvdbid': subscribe.tvdbid, + 'doubanid': subscribe.doubanid, + 'bangumiid': subscribe.bangumiid + } + return f"Subscribe|{json.dumps(source_keyword, ensure_ascii=False)}"