From b8ea917db27e8d3aeb859ab0d8d80f7abb67b455 Mon Sep 17 00:00:00 2001 From: Ricter Z Date: Fri, 24 Aug 2018 23:55:34 +0800 Subject: [PATCH] max page #26 --- nhentai/cmdline.py | 2 ++ nhentai/command.py | 2 +- nhentai/parser.py | 17 ++++++++++++----- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/nhentai/cmdline.py b/nhentai/cmdline.py index 5be7e0b..4a12044 100644 --- a/nhentai/cmdline.py +++ b/nhentai/cmdline.py @@ -47,6 +47,8 @@ def cmd_parser(): parser.add_option('--page', type='int', dest='page', action='store', default=1, help='page number of search result') parser.add_option('--tag', type='string', dest='tag', action='store', help='download doujinshi by tag') + 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('--output', type='string', dest='output_dir', action='store', default='', help='output dir') parser.add_option('--threads', '-t', type='int', dest='threads', action='store', default=5, diff --git a/nhentai/command.py b/nhentai/command.py index b1492a6..c62bc43 100644 --- a/nhentai/command.py +++ b/nhentai/command.py @@ -30,7 +30,7 @@ def main(): if options.tag: tag_id = tag_guessing(options.tag) if tag_id: - doujinshis = tag_parser(tag_id) + doujinshis = tag_parser(tag_id, max_page=options.max_page) print_doujinshi(doujinshis) if options.is_download: doujinshi_ids = map(lambda d: d['id'], doujinshis) diff --git a/nhentai/parser.py b/nhentai/parser.py index bea6050..d1bd304 100644 --- a/nhentai/parser.py +++ b/nhentai/parser.py @@ -161,15 +161,22 @@ def print_doujinshi(doujinshi_list): logger.info('Search Result\n{}'.format(data)) -def tag_parser(tag_id): +def tag_parser(tag_id, max_page=1): logger.info('Get doujinshi of tag id: {0}'.format(tag_id)) result = [] response = request('get', url=constant.TAG_API_URL, params={'sort': 'popular', 'tag_id': tag_id}).json() + page = max_page if max_page <= response['num_pages'] else int(response['num_pages']) - for row in response['result']: - title = row['title']['english'] - title = title[:85] + '..' if len(title) > 85 else title - result.append({'id': row['id'], 'title': title}) + for i in range(1, page+1): + logger.info('Get page {} ...'.format(i)) + + if page != 1: + response = request('get', url=constant.TAG_API_URL, params={'sort': 'popular', 'tag_id': tag_id}).json() + + for row in response['result']: + title = row['title']['english'] + title = title[:85] + '..' if len(title) > 85 else title + result.append({'id': row['id'], 'title': title}) if not result: logger.warn('Not found anything of tag id {}'.format(tag_id))