Person Updater overhaul
This commit is contained in:
@@ -70,3 +70,30 @@ def person_name_with_id(name: str, *,
|
||||
if al_id:
|
||||
return f"{name} (AL {al_id})"
|
||||
return name
|
||||
|
||||
|
||||
# Matches the suffix produced by person_name_with_id at the end of a name.
|
||||
_TRACKER_ID_RE = re.compile(r"\s*\((MAL|AL)\s+(\d+)\)\s*$", re.IGNORECASE)
|
||||
|
||||
|
||||
def parse_person_tracker_id(name: str) -> "tuple[str, int] | None":
|
||||
"""
|
||||
Inverse of person_name_with_id: extracts the tracker id from a
|
||||
disambiguated Kavita person name.
|
||||
|
||||
"Rem (MAL 118737)" -> ("mal", 118737)
|
||||
"Subaru (AL 88311)" -> ("al", 88311)
|
||||
"Kotoyama" -> None (no id suffix — e.g. an author/staff record)
|
||||
|
||||
Returns ("mal" | "al", id) or None.
|
||||
"""
|
||||
if not name:
|
||||
return None
|
||||
m = _TRACKER_ID_RE.search(name)
|
||||
if not m:
|
||||
return None
|
||||
source = "mal" if m.group(1).upper() == "MAL" else "al"
|
||||
try:
|
||||
return source, int(m.group(2))
|
||||
except ValueError:
|
||||
return None
|
||||
|
||||
Reference in New Issue
Block a user