Skip to content

armgit / peach

Public Imported from GitHub
0

Peach: Local-first video metadata scraper and media manager.

fix(junk): 垃圾永久删除跳过个人记录、已保留与带字幕的行

longmeidao926524676fbc
diff --git a/scripts/trash_junk.py b/scripts/trash_junk.py
index 57c30096d..1fd8c9360 100644
--- a/scripts/trash_junk.py
+++ b/scripts/trash_junk.py
@@ -9,6 +9,10 @@
`--purge --review-csv` 只处理清单里 decision=delete 的确认项。每项须带路径、体积、
证据与至少 0.95 的确认置信度;执行时核对文件未变化、来源在线且路径在根内,保护字幕与 NFO。
+以下几类整条跳过、不删,并在结果的 `skipped` 里逐条回报:已不在垃圾复核队列里的行、
+用户点过「不是垃圾」的行、带个人记录(播放、评分、偏好、稍后看、播放列表等,判据同
+`personal_records.record_holders`)的行,以及登记了字幕或盘上有同名字幕、NFO 的行。
+删除只走 `purge_assets()`。
用法:
python scripts/trash_junk.py --min-score 60
@@ -29,6 +33,7 @@ if str(SRC_DIR) not in sys.path:
sys.path.insert(0, str(SRC_DIR))
from peach.config import DATABASE_PATH, GENERATED_DIR, LOCATION_ROOT_DECLARATIONS
+from peach.personal_records import record_holders
from peach.platform import root_online, translate_ledger_path, within_root
from peach.review_csv import read_rows, write_rows
from peach.scripting import (
@@ -61,12 +66,49 @@ def load_review(path: Path) -> list[dict]:
return selected
-def purge_reviewed(connection, reviews: list[dict]) -> dict:
- """核对清单与文件后,复用批量永久删除的隔离、提交与清退流程。"""
+def candidate_ids(db_path: Path) -> set[int]:
+ """当前仍在垃圾复核队列(未被标「不是垃圾」)里的资产 id,只读。"""
+ result = q_ads(WebContract(Path(db_path)), limit=1_000_000)
+ return {int(item["id"]) for item in result["items"]}
+
+
+def sidecar_files(path: Path) -> list[str]:
+ """盘上与该文件同名的字幕与 NFO(`片名.srt`、`片名.zh.ass`、`片名.nfo` 等)。"""
+ prefix = path.stem.casefold() + "."
+ return sorted(entry.name for entry in path.parent.iterdir()
+ if entry != path and entry.name.casefold().startswith(prefix)
+ and entry.suffix.casefold() in MEDIA_SIDECAR_SUFFIXES)
+
+
+def skip_reason(connection, row, path: Path, candidates: set[int], holders: set[int]) -> str:
+ """不能永久删除的原因;空串表示可以删。"""
+ if connection.execute(
+ "SELECT 1 FROM review_decision WHERE category='junk_file' AND status='rejected' "
+ "AND item_key=?", (str(row["id"]),)).fetchone():
+ return "用户已标为不是垃圾"
+ if int(row["id"]) not in candidates:
+ return "已不在垃圾复核队列里"
+ if int(row["id"]) in holders:
+ return "带个人记录"
+ if connection.execute("SELECT 1 FROM asset_subtitle WHERE asset_id=?", (row["id"],)).fetchone():
+ return "登记了字幕"
+ sidecars = sidecar_files(path)
+ if sidecars:
+ return "盘上有同名字幕或资料:" + "、".join(sidecars)
+ return ""
+
+
+def purge_reviewed(connection, reviews: list[dict], candidates: set[int]) -> dict:
+ """核对清单与文件后,复用批量永久删除的隔离、提交与清退流程。
+
+ `candidates` 是删除前只读算出的垃圾复核队列 id;不在其中的行跳过。
+ """
baseline = {tuple(row) for row in connection.execute("PRAGMA foreign_key_check")}
outcome = None
+ skipped = []
try:
connection.execute("BEGIN IMMEDIATE")
+ holders = record_holders(connection, [int(review["id"]) for review in reviews])
rows = []
for review in reviews:
row = connection.execute("SELECT * FROM asset WHERE id=?", (int(review["id"]),)).fetchone()
@@ -80,6 +122,10 @@ def purge_reviewed(connection, reviews: list[dict]) -> dict:
raise ValueError("媒体资料与字幕不能进入垃圾永久删除")
if path.is_symlink() or not path.is_file() or path.stat().st_size != int(review["size"]):
raise ValueError("删除清单已失效:文件不存在、类型或体积变化")
+ reason = skip_reason(connection, row, path, candidates, holders)
+ if reason:
+ skipped.append({"id": row["id"], "path": row["path"], "reason": reason})
+ continue
rows.append(row)
outcome = purge_assets(connection, rows)
integrity, _ = verify_after_write(connection)
@@ -92,7 +138,7 @@ def purge_reviewed(connection, reviews: list[dict]) -> dict:
if outcome is not None:
_restore_staged_media(outcome["_staged"])
raise
- return {**_finish_purge(outcome), "existing_foreign_keys": len(baseline)}
+ return {**_finish_purge(outcome), "skipped": skipped, "existing_foreign_keys": len(baseline)}
def select_candidates(db_path: Path, *, min_score: int, locations: tuple[str, ...] = (),
@@ -151,15 +197,16 @@ def main(argv: list[str] | None = None) -> int:
if not args.apply:
print(f"清单确认删除 {len(reviewed)} 条;未加 --apply,只核对清单。")
return 0
+ candidates = candidate_ids(args.db)
connection = open_for_write(args)
try:
- result = purge_reviewed(connection, reviewed)
+ result = purge_reviewed(connection, reviewed, candidates)
finally:
connection.close()
report = args.out or args.review_csv.with_suffix(".result.json")
report.write_text(json.dumps(result, ensure_ascii=False, indent=2), encoding="utf-8")
print(json.dumps(result, ensure_ascii=False))
- return 1 if result["blocked"] or result["cleanup_pending"] else 0
+ return 1 if result["blocked"] or result["cleanup_pending"] or result["skipped"] else 0
locations = tuple(args.location or ())
selected = select_candidates(args.db, min_score=args.min_score,
locations=locations, kind=args.kind)
diff --git a/tests/test_trash_junk.py b/tests/test_trash_junk.py
index a29fcc9b6..71385eb0d 100644
--- a/tests/test_trash_junk.py
+++ b/tests/test_trash_junk.py
@@ -131,12 +131,76 @@ class TrashJunkTests(unittest.TestCase):
self.trash_junk.write_rows(self.out, fields, [review, {**review, "decision": "retain"}])
self.assertEqual(len(self.trash_junk.load_review(self.out)), 1)
- def purge(self, rows):
+ def purge(self, rows, candidates=None):
connection = sqlite3.connect(self.db_path)
connection.row_factory = sqlite3.Row
self.addCleanup(connection.close)
+ if candidates is None:
+ candidates = {int(row["id"]) for row in rows}
with mock.patch.object(self.trash_junk, "LOCATION_ROOT_DECLARATIONS", {"local": (str(self.root),)}):
- return self.trash_junk.purge_reviewed(connection, rows)
+ return self.trash_junk.purge_reviewed(connection, rows, candidates)
+
+ def execute(self, sql, *params):
+ with closing(sqlite3.connect(self.db_path)) as connection:
+ connection.execute(sql, params)
+ connection.commit()
+
+ def exists(self, asset_id):
+ with closing(sqlite3.connect(self.db_path)) as connection:
+ return connection.execute("SELECT 1 FROM asset WHERE id=?", (asset_id,)).fetchone() is not None
+
+ def test_purge_skips_rows_with_personal_records_and_reports_them(self):
+ played, played_review = self.reviewed_file(1)
+ queued, queued_review = self.reviewed_file(2)
+ clean, clean_review = self.reviewed_file(3)
+ self.execute("UPDATE asset SET play_count=1 WHERE id=1")
+ self.execute("INSERT INTO watch_queue(profile_id,asset_id,added_at) VALUES('default',2,'2026-01-01')")
+ result = self.purge([played_review, queued_review, clean_review])
+ self.assertEqual(result["purged"], 1)
+ self.assertEqual({item["id"]: item["reason"] for item in result["skipped"]},
+ {1: "带个人记录", 2: "带个人记录"})
+ self.assertTrue(played.exists() and queued.exists())
+ self.assertTrue(self.exists(1) and self.exists(2))
+ self.assertFalse(clean.exists())
+ self.assertFalse(self.exists(3))
+
+ def test_purge_skips_rows_the_user_kept_or_that_left_the_queue(self):
+ kept, kept_review = self.reviewed_file(1)
+ stale, stale_review = self.reviewed_file(2)
+ self.execute("INSERT INTO review_decision(category,item_key,status,note,updated_at) "
+ "VALUES('junk_file','1','rejected','用户确认不是垃圾','2026-01-01')")
+ result = self.purge([kept_review, stale_review], candidates={1})
+ self.assertEqual(result["purged"], 0)
+ self.assertEqual({item["id"]: item["reason"] for item in result["skipped"]},
+ {1: "用户已标为不是垃圾", 2: "已不在垃圾复核队列里"})
+ self.assertTrue(kept.exists() and stale.exists())
+ self.assertTrue(self.exists(1) and self.exists(2))
+
+ def test_purge_skips_videos_with_subtitles_instead_of_orphaning_them(self):
+ video, review = self.reviewed_file(1, ".mp4")
+ subtitle = video.with_name(video.stem + ".zh.srt")
+ subtitle.write_text("1\n", encoding="utf-8")
+ result = self.purge([review])
+ self.assertEqual(result["purged"], 0)
+ self.assertIn(subtitle.name, result["skipped"][0]["reason"])
+ self.assertTrue(video.exists() and subtitle.exists())
+
+ subtitle.unlink()
+ self.execute("INSERT INTO asset_subtitle(asset_id,location,path,name,format,size,pairing,"
+ "first_seen,last_seen) VALUES(1,'local',?,?,'srt',2,'exact','2026-01-01','2026-01-01')",
+ str(subtitle), subtitle.name)
+ result = self.purge([review])
+ self.assertEqual(result["skipped"][0]["reason"], "登记了字幕")
+ self.assertTrue(video.exists() and self.exists(1))
+
+ def test_purge_candidates_come_from_the_junk_review_queue(self):
+ promotion = self.root / "tuu26.com.mp4"
+ promotion.write_bytes(b"promotion")
+ self.add(1, "local", str(promotion), "video", promotion.stat().st_size, 60)
+ self.reviewed_file(2)
+ candidates = self.trash_junk.candidate_ids(self.db_path)
+ self.assertIn(1, candidates)
+ self.assertNotIn(2, candidates)
def test_reviewed_purge_deletes_only_confirmed_file_and_ledger_row(self):
path, review = self.reviewed_file()