Skip to content

Commit

Permalink
Added ability to set relative to end of chapter or pages with legacy …
Browse files Browse the repository at this point in the history
…range
  • Loading branch information
mansuf committed Jun 14, 2024
1 parent dfa593d commit 63577e9
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 14 deletions.
17 changes: 14 additions & 3 deletions mangadex_downloader/chapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import logging
import queue
from pathvalidate import sanitize_filename
from typing import List

from .user import User
from .language import Language
Expand All @@ -35,7 +36,7 @@
from .errors import ChapterNotFound, GroupNotFound, UserNotFound
from .group import Group
from .config import config, env
from .utils import convert_int_or_float, get_local_attr
from .utils import convert_int_or_float, get_local_attr, convert_start_end_from_negative
from .progress_bar import progress_bar_manager as pbm

log = logging.getLogger(__name__)
Expand Down Expand Up @@ -116,6 +117,11 @@ def iter(self, log_info=False):
quality_mode = "data-saver" if self.data_saver else "data"
images = self._low_images if self.data_saver else self._images

# Convert range numbers if it's negative
self.start_page, self.end_page = convert_start_end_from_negative(
self.start_page, self.end_page, range(1, len(images) + 1)
)

page = 1
for img in images:
if not self._check_range_page(page, log_info):
Expand Down Expand Up @@ -374,7 +380,7 @@ def __init__(
"_range and (start_* or end_* or no_oneshot) cannot be together"
)

self.chapters = chapters
self.chapters: List[Chapter] = chapters
self.manga = manga
self.language = Language(lang)
self.queue = queue.Queue()
Expand All @@ -388,6 +394,11 @@ def __init__(
self.legacy_range = legacy_range
self.duplicates = {}

# Convert the numbers if it's negative
self.start_chapter, self.end_chapter = convert_start_end_from_negative(
self.start_chapter, self.end_chapter, [i.chapter for i in self.chapters]
)

if _range is not None:
# self.range = range_mod.compile(_range)
self.range = None
Expand Down Expand Up @@ -679,7 +690,7 @@ def _parse_volumes_from_chapter(self, chapter):

def _parse_volumes(self):
iterator = iter_chapters_feed(self.manga.id, self.language)
self.chapters = map(Chapter.from_data, iterator)
self.chapters.extend(map(Chapter.from_data, iterator))

if not self.chapters:
raise ChapterNotFound(
Expand Down
21 changes: 21 additions & 0 deletions mangadex_downloader/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,27 @@ def check_conflict_options(args, parser):
if args.ignore_missing_chapters and args.no_track:
parser.error("--ignore-missing-chapters cannot be used when --no-track is set")

if args.group and args.no_group_name:
raise MangaDexException("--group cannot be used together with --no-group-name")

if args.start_chapter is not None and args.end_chapter is not None:
if args.start_chapter > args.end_chapter:
raise MangaDexException("--start-chapter cannot be more than --end-chapter")

if args.start_chapter < 0 and args.end_chapter >= 0:
raise MangaDexException(
"--end-chapter cannot be positive number while --start-chapter is negative number"
)

if args.start_page is not None and args.end_page is not None:
if args.start_page > args.end_page:
raise MangaDexException("--start-page cannot be more than --end-page")

if args.start_page < 0 and args.end_page >= 0:
raise MangaDexException(
"--end-page cannot be positive number while --start-page is negative number"
)


def _main(argv):
parser = None
Expand Down
11 changes: 0 additions & 11 deletions mangadex_downloader/cli/url.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,6 @@
def download_manga(url, args, legacy=False):
check_group_all(args)

if args.group and args.no_group_name:
raise MangaDexException("--group cannot be used together with --no-group-name")

if args.start_chapter is not None and args.end_chapter is not None:
if args.start_chapter > args.end_chapter:
raise MangaDexException("--start-chapter cannot be more than --end-chapter")

if args.start_page is not None and args.end_page is not None:
if args.start_page > args.end_page:
raise MangaDexException("--start-page cannot be more than --end-page")

# We cannot allow if --range and other range options (such as: --start-chapter) together
range_forbidden_args = {
"start_chapter": "--start-chapter",
Expand Down
40 changes: 40 additions & 0 deletions mangadex_downloader/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,3 +364,43 @@ def create_manga_info(path, manga, replace):

log.debug("Download finished because --create-manga-info is present")
return manga


def convert_start_end_from_negative(start_num, end_num, data):
"""Convert start and end numbers from negative to positive
Use cases:
- "--start-chapter"
- "--end-chapter"
- "--start-page"
- "--end-page"
"""

if start_num is None and end_num is None:
return start_num, end_num

if start_num is None:
start_num = 0
if end_num is None:
end_num = 0

if start_num >= 0 and end_num >= 0:
return start_num, end_num

start_num = convert_int_or_float(start_num)
end_num = convert_int_or_float(end_num)

# Remove chapters from last index to beginning that is not numbers
# to make sure we can use legacy range chapters and pages
# We don't need to sorting the array, because that's been already done
# in mangadex_downloader.chapter.IteratorChapter._fill_data()
filtered_data = []
for num in data:
try:
a = convert_int_or_float(num)
except ValueError:
continue
else:
filtered_data.append(a)

return filtered_data[start_num], filtered_data[end_num]

0 comments on commit 63577e9

Please sign in to comment.