comicbox.formats.base.online.profile

[docs] module comicbox.formats.base.online.profile

  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
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
"""
ComicProfile / Candidate types and helpers.

`ComicProfile` is the input the matcher reads to score candidates against
the comic at hand. It's built once per comic from the normalized
non-online source metadata so we don't depend on what the online lookup
itself produced.

`Candidate` and `CandidateSummary` are the matcher output unit — one per
search hit.
"""

from __future__ import annotations

import re
from dataclasses import dataclass, field
from typing import Any, Final

from glom import glom


@dataclass(frozen=True, slots=True)
class ComicProfile:
    """Comic-side inputs for the matcher."""

    series: str | None = None
    issue: str | None = None
    issue_int: int | None = None
    year: int | None = None
    publisher: str | None = None
    page_count: int | None = None
    # Series volume ordinal (e.g. "Spider-Man Vol. 2 #1" → 2). When set,
    # used as a soft search filter for sources that support it (Metron's
    # `series_volume`); CV's API has no ordinal-volume filter.
    volume: int | None = None


@dataclass(frozen=True, slots=True)
class CandidateSummary:
    """Display fields shown in the prompt UX."""

    series: str
    issue: str
    year: int | None
    publisher: str | None
    page_count: int | None
    cover_url: str | None
    variant_label: str | None


@dataclass(frozen=True, slots=True)
class Candidate:
    """One search result, post-ranking."""

    source: str
    issue_id: int
    summary: CandidateSummary
    raw: dict[str, Any] = field(default_factory=dict)
    metadata_score: float = 0.0
    cover_score: float | None = None
    score: float = 0.0
    url: str = ""
    precomputed_cover_hash: str | None = None
    # The parent container's id — CV's `volume.id`, Metron's `series.id`.
    # Two issues sharing a volume_id are siblings in the same series run;
    # this is what calibration uses to distinguish "variant cover of the
    # same issue" (same volume_id) from "wrong-series collision" (different
    # volume_ids that happen to share a name like "Watchmen"). Sources set
    # this when constructing the candidate; left as None for sources that
    # don't expose it.
    volume_id: int | None = None


_INT_RE = re.compile(r"^\d+$")
_LEADING_DIGITS_RE = re.compile(r"^(\d+)(.*)$")


def parse_issue_int(raw: Any) -> int | None:
    """
    Return the integer form of an issue number when parseable.

    Strips leading zeros (`001` → `1`); returns None for non-numeric forms
    like `1a`, `1.5`, `0a`, `Special`, etc.
    """
    if raw is None:
        return None
    s = str(raw).strip()
    if not s or not _INT_RE.match(s):
        return None
    return int(s)


def strip_issue_leading_zeros(raw: str | None) -> str | None:
    """
    Strip leading zeros from an issue number string, preserving any suffix.

    Used when building API search params: comicvine, metron, etc. expect
    `7` not `007`. Variant suffixes (`a`, `.5`) survive unchanged.

    Examples:
        "007" → "7"
        "001a" → "1a"
        "0" → "0"
        "1.5" → "1.5"
        "Special" → "Special"
        None → None

    """
    if raw is None:
        return None
    s = str(raw).strip()
    if not s:
        return s
    m = _LEADING_DIGITS_RE.match(s)
    if not m:
        return s
    digits, rest = m.groups()
    stripped = digits.lstrip("0") or "0"
    return stripped + rest


def parse_year(raw: Any) -> int | None:
    """Pull a 4-digit year out of a date-ish input."""
    if raw is None:
        return None
    s = str(raw).strip()
    m = re.search(r"\b(\d{4})\b", s)
    return int(m.group(1)) if m else None


def _resolve_volume(md: dict) -> int | None:
    """
    Extract `comicbox.volume.number` as an int, defensively.

    Rejects 4-digit year-shaped values (1900-2100). The
    [ComicInfo.xml convention](https://anansi-project.github.io/docs/comicinfo/documentation#volume)
    of using the year-of-first-issue as the volume number is widespread
    (comictagger writes it that way), but Metron's `series_volume`
    filter expects an ordinal (1, 2, 3 — "Vol. N of M"). Sending the
    year as `series_volume` matches no issues and wastes API budget on
    the drop-volume retry.

    A real volume "Vol. 2019 of 9999" doesn't exist in the wild —
    real ordinal volumes are well under 50 in practice. Treating
    1900-2100 as "year masquerading as volume" is safe and matches the
    failure mode we actually see.
    """
    raw = glom(md, "comicbox.volume.number", default=None)
    if raw is None:
        return None
    try:
        v = int(raw)
    except (TypeError, ValueError):
        return None
    if 1900 <= v <= 2100:  # noqa: PLR2004 — year range, not a magic threshold
        return None
    return v


_SIMPLE_PROFILE_PATHS: Final[tuple[tuple[str, str], ...]] = (
    ("series", "comicbox.series.name"),
    ("issue", "comicbox.issue.name"),
    ("publisher", "comicbox.publisher.name"),
    ("page_count", "comicbox.page_count"),
)


def accumulate_profile_fields(fields: dict[str, Any], md: dict) -> None:
    """
    Last-wins accumulation of profile fields across normalized sources.

    Callers feed sources in `MetadataSources` enum order, which is the
    same order the main merge overlays them (`box/merge.py`,
    `_set_merged_metadata`) — and that merge is last-wins: a
    higher-precedence source overwrites an earlier one. So a later source
    that supplies a field overwrites the value already collected, which
    is why embedded archive files (`ARCHIVE_FILE`) beat the filename
    parse (`ARCHIVE_FILENAME`) here, matching the merged metadata the rest
    of comicbox produces. A later source that *lacks* the field leaves the
    earlier value intact (the truthiness guards below), so absent embedded
    fields still fall back to the filename.
    """
    for field_name, path in _SIMPLE_PROFILE_PATHS:
        if v := glom(md, path, default=None):
            fields[field_name] = v
    raw_year = glom(md, "comicbox.date.year", default=None) or glom(
        md, "comicbox.date.cover_date", default=None
    )
    if (parsed := parse_year(raw_year)) is not None:
        fields["year"] = parsed
    if (v := _resolve_volume(md)) is not None:
        fields["volume"] = v