From a4a391f238747bcaed2a4348ed68aba6900a5144 Mon Sep 17 00:00:00 2001 From: jxxghp Date: Thu, 8 Jun 2023 18:47:04 +0800 Subject: [PATCH] fix bug --- app/chain/common.py | 17 +++++++++-------- app/chain/douban_sync.py | 4 ++-- app/chain/subscribe.py | 8 ++++---- app/chain/user_message.py | 19 ++++++++++++++++++- app/core/context.py | 8 ++++---- app/modules/telegram/telegram.py | 2 -- app/modules/themoviedb/tmdb_cache.py | 2 +- tests/run.py | 6 +++--- tests/test_doubansync.py | 6 +++--- 9 files changed, 44 insertions(+), 28 deletions(-) diff --git a/app/chain/common.py b/app/chain/common.py index f025d4c6..bae11a33 100644 --- a/app/chain/common.py +++ b/app/chain/common.py @@ -1,6 +1,6 @@ import re from pathlib import Path -from typing import List, Optional, Tuple, Set +from typing import List, Optional, Tuple, Set, Dict from app.chain import ChainBase from app.core import MediaInfo @@ -79,7 +79,7 @@ class CommonChain(ChainBase): ] """ # 已下载的项目 - downloaded_list: list = [] + downloaded_list: List[Context] = [] def __download_torrent(_torrent: TorrentInfo) -> Tuple[Optional[Path], list]: """ @@ -141,9 +141,9 @@ class CommonChain(ChainBase): userid=userid) return _hash - def __update_seasons(tmdbid, need, current): + def __update_seasons(tmdbid: str, need: list, current: list) -> list: """ - 更新need_tvs季数 + 更新need_tvs季数,返回剩余季数 """ need = list(set(need).difference(set(current))) for cur in current: @@ -154,9 +154,9 @@ class CommonChain(ChainBase): need_tvs.pop(tmdbid) return need - def __update_episodes(tmdbid, seq, need, current): + def __update_episodes(tmdbid: str, seq: int, need: list, current: set) -> list: """ - 更新need_tvs集数 + 更新need_tvs集数,返回剩余集数 """ need = list(set(need).difference(set(current))) if need: @@ -167,7 +167,7 @@ class CommonChain(ChainBase): need_tvs.pop(tmdbid) return need - def __get_season_episodes(tmdbid, season): + def __get_season_episodes(tmdbid: str, season: int) -> int: """ 获取需要的季的集数 """ @@ -186,7 +186,7 @@ class CommonChain(ChainBase): # 电视剧整季匹配 if need_tvs: # 先把整季缺失的拿出来,看是否刚好有所有季都满足的种子 - need_seasons = {} + need_seasons: Dict[str, list] = {} for need_tmdbid, need_tv in need_tvs.items(): for tv in need_tv: if not tv: @@ -223,6 +223,7 @@ class CommonChain(ChainBase): continue else: download_id = __download(context) + if download_id: # 更新仍需季集 need_season = __update_seasons(tmdbid=need_tmdbid, diff --git a/app/chain/douban_sync.py b/app/chain/douban_sync.py index 4acefd2e..514529cc 100644 --- a/app/chain/douban_sync.py +++ b/app/chain/douban_sync.py @@ -83,8 +83,8 @@ class DoubanSyncChain(ChainBase): logger.warn(f'{mediainfo.get_title_string()} 未搜索到资源') continue # 自动下载 - _, lefts = self.common.batch_download(contexts=contexts, need_tvs=no_exists) - if not lefts: + downloads, lefts = self.common.batch_download(contexts=contexts, need_tvs=no_exists) + if downloads and not lefts: # 全部下载完成 logger.info(f'{mediainfo.get_title_string()} 下载完成') else: diff --git a/app/chain/subscribe.py b/app/chain/subscribe.py index fdebba6a..25bab402 100644 --- a/app/chain/subscribe.py +++ b/app/chain/subscribe.py @@ -105,8 +105,8 @@ class SubscribeChain(ChainBase): logger.warn(f'{subscribe.keyword or subscribe.name} 未搜索到资源') continue # 自动下载 - _, lefts = self.common.batch_download(contexts=contexts, need_tvs=no_exists) - if not lefts: + downloads, lefts = self.common.batch_download(contexts=contexts, need_tvs=no_exists) + if downloads and not lefts: # 全部下载完成 logger.info(f'{mediainfo.get_title_string()} 下载完成,完成订阅') self.subscribes.delete(subscribe.id) @@ -195,8 +195,8 @@ class SubscribeChain(ChainBase): logger(f'{mediainfo.get_title_string()} 匹配完成,共匹配到{len(_match_context)}个资源') if _match_context: # 批量择优下载 - _, lefts = self.common.batch_download(contexts=_match_context, need_tvs=no_exists) - if not lefts: + downloads, lefts = self.common.batch_download(contexts=_match_context, need_tvs=no_exists) + if downloads and not lefts: # 全部下载完成 logger.info(f'{mediainfo.get_title_string()} 下载完成,完成订阅') self.subscribes.delete(subscribe.id) diff --git a/app/chain/user_message.py b/app/chain/user_message.py index 0734304b..c91e654b 100644 --- a/app/chain/user_message.py +++ b/app/chain/user_message.py @@ -128,7 +128,24 @@ class UserMessageChain(ChainBase): userid=userid) return # 批量下载 - self.common.batch_download(contexts=cache_list, need_tvs=no_exists, userid=userid) + downloads, lefts = self.common.batch_download(contexts=cache_list, + need_tvs=no_exists, + userid=userid) + if downloads and not lefts: + # 全部下载完成 + logger.info(f'{self._current_media.get_title_string()} 下载完成') + else: + # 未完成下载 + logger.info(f'{self._current_media.get_title_string()} 未下载未完整,添加订阅 ...') + # 添加订阅 + state, msg = self.subscribes.add(self._current_media, + season=self._current_meta.begin_season) + if state: + # 订阅成功 + self.common.post_message( + title=f"{self._current_media.get_title_string()} 已添加订阅", + text=f"用户:{userid}", + image=self._current_media.get_message_image()) else: # 下载种子 torrent: TorrentInfo = cache_list[int(text) - 1] diff --git a/app/core/context.py b/app/core/context.py index 6d818e45..cec25d41 100644 --- a/app/core/context.py +++ b/app/core/context.py @@ -279,7 +279,7 @@ class MediaInfo(object): # 本体 self.douban_info = info # 豆瓣ID - self.douban_id = info.get("id") + self.douban_id = str(info.get("id")) # 评分 if not self.vote_average: rating = info.get('rating') @@ -350,7 +350,7 @@ class MediaInfo(object): 返回背景图片地址 """ if self.backdrop_path: - return self.backdrop_path + return self.backdrop_path.replace("original", "w500") return default or "" def get_message_image(self, default: bool = None): @@ -358,7 +358,7 @@ class MediaInfo(object): 返回消息图片地址 """ if self.backdrop_path: - return self.backdrop_path + return self.backdrop_path.replace("original", "w500") return self.get_poster_image(default=default) def get_poster_image(self, default: bool = None): @@ -366,7 +366,7 @@ class MediaInfo(object): 返回海报图片地址 """ if self.poster_path: - return self.poster_path + return self.poster_path.replace("original", "w500") return default or "" def get_title_string(self): diff --git a/app/modules/telegram/telegram.py b/app/modules/telegram/telegram.py index 9a71da32..895a21e9 100644 --- a/app/modules/telegram/telegram.py +++ b/app/modules/telegram/telegram.py @@ -167,8 +167,6 @@ class Telegram(metaclass=Singleton): # 发送图文消息 if image: - # 转换TMDB图片质量 - image = image.replace("original", "w500") res = request.get_res("https://api.telegram.org/bot%s/sendPhoto?" % self._telegram_token + urlencode( {"chat_id": chat_id, "photo": image, "caption": caption, "parse_mode": "Markdown"})) if __res_parse(res): diff --git a/app/modules/themoviedb/tmdb_cache.py b/app/modules/themoviedb/tmdb_cache.py index 22ebb916..5db22244 100644 --- a/app/modules/themoviedb/tmdb_cache.py +++ b/app/modules/themoviedb/tmdb_cache.py @@ -143,7 +143,7 @@ class TmdbCache(metaclass=Singleton): if cache_year: cache_year = cache_year[:4] self._meta_data[self.__get_key(meta)] = { - "id": info.get("id"), + "id": str(info.get("id")), "type": info.get("media_type"), "year": cache_year, "title": cache_title, diff --git a/tests/run.py b/tests/run.py index 8ddc8438..9dcb2032 100644 --- a/tests/run.py +++ b/tests/run.py @@ -15,13 +15,13 @@ if __name__ == '__main__': # 测试名称识别 suite.addTest(MetaInfoTest('test_metainfo')) # 测试媒体识别 - suite.addTest(RecognizeTest('test_recognize')) + # suite.addTest(RecognizeTest('test_recognize')) # 测试CookieCloud同步 - suite.addTest(CookieCloudTest('test_cookiecloud')) + # suite.addTest(CookieCloudTest('test_cookiecloud')) # 测试文件转移 # suite.addTest(TransferTest('test_transfer')) # 测试豆瓣同步 - # suite.addTest(DoubanSyncTest('test_doubansync')) + suite.addTest(DoubanSyncTest('test_doubansync')) # 运行测试 runner = unittest.TextTestRunner() diff --git a/tests/test_doubansync.py b/tests/test_doubansync.py index a6a94890..a104b97c 100644 --- a/tests/test_doubansync.py +++ b/tests/test_doubansync.py @@ -12,6 +12,6 @@ class DoubanSyncTest(TestCase): def tearDown(self) -> None: pass - def test_doubansync(self): - result = DoubanSyncChain().process() - self.assertTrue(result[0]) + @staticmethod + def test_doubansync(): + DoubanSyncChain().process()