qr-scan-difficulty-check / server /reading_content.py
devmandan's picture
Claude Opus 5.5
Fix styled-file hints, and add inches, copies and the many-codes rule
a59c3c4 verified
Raw History Blame Contribute Delete
3.68 kB
"""A picture's content (see reading.py): the content the most scanners return, raw bytes and the text a scanner makes
of them counting as one content, and the content as the page may show it, with digests that pair pictures of one
content whichever scanners read them.
"""
from __future__ import annotations
import hashlib
def _texts(p: bytes) -> set[bytes]:
"""In UTF-8, the texts a scanner may make of raw bytes that aren't UTF-8: ISO-8859-1 (QR byte mode's default)
and, where they decode, Shift JIS (QR kanji mode)."""
try:
p.decode("utf-8")
return set()
except UnicodeDecodeError:
pass
out = {p.decode("latin-1").encode("utf-8")}
try:
out.add(p.decode("shift_jis").encode("utf-8"))
except UnicodeDecodeError:
pass
return out
def _same(p: bytes, q: bytes) -> bool:
"""One content: the same bytes, or raw bytes and a text made of them."""
return p == q or p in _texts(q) or q in _texts(p)
def _is_text(p: bytes) -> bool:
try:
p.decode("utf-8")
return True
except UnicodeDecodeError:
return False
def _consensus(contents: dict[tuple[str, int], list[bytes]]) -> bytes | None:
forms = {p for ps in contents.values() for p in ps}
if not forms:
return None
def votes(c: bytes) -> tuple[int, int]:
hits = [(s, k) for (s, k), ps in contents.items() if any(_same(p, c) for p in ps)]
return len({s for s, _ in hits}), len(hits)
return max(forms, key=lambda c: (*votes(c), _is_text(c), -len(c), c))
def _raws(p: bytes) -> list[bytes]:
"""Every set of raw bytes a content stands for: p, when p is raw bytes already (not UTF-8); else, in this order,
the ISO-8859-1 and the Shift JIS bytes of which p is a text a scanner made, or p itself when it is neither. A text
with only ISO-8859-1 characters ("25°C") can be made of either, so it stands for both."""
try:
text = p.decode("utf-8")
except UnicodeDecodeError:
return [p] # raw bytes already
out = []
for encoding in ("latin-1", "shift_jis"):
try:
raw = text.encode(encoding)
except UnicodeEncodeError:
continue
if raw != p and p in _texts(raw):
out.append(raw)
return out or [p]
def _raw(p: bytes) -> bytes:
"""The raw bytes content.sha256 is taken from: the first of _raws, so one content (_same) has one digest whichever
scanners read it, but for a text that stands for two byte strings, which content.sha256s covers."""
return _raws(p)[0]
def describe(content: bytes | None) -> dict | None:
"""The content as the page may show it: printable text (UTF-8, or else ISO-8859-1), otherwise only its length;
its SHA-256 as one content's raw bytes (_raw); and "sha256s", the sorted SHA-256s of every set of raw bytes it
stands for (_raws), which two pictures of one content always share. Only the visitor's browser receives them, to
pair pictures."""
if content is None:
return None
digest = hashlib.sha256(_raw(content)).hexdigest()
digests = sorted({hashlib.sha256(r).hexdigest() for r in _raws(content)}) # pairing: one content shares one
try:
text = content.decode("utf-8")
except UnicodeDecodeError:
text = content.decode("latin-1") # QR byte mode's default; binary content shows control characters
if all(c.isprintable() or c in "\r\n\t" for c in text):
return {"text": text[:300], "cut": len(text) > 300, "bytes": len(content), "sha256": digest, "sha256s": digests}
return {"text": None, "cut": False, "bytes": len(content), "sha256": digest, "sha256s": digests}