diff --git a/biliarchiver/_biliarchiver_upload_bvid.py b/biliarchiver/_biliarchiver_upload_bvid.py index b9b5aa0..3530fab 100644 --- a/biliarchiver/_biliarchiver_upload_bvid.py +++ b/biliarchiver/_biliarchiver_upload_bvid.py @@ -5,6 +5,7 @@ import time from internetarchive import get_item from requests import Response from rich import print +from biliarchiver.exception import VideosBasePathNotFoundError from biliarchiver.utils.string import human_readable_upper_part_map from biliarchiver.config import BILIBILI_IDENTIFIER_PERFIX, config @@ -19,6 +20,8 @@ def upload_bvid(bvid): _upload_bvid(bvid) except AlreadyRunningError: print(f'已经有一个上传 {bvid} 的进程在运行,跳过') + except VideosBasePathNotFoundError: + print(f'没有找到 {bvid} 对应的文件夹。可能是因已存在 IA item 而跳过了下载,或者你传入了错误的 bvid') except Exception as e: print(f'上传 {bvid} 时出错:') raise e @@ -30,9 +33,13 @@ def _upload_bvid(bvid: str): upper_part = human_readable_upper_part_map(string=bvid, backward=True) OLD_videos_basepath: Path = config.storage_home_dir / 'videos' / bvid videos_basepath: Path = config.storage_home_dir / 'videos' / f'{bvid}-{upper_part}' + if os.path.exists(OLD_videos_basepath): print(f'检测到旧的视频主目录 {OLD_videos_basepath},将其重命名为 {videos_basepath}...') os.rename(OLD_videos_basepath, videos_basepath) + + if not os.path.exists(videos_basepath): + raise VideosBasePathNotFoundError(f'{videos_basepath}') for local_identifier in os.listdir(videos_basepath): remote_identifier = f'{local_identifier}-{upper_part}' if os.path.exists(f'{videos_basepath}/{local_identifier}/_uploaded.mark'): diff --git a/biliarchiver/bili_archive_bvids.py b/biliarchiver/bili_archive_bvids.py index 7a988ea..5034642 100644 --- a/biliarchiver/bili_archive_bvids.py +++ b/biliarchiver/bili_archive_bvids.py @@ -39,6 +39,19 @@ def parse_args(): return args def check_ia_item_exist(client: Client, identifier: str) -> bool: + cache_dir = config.storage_home_dir / 'ia_item_exist_cache' + cache_dir.mkdir(parents=True, exist_ok=True) + def create_item_exist_cache_file(identifier: str) -> Path: + with open(cache_dir / f'{identifier}.mark', 'w', encoding='utf-8') as f: + f.write('') + return cache_dir / f'{identifier}.mark' + def check_ia_item_exist_from_cache_file(identifier: str) -> bool: + return (cache_dir / f'{identifier}.mark').exists() + + + if check_ia_item_exist_from_cache_file(identifier): + return True + # params = { # 'identifier': identifier, # 'output': 'json', @@ -55,6 +68,7 @@ def check_ia_item_exist(client: Client, identifier: str) -> bool: # raise ValueError(f'Unexpected code: {r_json["code"]}') item = get_item(identifier) if item.exists: + create_item_exist_cache_file(identifier) return True return False @@ -135,9 +149,11 @@ def is_login(cilent: Client) -> bool: r.raise_for_status() nav_json = r.json() if nav_json['code'] == 0: - print('用户登录成功') + print('BiliBili 登录成功,饼干真香。') + print('NOTICE: 存档过程中请不要在 cookies 的源浏览器访问 B 站,避免 B 站刷新' + ' cookies 导致我们半路全下到的视频全是 480P 的优酷土豆级醇享画质。') return True - print('未登录/SESSDATA无效/过期') + print('未登录/SESSDATA无效/过期,你这饼干它保真吗?') return False def main(): diff --git a/biliarchiver/exception.py b/biliarchiver/exception.py new file mode 100644 index 0000000..7a32e1c --- /dev/null +++ b/biliarchiver/exception.py @@ -0,0 +1,6 @@ +class VideosBasePathNotFoundError(FileNotFoundError): + def __init__(self, path: str): + self.path = path + + def __str__(self): + return f"Videos base path {self.path} not found" \ No newline at end of file diff --git a/biliarchiver/version.py b/biliarchiver/version.py index 6326921..5037d48 100644 --- a/biliarchiver/version.py +++ b/biliarchiver/version.py @@ -1 +1 @@ -BILI_ARCHIVER_VERSION = '0.0.12' \ No newline at end of file +BILI_ARCHIVER_VERSION = '0.0.13' \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml index 5207057..2b267d1 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "biliarchiver" -version = "0.0.12" +version = "0.0.13" description = "" authors = ["yzqzss "] readme = "README.md"