diff --git a/app/chain/download.py b/app/chain/download.py index f20f0c59..7f9cf7ad 100644 --- a/app/chain/download.py +++ b/app/chain/download.py @@ -177,10 +177,10 @@ class DownloadChain(ChainBase): """ if not no_exists.get(tmdbid): return 0 - for nt in no_exists.get(tmdbid): - if season == nt.get("season"): - return nt.get("total_episodes") - return 0 + no_exist = no_exists.get(tmdbid) + if not no_exist.get(season): + return 0 + return no_exist[season].total_episodes # 分组排序 contexts = TorrentHelper().sort_group_torrents(contexts) @@ -192,110 +192,157 @@ class DownloadChain(ChainBase): # 电视剧整季匹配 if no_exists: - # 先把整季缺失的拿出来,看是否刚好有所有季都满足的种子 + # 先把整季缺失的拿出来,看是否刚好有所有季都满足的种子 {tmdbid: [seasons]} need_seasons: Dict[int, list] = {} for need_tmdbid, need_tv in no_exists.items(): for tv in need_tv.values(): if not tv: continue + # 季列表为空的,代表全季缺失 if not tv.episodes: if not need_seasons.get(need_tmdbid): need_seasons[need_tmdbid] = [] need_seasons[need_tmdbid].append(tv.season or 1) # 查找整季包含的种子,只处理整季没集的种子或者是集数超过季的种子 for need_tmdbid, need_season in need_seasons.items(): + # 循环种子 for context in contexts: + # 媒体信息 media = context.media_info + # 识别元数据 meta = context.meta_info + # 种子信息 torrent = context.torrent_info + # 排除电视剧 if media.type != MediaType.TV: continue - item_season = meta.season_list + # 种子的季清单 + torrent_season = meta.season_list + # 种子有集的不要 if meta.episode_list: continue + # 匹配TMDBID if need_tmdbid == media.tmdb_id: - if set(item_season).issubset(set(need_season)): - if len(item_season) == 1: + # 种子季是需要季或者子集 + if set(torrent_season).issubset(set(need_season)): + if len(torrent_season) == 1: # 只有一季的可能是命名错误,需要打开种子鉴别,只有实际集数大于等于总集数才下载 torrent_path, torrent_files = __download_torrent(torrent) if not torrent_path: continue torrent_episodes = self.torrent.get_torrent_episodes(torrent_files) if not torrent_episodes \ - or len(torrent_episodes) >= __get_season_episodes(need_tmdbid, item_season[0]): + or len(torrent_episodes) >= __get_season_episodes(need_tmdbid, + torrent_season[0]): + # 下载 download_id = __download(_context=context, _torrent_file=torrent_path) else: logger.info( - f"种子 {meta.org_string} 未含集数信息,解析文件数为 {len(torrent_episodes)}") + f"{meta.org_string} 解析文件集数为 {len(torrent_episodes)},未含所需集数") continue else: + # 下载 download_id = __download(context) if download_id: # 更新仍需季集 need_season = __update_seasons(_tmdbid=need_tmdbid, _need=need_season, - _current=item_season) + _current=torrent_season) # 电视剧季内的集匹配 if no_exists: + # TMDBID列表 need_tv_list = list(no_exists) for need_tmdbid in need_tv_list: + # dict[season, [NotExistMediaInfo]] need_tv = no_exists.get(need_tmdbid) if not need_tv: continue + # 循环每一季 for sea, tv in need_tv.items(): - need_season = tv.season or 1 + # 当前需要季 + need_season = sea + # 当前需要集 need_episodes = tv.episodes + # TMDB总集数 total_episodes = tv.total_episodes + # 需要开始集 start_episode = tv.start_episode or 1 # 缺失整季的转化为缺失集进行比较 if not need_episodes: - need_episodes = list(range(start_episode, total_episodes + start_episode)) + need_episodes = list(range(start_episode, total_episodes)) + # 循环种子 for context in contexts: + # 媒体信息 media = context.media_info + # 识别元数据 meta = context.meta_info + # 非剧集不处理 if media.type != MediaType.TV: continue + # 匹配TMDB if media.tmdb_id == need_tmdbid: + # 不重复添加 if context in downloaded_list: continue + # 种子季 + torrent_season = meta.season_list # 只处理单季含集的种子 - item_season = meta.season_list - if len(item_season) != 1 or item_season[0] != need_season: + if len(torrent_season) != 1 or torrent_season[0] != need_season: continue - item_episodes = meta.episode_list - if not item_episodes: + # 种子集列表 + torrent_episodes = meta.episode_list + # 整季的不处理 + if not torrent_episodes: continue # 为需要集的子集则下载 - if set(item_episodes).issubset(set(need_episodes)): + if set(torrent_episodes).issubset(set(need_episodes)): + # 下载 download_id = __download(context) if download_id: # 更新仍需集数 need_episodes = __update_episodes(_tmdbid=need_tmdbid, _need=need_episodes, _sea=need_season, - _current=item_episodes) + _current=torrent_episodes) # 仍然缺失的剧集,从整季中选择需要的集数文件下载,仅支持QB和TR if no_exists: - need_tv_list = list(no_exists) - for need_tmdbid in need_tv_list: + # TMDBID列表 + no_exists_list = list(no_exists) + for need_tmdbid in no_exists_list: + # dict[season, [NotExistMediaInfo]] need_tv = no_exists.get(need_tmdbid) if not need_tv: continue - for sea, tv in need_tv.items(): - need_season = tv.season or 1 + # 需要季列表 + need_tv_list = list(need_tv) + # 循环需要季 + for sea in need_tv_list: + # NotExistMediaInfo + tv = need_tv.get(sea) + # 当前需要季 + need_season = sea + # 当前需要集 need_episodes = tv.episodes + # 没有集的不处理 if not need_episodes: continue + # 循环种子 for context in contexts: + # 媒体信息 media = context.media_info + # 识别元数据 meta = context.meta_info + # 种子信息 torrent = context.torrent_info + # 非剧集不处理 if media.type != MediaType.TV: continue + # 不重复添加 if context in downloaded_list: continue + # 没有需要集后退出 if not need_episodes: break # 选中一个单季整季的或单季包括需要的所有集的 diff --git a/app/helper/torrent.py b/app/helper/torrent.py index 2a98419b..8f6f6648 100644 --- a/app/helper/torrent.py +++ b/app/helper/torrent.py @@ -114,7 +114,7 @@ class TorrentHelper: # 返回失败 return None, None, "", [], "" elif req is None: - return None, None, "", [], "无法打开链接:%s" % url + return None, None, "", [], f"无法打开链接:{url}" elif req.status_code == 429: return None, None, "", [], "触发站点流控,请稍后重试" else: @@ -139,6 +139,7 @@ class TorrentHelper: file_list = [torrentinfo.name] else: file_list = [fileinfo.name for fileinfo in torrentinfo.files] + logger.info(f"{torrent_path.stem} -> 目录:{folder_name},文件清单:{file_list}") return folder_name, file_list except Exception as err: logger.error(f"种子文件解析失败:{err}") @@ -225,6 +226,8 @@ class TorrentHelper: """ episodes = [] for file in files: + if not file: + continue if Path(file).suffix not in settings.RMT_MEDIAEXT: continue meta = MetaInfo(file) diff --git a/app/modules/subtitle/__init__.py b/app/modules/subtitle/__init__.py index 00ce7980..b6a04d64 100644 --- a/app/modules/subtitle/__init__.py +++ b/app/modules/subtitle/__init__.py @@ -82,7 +82,7 @@ class SubtitleModule(_ModuleBase): ret = request.get_res(sublink) if ret and ret.status_code == 200: # 创建目录 - if not download_dir.exists(): + if download_dir.is_dir() and not download_dir.exists(): download_dir.mkdir(parents=True, exist_ok=True) # 保存ZIP file_name = TorrentHelper.get_url_filename(ret, sublink) @@ -100,7 +100,10 @@ class SubtitleModule(_ModuleBase): shutil.unpack_archive(zip_file, zip_path, format='zip') # 遍历转移文件 for sub_file in SystemUtils.list_files_with_extensions(zip_path, settings.RMT_SUBEXT): - target_sub_file = download_dir / sub_file.name + if download_dir.is_dir(): + target_sub_file = download_dir / sub_file.name + else: + target_sub_file = download_dir.with_name(sub_file.name) if target_sub_file.exists(): logger.info(f"字幕文件已存在:{target_sub_file}") continue @@ -116,7 +119,10 @@ class SubtitleModule(_ModuleBase): sub_file = settings.TEMP_PATH / file_name # 保存 sub_file.write_bytes(ret.content) - target_sub_file = download_dir / sub_file.name + if download_dir.is_dir(): + target_sub_file = download_dir / sub_file.name + else: + target_sub_file = download_dir.with_name(sub_file.name) logger.info(f"转移字幕 {sub_file} 到 {target_sub_file}") SystemUtils.copy(sub_file, target_sub_file) else: