cleanup
This commit is contained in:
@@ -119,26 +119,18 @@ class MangaBakaWorksResolver:
|
||||
# ------------------------------------------------------------------
|
||||
# Public API
|
||||
# ------------------------------------------------------------------
|
||||
def get_works(self, series_id: str) -> list[dict]:
|
||||
def _fetch_all_pages(self, endpoint: str) -> list[dict]:
|
||||
"""
|
||||
Returns volume-level works for a series, filtered to those that have
|
||||
a usable cover image. Results are cached per series.
|
||||
|
||||
Pages through the API (limit=50) until the response returns an empty
|
||||
page, collecting all works before applying the cover filter.
|
||||
Pages through a MangaBaka list endpoint (limit=50 per page) and
|
||||
returns all collected `data` items. Network errors end the
|
||||
pagination early; items fetched so far are returned.
|
||||
"""
|
||||
if not series_id:
|
||||
return []
|
||||
|
||||
if series_id in self._cache:
|
||||
return self._cache[series_id]
|
||||
|
||||
all_works: list[dict] = []
|
||||
items: list[dict] = []
|
||||
page = 1
|
||||
try:
|
||||
while True:
|
||||
resp = self._session.get(
|
||||
f"{self.api_base_url}/series/{series_id}/works",
|
||||
f"{self.api_base_url}/series/{endpoint}",
|
||||
params={"limit": 50, "page": page},
|
||||
timeout=self.request_timeout,
|
||||
)
|
||||
@@ -146,17 +138,35 @@ class MangaBakaWorksResolver:
|
||||
page_data = resp.json().get("data") or []
|
||||
if not page_data:
|
||||
break
|
||||
all_works.extend(page_data)
|
||||
items.extend(page_data)
|
||||
if len(page_data) < 50:
|
||||
break
|
||||
page += 1
|
||||
except requests.RequestException:
|
||||
if not all_works:
|
||||
return []
|
||||
pass
|
||||
return items
|
||||
|
||||
def get_works(self, series_id: str) -> list[dict]:
|
||||
"""
|
||||
Returns volume-level works for a series, filtered to those that have
|
||||
a usable cover image.
|
||||
|
||||
Non-empty results are cached per series; empty results are not, so
|
||||
works added on MangaBaka later become visible without restarting
|
||||
the (long-running) process.
|
||||
"""
|
||||
if not series_id:
|
||||
return []
|
||||
|
||||
if series_id in self._cache:
|
||||
return self._cache[series_id]
|
||||
|
||||
all_works = self._fetch_all_pages(f"{series_id}/works")
|
||||
|
||||
# Discard works that carry no usable cover
|
||||
works_with_cover = [w for w in all_works if w.get("images")]
|
||||
self._cache[series_id] = works_with_cover
|
||||
if works_with_cover:
|
||||
self._cache[series_id] = works_with_cover
|
||||
return works_with_cover
|
||||
|
||||
def get_work_for_volume(self, series_id: str, volume) -> "dict | None":
|
||||
@@ -190,25 +200,7 @@ class MangaBakaWorksResolver:
|
||||
if series_id in self._images_cache:
|
||||
return self._images_cache[series_id]
|
||||
|
||||
raw_items: list[dict] = []
|
||||
page = 1
|
||||
try:
|
||||
while True:
|
||||
resp = self._session.get(
|
||||
f"{self.api_base_url}/series/{series_id}/images",
|
||||
params={"limit": 50, "page": page},
|
||||
timeout=self.request_timeout,
|
||||
)
|
||||
resp.raise_for_status()
|
||||
page_data = resp.json().get("data") or []
|
||||
if not page_data:
|
||||
break
|
||||
raw_items.extend(page_data)
|
||||
if len(page_data) < 50:
|
||||
break
|
||||
page += 1
|
||||
except requests.RequestException:
|
||||
pass
|
||||
raw_items = self._fetch_all_pages(f"{series_id}/images")
|
||||
|
||||
# Group by normalised volume index; collect all languages per volume.
|
||||
by_volume: dict[str, dict[str, str]] = {} # norm_vol -> {lang: url}
|
||||
@@ -236,7 +228,10 @@ class MangaBakaWorksResolver:
|
||||
if url:
|
||||
result[norm] = url
|
||||
|
||||
self._images_cache[series_id] = result
|
||||
# Empty results are not cached — covers added on MangaBaka later
|
||||
# become visible without restarting the long-running process.
|
||||
if result:
|
||||
self._images_cache[series_id] = result
|
||||
return result
|
||||
|
||||
def get_cover_for_volume_from_images(self, series_id: str,
|
||||
|
||||
Reference in New Issue
Block a user