diff --git a/docs/environment-variables.md b/docs/environment-variables.md index 1b202ce4..0041ca8d 100644 --- a/docs/environment-variables.md +++ b/docs/environment-variables.md @@ -1337,6 +1337,7 @@ How long to cache individual book details. Default: 600 (10 minutes). Max: 60480 | `PROWLARR_API_KEY` | Found in Prowlarr: Settings > General > API Key | string (secret) | _none_ | | `PROWLARR_INDEXERS` | Select which indexers to search. 📚 = has book categories. Leave empty to search all. | string (comma-separated) | _empty list_ | | `PROWLARR_INDEXER_TIMEOUT` | How long to wait for a single indexer to answer a search. Indexers behind FlareSolverr can need 90 seconds or more while a cold Cloudflare challenge is solved; raise this if searches come back empty and the Prowlarr log shows the search still running. | number | `90` | +| `PROWLARR_EBOOK_MAX_SIZE_MB` | Hide ebook results larger than this. Expanded searches and general indexers can return TV series, courses and software next to the books. 0 shows everything, and a result with no reported size is always shown. Audiobook searches are not limited. | number | `0` | | `PROWLARR_AUTO_EXPAND` | Automatically retry search without category filtering if no results are found | boolean | `false` | | `PROWLARR_COLLAPSE_DUPLICATES` | Collapse a release that several indexer entries returned down to a single row, keeping the entry with the best Prowlarr priority. Turn this off to see every entry that carried it, which is what makes results from filter-specific entries (freeleech and the like) visible. | boolean | `true` | | `PROWLARR_USE_SEED_PREFERENCES` | Apply per-indexer seed time and ratio preferences from Prowlarr when sending torrents to the download client | boolean | `false` | @@ -1393,6 +1394,16 @@ How long to wait for a single indexer to answer a search. Indexers behind FlareS - **Default:** `90` - **Constraints:** min: 5, max: 300 +#### `PROWLARR_EBOOK_MAX_SIZE_MB` + +**Maximum Ebook Size (MB)** + +Hide ebook results larger than this. Expanded searches and general indexers can return TV series, courses and software next to the books. 0 shows everything, and a result with no reported size is always shown. Audiobook searches are not limited. + +- **Type:** number +- **Default:** `0` +- **Constraints:** min: 0, max: 100000 + #### `PROWLARR_AUTO_EXPAND` **Auto-expand search on no results** diff --git a/shelfmark/release_sources/prowlarr/settings.py b/shelfmark/release_sources/prowlarr/settings.py index d4f74780..41c1159f 100644 --- a/shelfmark/release_sources/prowlarr/settings.py +++ b/shelfmark/release_sources/prowlarr/settings.py @@ -226,6 +226,20 @@ def prowlarr_config_settings() -> list[SettingsField]: max_value=MAX_INDEXER_TIMEOUT_SECONDS, show_when={"field": "PROWLARR_ENABLED", "value": True}, ), + NumberField( + key="PROWLARR_EBOOK_MAX_SIZE_MB", + label="Maximum Ebook Size (MB)", + description=( + "Hide ebook results larger than this. Expanded searches and general indexers can " + "return TV series, courses and software next to the books. 0 shows everything, " + "and a result with no reported size is always shown. Audiobook searches are not " + "limited." + ), + default=0, + min_value=0, + max_value=100000, + show_when={"field": "PROWLARR_ENABLED", "value": True}, + ), CheckboxField( key="PROWLARR_AUTO_EXPAND", label="Auto-expand search on no results", diff --git a/shelfmark/release_sources/prowlarr/source.py b/shelfmark/release_sources/prowlarr/source.py index eb920e8e..dc6fc3b3 100644 --- a/shelfmark/release_sources/prowlarr/source.py +++ b/shelfmark/release_sources/prowlarr/source.py @@ -17,7 +17,7 @@ from shelfmark.core.author_match import AUTHOR_UNKNOWN, author_affinity from shelfmark.core.config import config from shelfmark.core.languages import normalize_language from shelfmark.core.logger import setup_logger -from shelfmark.core.request_helpers import normalize_optional_text +from shelfmark.core.request_helpers import coerce_int, normalize_optional_text from shelfmark.core.search_plan import ReleaseSearchVariant from shelfmark.core.utils import AUDIOBOOK_FORMATS as CORE_AUDIOBOOK_FORMATS from shelfmark.core.utils import normalize_http_url @@ -291,6 +291,22 @@ def _collapse_duplicate_indexer_results( return kept +def _max_release_bytes(content_type: str) -> int | None: + """The size above which an ebook result is hidden, or None when no limit is set.""" + if content_type == "audiobook": + return None + megabytes = coerce_int(config.get("PROWLARR_EBOOK_MAX_SIZE_MB", 0), 0) + return megabytes * _SIZE_UNIT_BASE * _SIZE_UNIT_BASE if megabytes > 0 else None + + +def _is_over_size(result: dict, max_bytes: int | None) -> bool: + """Whether a raw result reports a size above the limit. An unknown size is never over.""" + if max_bytes is None: + return False + size = coerce_int(result.get("size"), 0) + return size > max_bytes + + def _parse_size(size_bytes: int | None) -> str | None: """Convert bytes to human-readable size string.""" if size_bytes is None or size_bytes <= 0: @@ -1283,7 +1299,12 @@ class ProwlarrSource(ReleaseSource): # metadata author would second-guess what they typed. wanted_author = "" if plan.manual_query else plan.author + max_bytes = _max_release_bytes(content_type) + oversized = 0 for raw_result in all_results: + if _is_over_size(raw_result, max_bytes): + oversized += 1 + continue result_with_seed_settings = _apply_indexer_seed_settings( raw_result, indexer_seed_settings ) @@ -1311,6 +1332,9 @@ class ProwlarrSource(ReleaseSource): mam_releases.append(release) mam_indexer = mam_indexer or mam_indexers[idx_id_int] + if oversized: + logger.debug("Prowlarr: hid %s result(s) over the ebook size limit", oversized) + mam_session_id = _get_mam_session_id() if mam_session_id and mam_releases: # Rerun the search Prowlarr sent to MAM, so it returns the same torrents: diff --git a/tests/prowlarr/test_source.py b/tests/prowlarr/test_source.py index 5191acc1..45e1ed66 100644 --- a/tests/prowlarr/test_source.py +++ b/tests/prowlarr/test_source.py @@ -1535,3 +1535,75 @@ class TestProwlarrStaleIndexerSelection: fake_client = self._search_with_selection(monkeypatch, [98, 99]) assert fake_client.searched_indexer_ids == [] + + +class TestEbookMaxSize: + """PROWLARR_EBOOK_MAX_SIZE_MB hides oversized ebook results and nothing else (#844).""" + + INDEXER = 10 + MB = 1024 * 1024 + + def _results(self, *sizes): + return { + self.INDEXER: [ + {**_mam_result(self.INDEXER, "Indexer", f"guid-{n}"), "size": size} + for n, size in enumerate(sizes) + ] + } + + def _search(self, monkeypatch, sizes, *, limit=None, content_type="ebook"): + import shelfmark.release_sources.prowlarr.source as prowlarr_source + from shelfmark.core.search_plan import build_release_search_plan + + values = {"PROWLARR_INDEXERS": "", "PROWLARR_AUTO_EXPAND": False} + if limit is not None: + values["PROWLARR_EBOOK_MAX_SIZE_MB"] = limit + monkeypatch.setattr( + prowlarr_source.config, "get", lambda key, default=None: values.get(key, default) + ) + source = ProwlarrSource() + monkeypatch.setattr( + source, "_get_client", lambda: _MultiIndexerClient(self._results(*sizes), None) + ) + book = BookMetadata( + provider="hardcover", provider_id="123", title="Dune", authors=["Frank Herbert"] + ) + plan = build_release_search_plan(book, languages=["en"]) + return source.search(book, plan, content_type=content_type) + + def test_no_limit_shows_everything(self, monkeypatch): + releases = self._search(monkeypatch, [5 * self.MB, 4000 * self.MB]) + + assert len(releases) == 2 + + def test_zero_is_no_limit(self, monkeypatch): + releases = self._search(monkeypatch, [5 * self.MB, 4000 * self.MB], limit=0) + + assert len(releases) == 2 + + def test_a_result_over_the_limit_is_hidden(self, monkeypatch): + releases = self._search(monkeypatch, [5 * self.MB, 4000 * self.MB], limit=100) + + assert [r.size_bytes for r in releases] == [5 * self.MB] + + def test_a_result_exactly_at_the_limit_stays(self, monkeypatch): + releases = self._search(monkeypatch, [100 * self.MB, 100 * self.MB + 1], limit=100) + + assert [r.size_bytes for r in releases] == [100 * self.MB] + + def test_a_result_with_no_size_stays(self, monkeypatch): + releases = self._search(monkeypatch, [None, 0, 4000 * self.MB], limit=100) + + assert len(releases) == 2 + + def test_audiobook_searches_are_not_limited(self, monkeypatch): + releases = self._search( + monkeypatch, [5 * self.MB, 4000 * self.MB], limit=100, content_type="audiobook" + ) + + assert len(releases) == 2 + + def test_a_bad_setting_is_no_limit(self, monkeypatch): + releases = self._search(monkeypatch, [5 * self.MB, 4000 * self.MB], limit="lots") + + assert len(releases) == 2