|
| 1 | +import os |
| 2 | +import click |
| 3 | +import asyncio |
| 4 | +from pathlib import Path |
| 5 | +from rich import print |
| 6 | + |
| 7 | +from biliarchiver.utils.storage import get_free_space |
| 8 | +from biliarchiver.i18n import _ |
| 9 | + |
| 10 | + |
| 11 | +@click.command(help=click.style(_("清理并尝试修复未完成的任务"), fg="cyan")) |
| 12 | +@click.option( |
| 13 | + "--try-upload", "-u", is_flag=True, default=False, help=_("尝试上传下载完成的视频") |
| 14 | +) |
| 15 | +@click.option( |
| 16 | + "--try-download", |
| 17 | + "-d", |
| 18 | + is_flag=True, |
| 19 | + default=False, |
| 20 | + help=_("尝试继续下载未完成的视频"), |
| 21 | +) |
| 22 | +@click.option("--clean-locks", "-l", is_flag=True, default=False, help=_("清理锁文件")) |
| 23 | +@click.option( |
| 24 | + "--collection", "-c", default="opensource_movies", help=_("欲上传至的 collection") |
| 25 | +) |
| 26 | +@click.option("--all", "-a", is_flag=True, default=False, help=_("执行所有清理操作")) |
| 27 | +@click.option( |
| 28 | + "--min-free-space-gb", |
| 29 | + "-m", |
| 30 | + type=int, |
| 31 | + default=10, |
| 32 | + help=_("最小剩余空间 (GB),少于此值时将中止下载"), |
| 33 | +) |
| 34 | +def clean(try_upload, try_download, clean_locks, collection, all, min_free_space_gb): |
| 35 | + """清理命令主函数""" |
| 36 | + if all: |
| 37 | + try_upload = try_download = clean_locks = True |
| 38 | + |
| 39 | + if not any([try_upload, try_download, clean_locks]): |
| 40 | + print(_("请指定至少一项清理操作,或使用 --all/-a 执行所有清理操作")) |
| 41 | + return |
| 42 | + |
| 43 | + from biliarchiver.config import config |
| 44 | + |
| 45 | + # 检查磁盘空间 |
| 46 | + free_space_gb = get_free_space(config.storage_home_dir) / (1024 * 1024 * 1024) |
| 47 | + print(_("当前剩余磁盘空间: {:.2f} GB").format(free_space_gb)) |
| 48 | + |
| 49 | + # 清理锁文件 |
| 50 | + if clean_locks: |
| 51 | + clean_lock_files(config) |
| 52 | + |
| 53 | + # 处理下载和上传 |
| 54 | + videos_dir = config.storage_home_dir / "videos" |
| 55 | + if not videos_dir.exists(): |
| 56 | + print(_("视频目录不存在: {}").format(videos_dir)) |
| 57 | + return |
| 58 | + |
| 59 | + bvids_to_download = [] |
| 60 | + |
| 61 | + for video_dir in videos_dir.iterdir(): |
| 62 | + if not video_dir.is_dir(): |
| 63 | + continue |
| 64 | + |
| 65 | + # 提取BVID |
| 66 | + if "-" not in video_dir.name: |
| 67 | + continue |
| 68 | + |
| 69 | + bvid = video_dir.name.split("-")[0] |
| 70 | + |
| 71 | + # 检查是否是有效的BVID |
| 72 | + if not bvid.startswith("BV"): |
| 73 | + continue |
| 74 | + |
| 75 | + # 检查下载状态 |
| 76 | + if not (video_dir / "_all_downloaded.mark").exists(): |
| 77 | + if try_download: |
| 78 | + print(_("发现未完成下载的视频: {}").format(bvid)) |
| 79 | + bvids_to_download.append(bvid) |
| 80 | + continue |
| 81 | + |
| 82 | + # 下载完成,检查是否需要上传 |
| 83 | + if try_upload: |
| 84 | + process_finished_download(video_dir, bvid, collection) |
| 85 | + |
| 86 | + # 执行下载 |
| 87 | + if try_download and bvids_to_download: |
| 88 | + if free_space_gb < min_free_space_gb: |
| 89 | + print(_("剩余空间不足 {} GB,跳过下载操作").format(min_free_space_gb)) |
| 90 | + else: |
| 91 | + download_unfinished_videos(config, bvids_to_download, min_free_space_gb) |
| 92 | + |
| 93 | + |
| 94 | +def clean_lock_files(config): |
| 95 | + """清理所有锁文件""" |
| 96 | + lock_dir = config.storage_home_dir / ".locks" |
| 97 | + if not lock_dir.exists(): |
| 98 | + print(_("锁文件目录不存在: {}").format(lock_dir)) |
| 99 | + return |
| 100 | + |
| 101 | + total_locks = 0 |
| 102 | + total_size = 0 |
| 103 | + |
| 104 | + for lock_path in lock_dir.glob("**/*"): |
| 105 | + if lock_path.is_file(): |
| 106 | + size = lock_path.stat().st_size |
| 107 | + total_size += size |
| 108 | + total_locks += 1 |
| 109 | + lock_path.unlink() |
| 110 | + |
| 111 | + # 删除空文件夹 |
| 112 | + for dirpath, dirnames, filenames in os.walk(lock_dir, topdown=False): |
| 113 | + for dirname in dirnames: |
| 114 | + full_path = Path(dirpath) / dirname |
| 115 | + if not any(full_path.iterdir()): |
| 116 | + try: |
| 117 | + full_path.rmdir() |
| 118 | + except: |
| 119 | + pass |
| 120 | + |
| 121 | + print( |
| 122 | + _("已清理 {} 个锁文件,释放 {:.2f} MiB 空间").format( |
| 123 | + total_locks, total_size / (1024 * 1024) |
| 124 | + ) |
| 125 | + ) |
| 126 | + |
| 127 | + |
| 128 | +def process_finished_download(video_dir, bvid, collection): |
| 129 | + """处理下载完成的视频目录""" |
| 130 | + # 检查是否有标记为垃圾的文件 |
| 131 | + if (video_dir / "_spam.mark").exists(): |
| 132 | + print(_("{} 已被标记为垃圾,跳过").format(bvid)) |
| 133 | + return |
| 134 | + |
| 135 | + # 检查是否有分P需要上传 |
| 136 | + has_parts_to_upload = False |
| 137 | + for part_dir in video_dir.iterdir(): |
| 138 | + if not part_dir.is_dir(): |
| 139 | + continue |
| 140 | + |
| 141 | + # 检查该分P是否下载完成但未上传 |
| 142 | + if (part_dir / "_downloaded.mark").exists() and not ( |
| 143 | + part_dir / "_uploaded.mark" |
| 144 | + ).exists(): |
| 145 | + has_parts_to_upload = True |
| 146 | + break |
| 147 | + |
| 148 | + if has_parts_to_upload: |
| 149 | + print(_("尝试上传 {}").format(bvid)) |
| 150 | + from biliarchiver._biliarchiver_upload_bvid import upload_bvid |
| 151 | + try: |
| 152 | + upload_bvid( |
| 153 | + bvid, |
| 154 | + update_existing=False, |
| 155 | + collection=collection, |
| 156 | + delete_after_upload=True, |
| 157 | + ) |
| 158 | + except Exception as e: |
| 159 | + error_str = str(e) |
| 160 | + if "appears to be spam" in error_str: |
| 161 | + print(_("{} 被检测为垃圾,标记并跳过").format(bvid)) |
| 162 | + with open(video_dir / "_spam.mark", "w", encoding="utf-8") as f: |
| 163 | + f.write(error_str) |
| 164 | + else: |
| 165 | + print(_("上传 {} 时出错: {}").format(bvid, e)) |
| 166 | + |
| 167 | + |
| 168 | +def download_unfinished_videos(config, bvids, min_free_space_gb): |
| 169 | + """尝试下载未完成的视频""" |
| 170 | + if not bvids: |
| 171 | + return |
| 172 | + |
| 173 | + # 创建临时文件保存BVID列表 |
| 174 | + temp_file = config.storage_home_dir / "_temp_bvids.txt" |
| 175 | + with open(temp_file, "w", encoding="utf-8") as f: |
| 176 | + f.write("\n".join(bvids)) |
| 177 | + |
| 178 | + print(_("尝试继续下载 {} 个未完成的视频").format(len(bvids))) |
| 179 | + |
| 180 | + # 构建参数 |
| 181 | + kwargs = { |
| 182 | + "bvids": str(temp_file), |
| 183 | + "skip_ia_check": True, |
| 184 | + "from_browser": None, |
| 185 | + "min_free_space_gb": min_free_space_gb, |
| 186 | + "skip_to": 0, |
| 187 | + "disable_version_check": False, |
| 188 | + } |
| 189 | + |
| 190 | + try: |
| 191 | + # 使用asyncio运行异步函数 |
| 192 | + from biliarchiver.cli_tools.bili_archive_bvids import _down |
| 193 | + |
| 194 | + asyncio.run(_down(**kwargs)) |
| 195 | + except Exception as e: |
| 196 | + print(_("下载过程中出错: {}").format(e)) |
| 197 | + finally: |
| 198 | + # 清理临时文件 |
| 199 | + if temp_file.exists(): |
| 200 | + temp_file.unlink() |
0 commit comments