From ec9e44efbed36ec8d2d5fc47e245b930a8925a0c Mon Sep 17 00:00:00 2001 From: pax Date: Wed, 15 Apr 2026 17:27:10 -0500 Subject: [PATCH] category_fetcher: extract shared tag-API params builder Both fetch_via_tag_api and _probe_batch_api built the same params dict (with identical lstrip/startswith credential quirks) inline. Pulled into _build_tag_api_params so future credential-format tweaks have one site, not two. --- CHANGELOG.md | 3 ++ booru_viewer/core/api/category_fetcher.py | 57 +++++++++++------------ 2 files changed, 30 insertions(+), 30 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ffe5963..d814d74 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,9 @@ ### Changed - Thumbnail drag-start threshold raised from 10px to 30px to match the rubber band's gate — small mouse wobbles on a thumb no longer trigger a file drag +### Refactored +- `category_fetcher` batch tag-API params are now built by a shared `_build_tag_api_params` helper instead of duplicated across `fetch_via_tag_api` and `_probe_batch_api` + ## v0.2.7 ### Fixed diff --git a/booru_viewer/core/api/category_fetcher.py b/booru_viewer/core/api/category_fetcher.py index e625e1f..2304149 100644 --- a/booru_viewer/core/api/category_fetcher.py +++ b/booru_viewer/core/api/category_fetcher.py @@ -213,6 +213,31 @@ class CategoryFetcher: and bool(self._client.api_user) ) + def _build_tag_api_params(self, chunk: list[str]) -> dict: + """Params dict for a tag-DAPI batch request. + + The ``lstrip("&")`` and ``startswith("api_key=")`` guards + accommodate users who paste their credentials with a leading + ``&`` or as ``api_key=VALUE`` — either form gets normalised + to a clean name→value mapping. + """ + params: dict = { + "page": "dapi", + "s": "tag", + "q": "index", + "json": "1", + "names": " ".join(chunk), + "limit": len(chunk), + } + if self._client.api_key and self._client.api_user: + key = self._client.api_key.strip().lstrip("&") + user = self._client.api_user.strip().lstrip("&") + if key and not key.startswith("api_key="): + params["api_key"] = key + if user and not user.startswith("user_id="): + params["user_id"] = user + return params + async def fetch_via_tag_api(self, posts: list["Post"]) -> int: """Batch-fetch tag types via the booru's tag DAPI. @@ -244,21 +269,7 @@ class CategoryFetcher: BATCH = 500 for i in range(0, len(missing), BATCH): chunk = missing[i:i + BATCH] - params: dict = { - "page": "dapi", - "s": "tag", - "q": "index", - "json": "1", - "names": " ".join(chunk), - "limit": len(chunk), - } - if self._client.api_key and self._client.api_user: - key = self._client.api_key.strip().lstrip("&") - user = self._client.api_user.strip().lstrip("&") - if key and not key.startswith("api_key="): - params["api_key"] = key - if user and not user.startswith("user_id="): - params["user_id"] = user + params = self._build_tag_api_params(chunk) try: resp = await self._client._request("GET", tag_api_url, params=params) resp.raise_for_status() @@ -480,21 +491,7 @@ class CategoryFetcher: # Send one batch request chunk = missing[:500] - params: dict = { - "page": "dapi", - "s": "tag", - "q": "index", - "json": "1", - "names": " ".join(chunk), - "limit": len(chunk), - } - if self._client.api_key and self._client.api_user: - key = self._client.api_key.strip().lstrip("&") - user = self._client.api_user.strip().lstrip("&") - if key and not key.startswith("api_key="): - params["api_key"] = key - if user and not user.startswith("user_id="): - params["user_id"] = user + params = self._build_tag_api_params(chunk) try: resp = await self._client._request("GET", tag_api_url, params=params)