feat: delete after upload

This commit is contained in:
OverflowCat 2023-08-25 22:37:57 +08:00
parent ce4f21789e
commit c18b2a4517
5 changed files with 79 additions and 12 deletions

View File

@ -7,6 +7,10 @@ from urllib.parse import urlparse
from internetarchive import get_item
from requests import Response
from rich import print
from pathlib import Path
from shutil import rmtree
from biliarchiver.i18n import _
from biliarchiver.exception import (
VideosBasePathNotFoundError,
VideosNotFinishedDownloadError,
@ -20,12 +24,23 @@ from biliarchiver.version import BILI_ARCHIVER_VERSION
from biliarchiver.i18n import _
def upload_bvid(bvid: str, *, update_existing: bool = False, collection: str):
def upload_bvid(
bvid: str,
*,
update_existing: bool = False,
collection: str,
delete_after_upload: bool = False,
):
try:
lock_dir = config.storage_home_dir / ".locks" / bvid
os.makedirs(lock_dir, exist_ok=True)
lock_dir.mkdir(parents=True, exist_ok=True)
with UploadLock(lock_dir): # type: ignore
_upload_bvid(bvid, update_existing=update_existing, collection=collection)
_upload_bvid(
bvid,
update_existing=update_existing,
collection=collection,
delete_after_upload=delete_after_upload,
)
except AlreadyRunningError:
print(_("已经有一个上传 {} 的进程在运行,跳过".format(bvid)))
except VideosBasePathNotFoundError:
@ -37,7 +52,13 @@ def upload_bvid(bvid: str, *, update_existing: bool = False, collection: str):
raise e
def _upload_bvid(bvid: str, *, update_existing: bool = False, collection: str):
def _upload_bvid(
bvid: str,
*,
update_existing: bool = False,
collection: str,
delete_after_upload: bool = False,
):
access_key, secret_key = read_ia_keys(config.ia_key_file)
# identifier format: BiliBili-{bvid}_p{pid}-{upper_part}
@ -54,7 +75,8 @@ def _upload_bvid(bvid: str, *, update_existing: bool = False, collection: str):
if not (videos_basepath / "_all_downloaded.mark").exists():
raise VideosNotFinishedDownloadError(f"{videos_basepath}")
for local_identifier in os.listdir(videos_basepath):
local_identifiers = [f.name for f in videos_basepath.iterdir() if f.is_dir()]
for local_identifier in local_identifiers:
remote_identifier = f"{local_identifier}-{upper_part}"
if (
os.path.exists(f"{videos_basepath}/{local_identifier}/_uploaded.mark")
@ -271,6 +293,18 @@ def _upload_bvid(bvid: str, *, update_existing: bool = False, collection: str):
f.write("")
print(f"==== {remote_identifier} {_('上传完成')} ====")
if delete_after_upload and len(local_identifiers) > 0:
try:
for local_identifier in local_identifiers:
rmtree(f"{videos_basepath}/{local_identifier}")
print(
"[yellow]"
+ _("已删除视频文件夹 {}").format(", ".join(local_identifiers))
+ "[/yellow]"
)
except Exception as e:
print(e)
def read_ia_keys(keysfile):
"""Return: tuple(`access_key`, `secret_key`)"""

View File

@ -5,7 +5,11 @@ from biliarchiver.i18n import _
@click.command(help=click.style(_("从哔哩哔哩下载"), fg="cyan"))
@click.option(
"--bvids", type=click.STRING, required=True, help=_("空白字符分隔的 bvids 列表(记得加引号),或文件路径")
"--bvids",
"-i",
type=click.STRING,
required=True,
help=_("空白字符分隔的 bvids 列表(记得加引号),或文件路径"),
)
@click.option(
"--skip-ia-check",

View File

@ -26,9 +26,10 @@ BILIBILI_VIDEOS_SUB_1_COLLECTION = "bilibili_videos_sub_1"
@click.command(help=click.style(_("上传至互联网档案馆"), fg="cyan"))
@click.option("--bvids", type=click.STRING, default=None, help=_("bvids 列表的文件路径"))
@click.option("--bvids", "-i", type=click.STRING, default=None, help=_("bvids 列表的文件路径"))
@click.option(
"--by-storage-home-dir",
"-a",
is_flag=True,
default=False,
help=_("使用 `$storage_home_dir/videos` 目录下的所有视频"),
@ -36,6 +37,7 @@ BILIBILI_VIDEOS_SUB_1_COLLECTION = "bilibili_videos_sub_1"
@click.option("--update-existing", is_flag=True, default=False, help=_("更新已存在的 item"))
@click.option(
"--collection",
"-c",
default=DEFAULT_COLLECTION,
type=click.Choice(
[
@ -47,23 +49,38 @@ BILIBILI_VIDEOS_SUB_1_COLLECTION = "bilibili_videos_sub_1"
help=_("欲上传至的 collection. (非默认值仅限 collection 管理员使用)")
+ f" [default: {DEFAULT_COLLECTION}]",
)
@click.option(
"--delete-after-upload",
"-d",
is_flag=True,
default=False,
help=_("上传后删除视频文件"),
)
def up(
bvids: TextIOWrapper,
by_storage_home_dir: bool,
update_existing: bool,
collection: str,
delete_after_upload: bool,
):
from biliarchiver._biliarchiver_upload_bvid import upload_bvid
from biliarchiver.config import config
ids = []
if by_storage_home_dir:
for bvid_with_upper_part in os.listdir(config.storage_home_dir / "videos"):
bvid = bvid_with_upper_part
if "-" in bvid_with_upper_part:
bvid = bvid_with_upper_part.split("-")[0]
upload_bvid(bvid, update_existing=update_existing, collection=collection)
ids.append(bvid)
elif bvids:
for bvid in read_bvids(bvids):
upload_bvid(bvid, update_existing=update_existing, collection=collection)
ids = read_bvids(bvids)
for id in ids:
upload_bvid(
id,
update_existing=update_existing,
collection=collection,
delete_after_upload=delete_after_upload,
)

View File

@ -425,3 +425,9 @@ msgstr ""
#: biliarchiver/_biliarchiver_upload_bvid.py:80
msgid "没有下载完成"
msgstr ""
msgid "上传后删除视频文件"
msgstr ""
msgid "已删除视频文件夹 {}"
msgstr ""

View File

@ -420,3 +420,9 @@ msgstr "{} is not the correct local_identifier that starts with {}"
#: biliarchiver/_biliarchiver_upload_bvid.py:80
msgid "没有下载完成"
msgstr "Download not finished"
msgid "上传后删除视频文件"
msgstr "Delete video files after upload"
msgid "已删除视频文件夹 {}"
msgstr "Deleted video folder {}"