This commit is contained in:
jxxghp 2023-06-08 18:47:04 +08:00
parent 298df1fbf8
commit a4a391f238
9 changed files with 44 additions and 28 deletions

View File

@ -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,

View File

@ -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:

View File

@ -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)

View File

@ -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]

View File

@ -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):

View File

@ -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):

View File

@ -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,

View File

@ -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()

View File

@ -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()