This commit is contained in:
yzqzss 2023-06-09 03:42:21 +08:00
parent 44da6f4749
commit 2e5c86f33e
4 changed files with 42 additions and 29 deletions

View File

@ -1,9 +1,13 @@
import asyncio
import os
import argparse
from pathlib import Path
from typing import Union
from internetarchive import get_item
from biliarchiver.archive_bvid import archive_bvid
from biliarchiver.config import Config
from biliarchiver.config import config
from bilix.sites.bilibili.downloader import DownloaderBilibili
from rich.console import Console
@ -20,14 +24,12 @@ from dataclasses import dataclass
@dataclass
class Args:
cookies: str
bvids: str
skip_ia: bool
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('--cookies', dest='cookies', type=str, default='~/.cookies.txt')
parser.add_argument('--bvids', dest='bvids', type=str, help='bvids 列表的文件路径', required=True)
parser.add_argument('-s', '--skip-ia-check', dest='skip_ia', action='store_true',
help='不检查 IA 上是否已存在对应 BVID 的 item ,直接开始下载')
@ -37,20 +39,25 @@ def parse_args():
return args
def check_ia_item_exist(client: Client, identifier: str) -> bool:
params = {
'identifier': identifier,
'output': 'json',
}
r = client.get('https://archive.org/services/check_identifier.php' ,params=params)
r.raise_for_status()
r_json = r.json()
assert r_json['type'] =='success'
if r_json['code'] == 'available':
return False
elif r_json['code'] == 'not_available':
# params = {
# 'identifier': identifier,
# 'output': 'json',
# }
# r = client.get('https://archive.org/services/check_identifier.php' ,params=params)
# r.raise_for_status()
# r_json = r.json()
# assert r_json['type'] =='success'
# if r_json['code'] == 'available':
# return False
# elif r_json['code'] == 'not_available':
# return True
# else:
# raise ValueError(f'Unexpected code: {r_json["code"]}')
item = get_item(identifier)
if item.exists:
return True
else:
raise ValueError(f'Unexpected code: {r_json["code"]}')
return False
def _main():
args = parse_args()
@ -60,8 +67,6 @@ def _main():
with open(args.bvids, 'r', encoding='utf-8') as f:
bvids_from_file = f.read().splitlines()
config = Config()
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
@ -70,7 +75,7 @@ def _main():
part_concurrency=config.part_concurrency,
stream_retry=config.stream_retry,
)
update_cookies_from_file(d.client, args.cookies)
update_cookies_from_file(d.client, config.cookies_file)
client = Client(cookies=d.client.cookies, headers=d.client.headers)
logined = is_login(client)
if not logined:
@ -97,12 +102,18 @@ def _main():
def update_cookies_from_file(client: AsyncClient, cookies_path: str):
cookies_path = os.path.expanduser(cookies_path)
def update_cookies_from_file(client: AsyncClient, cookies_path: Union[str, Path]):
if isinstance(cookies_path, Path):
cookies_path = cookies_path.expanduser()
elif isinstance(cookies_path, str):
cookies_path = Path(cookies_path).expanduser()
else:
raise TypeError(f'cookies_path: {type(cookies_path)}')
assert os.path.exists(cookies_path), f'cookies 文件不存在: {cookies_path}'
from http.cookiejar import MozillaCookieJar
cj = MozillaCookieJar()
cj.load(cookies_path, ignore_discard=True, ignore_expires=True)
cj.load(f'{cookies_path}', ignore_discard=True, ignore_expires=True)
loadded_cookies = 0
for cookie in cj:
# only load bilibili cookies
@ -112,9 +123,9 @@ def update_cookies_from_file(client: AsyncClient, cookies_path: str):
cookie.name, cookie.value, domain=cookie.domain, path=cookie.path
)
loadded_cookies += 1
print(f'{cookies_path} 加载{loadded_cookies} 块 cookies')
print(f'{cookies_path} 品尝{loadded_cookies} 块 cookies')
if loadded_cookies > 100:
print('可能加载了过多的 cookies可能导致 httpx.Client 响应非常')
print('吃了过多的 cookies可能导致 httpx.Client 怠工,响应非常缓')
assert client.cookies.get('SESSDATA') is not None, 'SESSDATA 不存在'
# print(f'SESS_DATA: {client.cookies.get("SESSDATA")}')

View File

@ -16,12 +16,13 @@ class singleton(type):
@dataclass
class Config(metaclass=singleton):
class _Config(metaclass=singleton):
video_concurrency: int = 3
part_concurrency: int = 10
stream_retry: int = 20
storage_home_dir: Path = Path('bilibili_archive_dir/').expanduser()
ia_key_file: Path = Path('~/.bili_ia_keys.txt').expanduser()
cookies_file: Path = Path('~/.cookies.txt').expanduser()
def __init__(self):
self.is_right_pwd()
@ -29,7 +30,7 @@ class Config(metaclass=singleton):
print(f'{CONFIG_FILE} 不存在,创建中...')
self.save()
with open(CONFIG_FILE, 'r', encoding='utf-8') as f:
print(f'读取 {CONFIG_FILE}...')
print(f'Loading {CONFIG_FILE} ...')
config_file = json.load(f)
self.video_concurrency: int = config_file['video_concurrency']
@ -47,10 +48,11 @@ class Config(metaclass=singleton):
'stream_retry': self.stream_retry,
'storage_home_dir': str(self.storage_home_dir),
'ia_key_file': str(self.ia_key_file),
'cookies_file': str(self.cookies_file),
}, f, ensure_ascii=False, indent=4)
def is_right_pwd(self):
if not os.path.exists('biliarchiver.home'):
raise Exception('先在当前工作目录创建 biliarchiver.home 文件')
config = Config()
config = _Config()

View File

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

View File

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