Skip to content

Commit

Permalink
Fix volume covers behaviour
Browse files Browse the repository at this point in the history
Ref: #105
  • Loading branch information
mansuf committed Jun 10, 2024
1 parent d52ae25 commit b737e4a
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 17 deletions.
45 changes: 34 additions & 11 deletions mangadex_downloader/format/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

from enum import Enum
from .chinfo import get_chapter_info as get_chinfo
from ..language import Language
from ..downloader import FileDownloader
from ..utils import get_cover_art_url
from .. import __repository__, __url_repository__, json_op
Expand Down Expand Up @@ -66,17 +67,39 @@ def find_volume_cover(cover):

return volume == cover.volume

f = filter(find_volume_cover, CoverArtIterator(manga.id))

try:
cover = next(f)
except StopIteration:
if download:
pbm.logger.warning(
f"Failed to find volume cover for volume {volume}. "
"Falling back to manga cover..."
)
cover = manga.cover
cover_art_iter_kwargs = [
# Fix default volume covers behaviour
# See https://github.com/mansuf/mangadex-downloader/issues/105
{"language_override": None}, # --volume-cover-language or --language
{
"language_override": manga.original_language.value,
}, # volume cover from manga original language
{
"language_override": "all",
}, # Volume cover from any languages that exists
]

cover = None
for kwargs in cover_art_iter_kwargs:
kwargs["manga_id"] = manga.id
iterator = CoverArtIterator(**kwargs)
f = filter(find_volume_cover, iterator)

try:
cover = next(f)
except StopIteration:
lang = Language(iterator.language)
log.debug(f"Failed to find volume cover in {lang.name} language")
continue
else:
break

if download and cover is None:
pbm.logger.warning(
f"Failed to find volume cover for volume {volume}. "
"Falling back to manga cover..."
)
cover = manga.cover

url = get_cover_art_url(manga.id, cover, "original")

Expand Down
19 changes: 13 additions & 6 deletions mangadex_downloader/iterator.py
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ def iter_random_manga(**filters):
class CoverArtIterator(BaseIterator):
cache = {}

def __init__(self, manga_id):
def __init__(self, manga_id, language_override=None):
super().__init__()

self.limit = 100
Expand All @@ -467,6 +467,16 @@ def __init__(self, manga_id):
# Data is not cached
self.cache[manga_id] = []

self.language = None

if language_override:
self.language = language_override
else:
vol_cover_lang = config.volume_cover_language
cover_locale = vol_cover_lang if vol_cover_lang else config.language

self.language = cover_locale

def _make_cache_iterator(self):
for item in self.cache.get(self.manga_id):
yield item
Expand All @@ -485,9 +495,6 @@ def fill_data(self):
if self.cache.get(self.manga_id):
return

vol_cover_lang = config.volume_cover_language
cover_locale = vol_cover_lang if vol_cover_lang else config.language

# One-shot filling covers in single function call
# So cache can be used
while True:
Expand All @@ -497,8 +504,8 @@ def fill_data(self):
"limit": self.limit,
}

if cover_locale != "all":
params.update({"locales[]": cover_locale})
if self.language != "all" and self.language is not None:
params.update({"locales[]": self.language})

url = f"{base_url}/cover"
r = Net.mangadex.get(url, params=params)
Expand Down
5 changes: 5 additions & 0 deletions mangadex_downloader/manga.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@ def title(self):
""":class:`str`: Return title of the manga"""
return self._title

@property
def original_language(self):
""":class:`str`: Return original language of the manga"""
return Language(self._attr.get("originalLanguage"))

@property
def alternative_titles(self):
"""List[:class:`str`]: List of alternative titles"""
Expand Down

0 comments on commit b737e4a

Please sign in to comment.