From: myzWILLmake Date: Thu, 6 Feb 2020 17:32:51 +0000 (+0800) Subject: add page_range option for favorites X-Git-Tag: 0.3.7~11^2 X-Git-Url: https://git.lizzy.rs/?a=commitdiff_plain;h=43a9b981ddc92c5f06be84b83898eb9dd6973697;p=nhentai.git add page_range option for favorites --- diff --git a/nhentai/cmdline.py b/nhentai/cmdline.py index 10f5ecd..f8e7679 100644 --- a/nhentai/cmdline.py +++ b/nhentai/cmdline.py @@ -63,6 +63,8 @@ def cmd_parser(): help='page number of search results') parser.add_option('--max-page', type='int', dest='max_page', action='store', default=1, help='The max page when recursive download tagged doujinshi') + parser.add_option('--page-range', type='string', dest='page_range', action='store', + help='page range of favorites. e.g. 1,2-5,14') parser.add_option('--sorting', dest='sorting', action='store', default='date', help='sorting of doujinshi (date / popular)', choices=['date', 'popular']) diff --git a/nhentai/command.py b/nhentai/command.py index 8009351..deb9071 100644 --- a/nhentai/command.py +++ b/nhentai/command.py @@ -35,7 +35,7 @@ def main(): if not options.is_download: logger.warning('You do not specify --download option') - doujinshis = favorites_parser() + doujinshis = favorites_parser(options.page_range) print_doujinshi(doujinshis) if options.is_download and doujinshis: doujinshi_ids = [i['id'] for i in doujinshis] diff --git a/nhentai/parser.py b/nhentai/parser.py index 57e94bf..212461f 100644 --- a/nhentai/parser.py +++ b/nhentai/parser.py @@ -65,7 +65,7 @@ def _get_title_and_id(response): return result -def favorites_parser(): +def favorites_parser(page_range=''): result = [] html = BeautifulSoup(request('get', constant.FAV_URL).content, 'html.parser') count = html.find('span', attrs={'class': 'count'}) @@ -89,7 +89,12 @@ def favorites_parser(): if os.getenv('DEBUG'): pages = 1 - for page in range(1, pages + 1): + page_range_list = range(1, pages + 1) + if page_range: + logger.info('page range is {0}'.format(page_range)) + page_range_list = page_range_parser(page_range, pages) + + for page in page_range_list: try: logger.info('Getting doujinshi ids of page %d' % page) resp = request('get', constant.FAV_URL + '?page=%d' % page).content @@ -100,6 +105,30 @@ def favorites_parser(): return result +def page_range_parser(page_range, max_page_num): + pages = set() + ranges = str.split(page_range, ',') + for range_str in ranges: + idx = range_str.find('-') + if idx == -1: + try: + page = int(range_str) + if page <= max_page_num: + pages.add(page) + except ValueError: + logger.error('page range({0}) is not valid'.format(page_range)) + else: + try: + left = int(range_str[:idx]) + right = int(range_str[idx+1:]) + if right > max_page_num: + right = max_page_num + for page in range(left, right+1): + pages.add(page) + except ValueError: + logger.error('page range({0}) is not valid'.format(page_range)) + + return list(pages) def doujinshi_parser(id_): if not isinstance(id_, (int,)) and (isinstance(id_, (str,)) and not id_.isdigit()):