|
1 | 1 | import os
|
2 | 2 | import click
|
| 3 | +import shutil |
3 | 4 | import asyncio
|
4 | 5 | from pathlib import Path
|
5 | 6 | from rich import print
|
|
20 | 21 | help=_("尝试继续下载未完成的视频"),
|
21 | 22 | )
|
22 | 23 | @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 | +) |
23 | 27 | @click.option(
|
24 | 28 | "--collection", "-c", default="opensource_movies", help=_("欲上传至的 collection")
|
25 | 29 | )
|
|
31 | 35 | default=10,
|
32 | 36 | help=_("最小剩余空间 (GB),少于此值时将中止下载"),
|
33 | 37 | )
|
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 | +): |
35 | 47 | """清理命令主函数"""
|
36 | 48 | if all:
|
37 |
| - try_upload = try_download = clean_locks = True |
| 49 | + try_upload = try_download = clean_locks = clean_uploaded = True |
38 | 50 |
|
39 |
| - if not any([try_upload, try_download, clean_locks]): |
| 51 | + if not any([try_upload, try_download, clean_locks, clean_uploaded]): |
40 | 52 | print(_("请指定至少一项清理操作,或使用 --all/-a 执行所有清理操作"))
|
41 | 53 | return
|
42 |
| - |
| 54 | + |
43 | 55 | from biliarchiver.config import config
|
44 | 56 |
|
45 | 57 | # 检查磁盘空间
|
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) |
47 | 60 | print(_("当前剩余磁盘空间: {:.2f} GB").format(free_space_gb))
|
48 | 61 |
|
49 | 62 | # 清理锁文件
|
@@ -79,12 +92,33 @@ def clean(try_upload, try_download, clean_locks, collection, all, min_free_space
|
79 | 92 | bvids_to_download.append(bvid)
|
80 | 93 | continue
|
81 | 94 |
|
| 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 | + |
82 | 110 | # 下载完成,检查是否需要上传
|
83 | 111 | if try_upload:
|
84 | 112 | process_finished_download(video_dir, bvid, collection)
|
85 | 113 |
|
| 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 | + |
86 | 119 | # 执行下载
|
87 | 120 | if try_download and bvids_to_download:
|
| 121 | + free_space_gb = get_free_space(config.storage_home_dir) / (1024 * 1024 * 1024) |
88 | 122 | if free_space_gb < min_free_space_gb:
|
89 | 123 | print(_("剩余空间不足 {} GB,跳过下载操作").format(min_free_space_gb))
|
90 | 124 | else:
|
@@ -148,6 +182,7 @@ def process_finished_download(video_dir, bvid, collection):
|
148 | 182 | if has_parts_to_upload:
|
149 | 183 | print(_("尝试上传 {}").format(bvid))
|
150 | 184 | from biliarchiver._biliarchiver_upload_bvid import upload_bvid
|
| 185 | + |
151 | 186 | try:
|
152 | 187 | upload_bvid(
|
153 | 188 | bvid,
|
|
0 commit comments