mirror of
https://github.com/kuhyx/testsAndMisc.git
synced 2026-11-10 22:47:02 +01:00
New package. Walks every thread on 4chan's /wsg/, downloads each video it has
not seen, and reviews them one at a time: mpv embedded in a Tk window via --wid
with Keep/Pass/Undo directly beneath the picture.
Dedupe is structural rather than a convention. Every post carrying a file also
carries md5 -- base64 MD5 of the whole file -- so it is the sqlite primary key.
An already-seen video costs zero bytes to skip and a byte-identical repost in
another thread collapses onto the existing row. Verified against a live file:
the API's Nz9OEKdMuMZEdYE6eLTKmA== matched base64(md5(bytes)) exactly.
Undo is unbounded and survives quitting: the verdict trail lives in the index,
not in memory, so yesterday's decisions can still be taken back.
No downloaded video is ever deleted. A pass moves the file to trash/ and leaves
it there; files.apply_move refuses to overwrite an existing destination, so the
guarantee holds by construction rather than per caller.
Three bugs only running it could find, each caught from evidence rather than
guesswork:
- the video stayed black: a <Configure> handler set mpv's geometry, fighting
the automatic sizing --wid already does
- playback never advanced: refresh() ran every tick and re-sent loadfile ~7x
a second, restarting the video continuously
- it froze on one video around the 26th while filenames kept advancing: mpv
pushes an event stream down the same socket and nothing drained it, so the
buffer filled and mpv stopped reading commands. Isolated to command #35
without a drain thread; all 60 honoured with one.
A five-lens review then found nine more, all reproduced before fixing: a failed
mpv start stranded the non-daemon worker forever; undo could clobber a file in
incoming/; a NUL-bearing md5 poisoned the index permanently; an oversized int
killed the worker with no event; gzip bombs on both hosts were accepted; the
board endpoints were fetched unconditionally forever; and Session.missing could
corrupt the undo trail.
mpv runs as a subprocess over its JSON IPC socket rather than via python-mpv,
which resolves libmpv at import time and would fail on CI while passing here.
Third-party surface is requests alone, already declared.
277 package tests, 100% branch coverage on all 22 modules, no suppressions.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
100 lines
2.8 KiB
Python
100 lines
2.8 KiB
Python
"""What a keep/pass decision means, expressed as data.
|
|
|
|
Planning a move and performing it are split deliberately: everything here is
|
|
pure and exhaustively testable, and :mod:`python_pkg.wsg_grabber.files` is the
|
|
only code that touches the filesystem.
|
|
|
|
Nothing in this package deletes a video. A pass relocates it to ``trash/`` and
|
|
leaves clearing that directory to the user.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import TYPE_CHECKING
|
|
|
|
from python_pkg.wsg_grabber.models import FileMove, Verdict
|
|
from python_pkg.wsg_grabber.states import FileState
|
|
|
|
if TYPE_CHECKING:
|
|
from collections.abc import Container
|
|
from pathlib import Path
|
|
|
|
from python_pkg.wsg_grabber.models import ReviewItem
|
|
|
|
|
|
def target_state(choice: Verdict) -> FileState:
|
|
"""Return the terminal state a verdict leads to.
|
|
|
|
Args:
|
|
choice: The user's decision.
|
|
|
|
Returns:
|
|
FileState: ``KEPT`` or ``PASSED``.
|
|
"""
|
|
return FileState.KEPT if choice is Verdict.KEEP else FileState.PASSED
|
|
|
|
|
|
def target_dir(choice: Verdict, keep: Path, trash: Path) -> Path:
|
|
"""Return the directory a verdict sends a file to.
|
|
|
|
Args:
|
|
choice: The user's decision.
|
|
keep: Directory for kept videos.
|
|
trash: Directory for passed videos.
|
|
|
|
Returns:
|
|
Path: Destination directory.
|
|
"""
|
|
return keep if choice is Verdict.KEEP else trash
|
|
|
|
|
|
def unique_destination(directory: Path, name: str, taken: Container[str]) -> Path:
|
|
"""Return a collision-free destination inside *directory*.
|
|
|
|
``taken`` is supplied by the caller rather than read from disk, keeping this
|
|
function pure and its collision handling directly testable.
|
|
|
|
Args:
|
|
directory: Destination directory.
|
|
name: Preferred filename.
|
|
taken: Names already present.
|
|
|
|
Returns:
|
|
Path: A path whose name is not in *taken*.
|
|
"""
|
|
if name not in taken:
|
|
return directory / name
|
|
stem, _, suffix = name.rpartition(".")
|
|
base, ext = (stem, f".{suffix}") if stem else (name, "")
|
|
index = 2
|
|
while f"{base}-{index}{ext}" in taken:
|
|
index += 1
|
|
return directory / f"{base}-{index}{ext}"
|
|
|
|
|
|
def plan_move(
|
|
item: ReviewItem,
|
|
choice: Verdict,
|
|
keep: Path,
|
|
trash: Path,
|
|
taken: Container[str] = (),
|
|
) -> FileMove:
|
|
"""Work out where a reviewed video should end up.
|
|
|
|
Args:
|
|
item: The video under review.
|
|
choice: The user's decision.
|
|
keep: Directory for kept videos.
|
|
trash: Directory for passed videos.
|
|
taken: Names already used in the destination directory.
|
|
|
|
Returns:
|
|
FileMove: Source, destination and the file's identity.
|
|
"""
|
|
destination = target_dir(choice, keep, trash)
|
|
return FileMove(
|
|
md5=item.md5,
|
|
src=item.path,
|
|
dst=unique_destination(destination, item.path.name, taken),
|
|
)
|