-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtextless_report.py
More file actions
68 lines (56 loc) · 2.25 KB
/
Copy pathtextless_report.py
File metadata and controls
68 lines (56 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
"""Human-review report for TMDB posters that appear to be mislabelled textless."""
from __future__ import annotations
from datetime import datetime, timezone
import fcntl
import logging
from pathlib import Path
import config as _cfg
logger = logging.getLogger(__name__)
_HEADER = (
"# Posters Plus suspected fake textless posters\n"
"# Generated by PP-OCR detection. Review manually before editing TMDB.\n"
"# detected_at_utc\tmedia_type\ttmdb_id\tvote_count\ttmdb_page\timage_url\timage_path\n"
)
def report_fake_textless_poster(
*,
media_type: str,
tmdb_id: str,
image_path: str,
vote_count: int | None,
) -> None:
"""Append one deduplicated poster to the human-review report."""
if not _cfg.TEXTLESS_FAKE_REPORT:
return
normalized_type = "tv" if str(media_type).lower() == "tv" else "movie"
normalized_id = str(tmdb_id).strip()
normalized_path = "/" + str(image_path).strip().lstrip("/")
if not normalized_id or normalized_path == "/":
return
report_path = Path(_cfg.TEXTLESS_FAKE_REPORT_PATH)
try:
report_path.parent.mkdir(parents=True, exist_ok=True)
with report_path.open("a+", encoding="utf-8") as report:
fcntl.flock(report.fileno(), fcntl.LOCK_EX)
report.seek(0)
contents = report.read()
marker = f"\t{normalized_path}\n"
if marker in contents:
return
if not contents:
report.write(_HEADER)
elif not contents.endswith("\n"):
report.write("\n")
detected_at = datetime.now(timezone.utc).isoformat(timespec="seconds")
tmdb_page = (
f"https://www.themoviedb.org/{normalized_type}/{normalized_id}"
"/images/posters?image_language=xx"
)
image_url = f"https://image.tmdb.org/t/p/original{normalized_path}"
votes = "" if vote_count is None else str(vote_count)
report.write(
f"{detected_at}\t{normalized_type}\t{normalized_id}\t{votes}\t"
f"{tmdb_page}\t{image_url}\t{normalized_path}\n"
)
report.flush()
except Exception as exc:
logger.warning("Could not update fake-textless report: %s", exc)