Skip to content

Commit a0fd8f2

Browse files
committed
feat: add option to clean uploaded videos in clean command
1 parent bd2b68d commit a0fd8f2

File tree

1 file changed

+40
-5
lines changed

1 file changed

+40
-5
lines changed

biliarchiver/cli_tools/clean_command.py

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import os
22
import click
3+
import shutil
34
import asyncio
45
from pathlib import Path
56
from rich import print
@@ -20,6 +21,9 @@
2021
help=_("尝试继续下载未完成的视频"),
2122
)
2223
@click.option("--clean-locks", "-l", is_flag=True, default=False, help=_("清理锁文件"))
24+
@click.option(
25+
"--clean-uploaded", "-cu", is_flag=True, default=False, help=_("清理已上传的视频")
26+
)
2327
@click.option(
2428
"--collection", "-c", default="opensource_movies", help=_("欲上传至的 collection")
2529
)
@@ -31,19 +35,28 @@
3135
default=10,
3236
help=_("最小剩余空间 (GB),少于此值时将中止下载"),
3337
)
34-
def clean(try_upload, try_download, clean_locks, collection, all, min_free_space_gb):
38+
def clean(
39+
try_upload,
40+
try_download,
41+
clean_locks,
42+
clean_uploaded,
43+
collection,
44+
all,
45+
min_free_space_gb,
46+
):
3547
"""清理命令主函数"""
3648
if all:
37-
try_upload = try_download = clean_locks = True
49+
try_upload = try_download = clean_locks = clean_uploaded = True
3850

39-
if not any([try_upload, try_download, clean_locks]):
51+
if not any([try_upload, try_download, clean_locks, clean_uploaded]):
4052
print(_("请指定至少一项清理操作,或使用 --all/-a 执行所有清理操作"))
4153
return
42-
54+
4355
from biliarchiver.config import config
4456

4557
# 检查磁盘空间
46-
free_space_gb = get_free_space(config.storage_home_dir) / (1024 * 1024 * 1024)
58+
free_space_before = get_free_space(config.storage_home_dir)
59+
free_space_gb = free_space_before / (1024 * 1024 * 1024)
4760
print(_("当前剩余磁盘空间: {:.2f} GB").format(free_space_gb))
4861

4962
# 清理锁文件
@@ -79,12 +92,33 @@ def clean(try_upload, try_download, clean_locks, collection, all, min_free_space
7992
bvids_to_download.append(bvid)
8093
continue
8194

95+
# 如果启用了清理已上传视频选项,检查所有分P是否已上传
96+
if clean_uploaded:
97+
all_parts_uploaded = True
98+
for part_dir in video_dir.iterdir():
99+
if not part_dir.is_dir():
100+
continue
101+
if not (part_dir / "_uploaded.mark").exists():
102+
all_parts_uploaded = False
103+
break
104+
105+
if all_parts_uploaded and video_dir.exists():
106+
print(_("清理已上传的视频: {}").format(bvid))
107+
shutil.rmtree(video_dir, ignore_errors=True)
108+
continue
109+
82110
# 下载完成,检查是否需要上传
83111
if try_upload:
84112
process_finished_download(video_dir, bvid, collection)
85113

114+
if clean_uploaded or try_upload:
115+
free_space_after = get_free_space(config.storage_home_dir)
116+
space_freed = free_space_after - free_space_before
117+
print(_("共释放 {:.2f} MiB 空间").format(space_freed / (1024 * 1024)))
118+
86119
# 执行下载
87120
if try_download and bvids_to_download:
121+
free_space_gb = get_free_space(config.storage_home_dir) / (1024 * 1024 * 1024)
88122
if free_space_gb < min_free_space_gb:
89123
print(_("剩余空间不足 {} GB,跳过下载操作").format(min_free_space_gb))
90124
else:
@@ -148,6 +182,7 @@ def process_finished_download(video_dir, bvid, collection):
148182
if has_parts_to_upload:
149183
print(_("尝试上传 {}").format(bvid))
150184
from biliarchiver._biliarchiver_upload_bvid import upload_bvid
185+
151186
try:
152187
upload_bvid(
153188
bvid,

0 commit comments

Comments
 (0)