From 2ad62a468cb9396ff5df98cc6a6d8510b9553a56 Mon Sep 17 00:00:00 2001 From: jxxghp Date: Thu, 6 Jul 2023 17:42:12 +0800 Subject: [PATCH] =?UTF-8?q?fix=20=E5=B7=B2=E4=B8=8B=E8=BD=BD=E4=BD=86?= =?UTF-8?q?=E6=9C=AA=E5=85=A5=E5=BA=93=E5=AF=BC=E8=87=B4=E9=87=8D=E5=A4=8D?= =?UTF-8?q?=E4=B8=8B=E8=BD=BD=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/chain/download.py | 3 ++ app/chain/subscribe.py | 64 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 66 insertions(+), 1 deletion(-) diff --git a/app/chain/download.py b/app/chain/download.py index 9093e130..08021dec 100644 --- a/app/chain/download.py +++ b/app/chain/download.py @@ -404,6 +404,9 @@ class DownloadChain(ChainBase): userid=userid) if not download_id: continue + # 把识别的集更新到上下文 + context.meta_info.begin_episode = min(selected_episodes) + context.meta_info.end_episode = max(selected_episodes) # 下载成功 downloaded_list.append(context) # 更新仍需集数 diff --git a/app/chain/subscribe.py b/app/chain/subscribe.py index 26761628..d8b75447 100644 --- a/app/chain/subscribe.py +++ b/app/chain/subscribe.py @@ -1,3 +1,4 @@ +import json import re from datetime import datetime from typing import Dict, List, Optional, Union, Tuple @@ -157,7 +158,7 @@ class SubscribeChain(ChainBase): 订阅搜索 :param sid: 订阅ID,有值时只处理该订阅 :param state: 订阅状态 N:未搜索 R:已搜索 - :param manul: 是否手动搜索 + :param manual: 是否手动搜索 :return: 更新订阅状态为R或删除订阅 """ if sid: @@ -210,7 +211,9 @@ class SubscribeChain(ChainBase): # 过滤 matched_contexts = [] for context in contexts: + torrent_meta = context.meta_info torrent_info = context.torrent_info + torrent_mediainfo = context.media_info # 包含 if subscribe.include: if not re.search(r"%s" % subscribe.include, @@ -221,6 +224,11 @@ class SubscribeChain(ChainBase): if re.search(r"%s" % subscribe.exclude, f"{torrent_info.title} {torrent_info.description}", re.I): continue + # 如果是电视剧过滤掉已经下载的集数 + if torrent_mediainfo.type == MediaType.TV: + if self.__check_subscribe_note(subscribe, torrent_meta.episodes): + logger.info(f'{torrent_info.title} 对应剧集 {torrent_meta.episodes} 已下载过') + continue matched_contexts.append(context) if not matched_contexts: logger.warn(f'订阅 {subscribe.name} 没有符合过滤条件的资源') @@ -230,6 +238,10 @@ class SubscribeChain(ChainBase): # 自动下载 downloads, lefts = self.downloadchain.batch_download(contexts=matched_contexts, no_exists=no_exists) + # 更新已经下载的集数 + if downloads: + self.__update_subscribe_note(subscribe, downloads) + if downloads and not lefts: # 全部下载完成 logger.info(f'{mediainfo.title_year} 下载完成,完成订阅') @@ -342,6 +354,11 @@ class SubscribeChain(ChainBase): torrent_meta = context.meta_info torrent_mediainfo = context.media_info torrent_info = context.torrent_info + # 如果是电视剧过滤掉已经下载的集数 + if torrent_mediainfo.type == MediaType.TV: + if self.__check_subscribe_note(subscribe, torrent_meta.episodes): + logger.info(f'{torrent_info.title} 对应剧集 {torrent_meta.episodes} 已下载过') + continue # 包含 if subscribe.include: if not re.search(r"%s" % subscribe.include, @@ -364,6 +381,10 @@ class SubscribeChain(ChainBase): if _match_context: # 批量择优下载 downloads, lefts = self.downloadchain.batch_download(contexts=_match_context, no_exists=no_exists) + # 更新已经下载的集数 + if downloads: + self.__update_subscribe_note(subscribe, downloads) + if downloads and not lefts: # 全部下载完成 logger.info(f'{mediainfo.title_year} 下载完成,完成订阅') @@ -380,6 +401,47 @@ class SubscribeChain(ChainBase): # 未搜索到资源,但本地缺失可能有变化,更新订阅剩余集数 self.__upate_lack_episodes(lefts=no_exists, subscribe=subscribe, mediainfo=mediainfo) + def __update_subscribe_note(self, subscribe: Subscribe, downloads: List[Context]): + """ + 更新已下载集数到note字段 + """ + # 查询现有Note + if not downloads: + return + note = [] + if subscribe.note: + note = json.loads(subscribe.note) + for context in downloads: + meta = context.meta_info + mediainfo = context.media_info + if mediainfo.type != MediaType.TV: + continue + if mediainfo.tmdb_id != subscribe.tmdbid: + continue + episodes = meta.episodes + if not episodes: + continue + # 合并已下载集 + note = list(set(note).union(set(episodes))) + # 更新订阅 + self.subscribehelper.update(subscribe.id, { + "note": json.dumps(note) + }) + + @staticmethod + def __check_subscribe_note(subscribe: Subscribe, episodes: List[int]) -> bool: + """ + 检查当前集是否已下载过 + """ + if not subscribe.note: + return False + if not episodes: + return False + note = json.loads(subscribe.note) + if set(episodes).issubset(set(note)): + return True + return False + def __upate_lack_episodes(self, lefts: Dict[int, Dict[int, NotExistMediaInfo]], subscribe: Subscribe, mediainfo: MediaInfo,