Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 29 additions & 7 deletions services/app/domain/release_profiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import re
import unicodedata
from itertools import permutations
from typing import Literal
from uuid import UUID

Expand Down Expand Up @@ -391,6 +392,21 @@ def identifier_values(value):
)


def co_author_credits(names):
"""Every author together, in any order, joined by a space, " and " or ", ".

"&" needs no joiner of its own: normalising turns it into a space.
"""
names = [name for name in names if normalized(name)]
if not 2 <= len(names) <= 3:
return set()
return {
normalized(joiner.join(order))
for order in permutations(names)
for joiner in (" ", " and ", ", ")
}


def indexer_title_authors(release, work):
"""An exact author/title pair can supply the missing structured indexer fields.

Expand Down Expand Up @@ -426,14 +442,18 @@ def indexer_title_authors(release, work):
authors = {}
for value in work["authors"]:
if author := normalized(value):
authors[author] = value
authors[author] = (value,)
# Inverted credits must carry the comma: don't accept arbitrary word
# permutations as an author match. Splits also cover compound surnames.
words = value.split()
for split in range(1, len(words)):
given, surname = " ".join(words[:split]), " ".join(words[split:])
if re.search(rf"\b{re.escape(surname)},\s*{re.escape(given)}\b", title, re.I):
authors[normalized(f"{surname} {given}")] = value
authors[normalized(f"{surname} {given}")] = (value,)
# Co-written books often credit every author before the title:
# "Terry Pratchett & Stephen Baxter - The Long Earth".
for credit in co_author_credits(work["authors"]):
authors[credit] = tuple(work["authors"])
matched = set()
for expected in expected_titles - {""}:
for author in authors:
Expand All @@ -443,7 +463,7 @@ def indexer_title_authors(release, work):
if pair in labelled_titles or re.fullmatch(
rf"{re.escape(pair)}(?: (?:19|20)\d{{2}})?(?: retail)?", actual
):
matched.add(authors[author])
matched.update(authors[author])
if not matched and not re.search(r"\.(?:rar|zip|7z|par2)$", title, re.I):
# Indexers often use "Author - Title- Subtitle" when the catalog has
# only the short title. Keep the author exact and use the shared subtitle
Expand All @@ -454,7 +474,7 @@ def indexer_title_authors(release, work):
if compatible_title(
source_title, work["title"], allow_extra_subtitle=True
) or title_outside_series_prefix(parts[1], work):
matched.add(authors[credit])
matched.update(authors[credit])
# AudiobookBay names, including Prowlarr's copy of them, are
# "Title - Author, Narrator". The catalog author is the first credit.
if not matched:
Expand All @@ -470,7 +490,7 @@ def indexer_title_authors(release, work):
normalized(head) in expected_titles
or (bare is not None and full_title and normalized(bare) == full_title)
):
matched.add(authors[credit])
matched.update(authors[credit])
# "Series 5 - Author" omits the catalog subtitle. Accept it only when that
# series and position are already on the work and the first credit matches.
if not matched:
Expand All @@ -488,7 +508,7 @@ def indexer_title_authors(release, work):
if isinstance(entry, dict)
)
):
matched.add(authors[credit])
matched.update(authors[credit])
return sorted(matched)


Expand All @@ -508,7 +528,9 @@ def catalog_language_release(release, work):
normalized(parse_title_labels(work["title"]).title),
normalized(optional_subtitle_base(work["title"])),
} - {""}
authors = {normalized(author) for author in work["authors"]} - {""}
authors = ({normalized(author) for author in work["authors"]} - {""}) | co_author_credits(
work["authors"]
)
for author in work["authors"]:
words = author.split()
for split in range(1, len(words)):
Expand Down
63 changes: 62 additions & 1 deletion tests/unit/test_release_name_identity.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
"""Reported indexer layouts must preserve the full catalog-backed identity."""

from datetime import UTC, datetime
from types import SimpleNamespace

import pytest

from app.domain.release_profiles import indexer_title_authors
from app.adapters.prowlarr import ProwlarrRelease
from app.domain.release_profiles import (
catalog_language_release,
co_author_credits,
indexer_title_authors,
)
from app.domain.series_identity import series_note_agrees


Expand Down Expand Up @@ -151,3 +157,58 @@ def test_supported_layout_still_requires_structured_author_agreement_and_catalog
assert authors(title, authors=["Other Writer"]) == []
assert authors(title, position=None) == []
assert authors(title) == ["Stephen King"]


CO_WRITTEN = {"title": "The Long Earth", "authors": ["Terry Pratchett", "Stephen Baxter"]}


def co_written(title):
release = SimpleNamespace(source="prowlarr", title=title, raw_title=title, authors=[])
return indexer_title_authors(release, CO_WRITTEN)


def test_every_co_author_together_is_one_credit():
credits = co_author_credits(CO_WRITTEN["authors"])
assert "terry pratchett stephen baxter" in credits
assert "stephen baxter and terry pratchett" in credits
assert co_author_credits(["Terry Pratchett"]) == set()


@pytest.mark.parametrize(
"title",
[
"Terry Pratchett Stephen Baxter - The Long Earth 2012 MP3",
"Terry Pratchett & Stephen Baxter - The Long Earth",
"Stephen Baxter and Terry Pratchett - The Long Earth",
"The Long Earth - Terry Pratchett & Stephen Baxter",
],
)
def test_release_names_crediting_every_co_author_match(title):
assert co_written(title) == ["Stephen Baxter", "Terry Pratchett"]


@pytest.mark.parametrize(
"title",
[
"Terry Pratchett Neil Gaiman - The Long Earth",
"Terry Pratchett & Stephen Baxter - The Long Mars",
],
)
def test_another_co_author_or_book_does_not_match(title):
assert co_written(title) == []


def test_language_after_every_co_author_and_title_is_inferred():
title = "Terry Pratchett & Stephen Baxter - The Long Earth German Hoerbuch"
source = ProwlarrRelease(
source_id="co-author-fixture",
acquisition_supported=True,
title=title,
raw_title=title,
medium="audio",
protocol="nzb",
indexer_name="Fixture",
categories=[3030],
observed_at=datetime.now(UTC),
)
assert catalog_language_release(source, CO_WRITTEN).language == "de"