缓存 IA item.exists 的结果,加快二次查询

让 uploader 在 videos_basepath 不存在时不 fail
来点小小的饼干震撼
This commit is contained in:
yzqzss 2023-06-09 14:05:35 +08:00
parent 2e5c86f33e
commit f718f2c338
5 changed files with 33 additions and 4 deletions

View File

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

View File

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

View File

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

View File

@ -1 +1 @@
BILI_ARCHIVER_VERSION = '0.0.12'
BILI_ARCHIVER_VERSION = '0.0.13'

View File

@ -1,6 +1,6 @@
[tool.poetry]
name = "biliarchiver"
version = "0.0.12"
version = "0.0.13"
description = ""
authors = ["yzqzss <yzqzss@yandex.com>"]
readme = "README.md"