diff --git a/nhentai/cmdline.py b/nhentai/cmdline.py index ac52a35..39787cd 100644 --- a/nhentai/cmdline.py +++ b/nhentai/cmdline.py @@ -54,6 +54,8 @@ def cmd_parser(): help='list or download your favorites.') # page options + parser.add_option('--page-all', dest='page_all', action='store_true', default=False, + help='all search results') parser.add_option('--page', '--page-range', type='string', dest='page', action='store', default='', help='page number of search results. e.g. 1,2-5,14') parser.add_option('--sorting', dest='sorting', action='store', default='recent', diff --git a/nhentai/command.py b/nhentai/command.py index 5222951..f22e946 100644 --- a/nhentai/command.py +++ b/nhentai/command.py @@ -6,7 +6,7 @@ import platform import time from nhentai.cmdline import cmd_parser, banner -from nhentai.parser import doujinshi_parser, search_parser, print_doujinshi, favorites_parser +from nhentai.parser import doujinshi_parser, search_parser, search_parser_all, print_doujinshi, favorites_parser from nhentai.doujinshi import Doujinshi from nhentai.downloader import Downloader from nhentai.logger import logger @@ -40,6 +40,13 @@ def main(): doujinshis = favorites_parser(page=page_list) + elif options.keyword and options.page_all: + from nhentai.constant import LANGUAGE + if LANGUAGE: + logger.info('Using default language: {0}'.format(LANGUAGE)) + options.keyword += ', language:{}'.format(LANGUAGE) + doujinshis = search_parser_all(options.keyword) + elif options.keyword: from nhentai.constant import LANGUAGE if LANGUAGE: diff --git a/nhentai/parser.py b/nhentai/parser.py index fd08d33..bd60e7a 100644 --- a/nhentai/parser.py +++ b/nhentai/parser.py @@ -189,7 +189,7 @@ def print_doujinshi(doujinshi_list): return doujinshi_list = [(i['id'], i['title']) for i in doujinshi_list] headers = ['id', 'doujinshi'] - logger.info('Search Result\n' + + logger.info('Search Result || Found %i doujinshis \n' % doujinshi_list.__len__() + tabulate(tabular_data=doujinshi_list, headers=headers, tablefmt='rst')) @@ -227,6 +227,38 @@ def search_parser(keyword, sorting, page): return result +def search_parser_all(keyword): + logger.debug('Searching doujinshis using keywords {0}'.format(keyword)) + + result = [] + + url = request('get', url=constant.SEARCH_URL, params={'query': keyword}).url + init_response = request('get', url.replace('%2B', '+')).json() + + for page in range(init_response['num_pages']): + try: + url = request('get', url=constant.SEARCH_URL, params={'query': keyword, 'page': page+1}).url + response = request('get', url.replace('%2B', '+')).json() + + print('Obtained %d / %d pages.' % (page+1, init_response['num_pages']), end='\r') + + except Exception as e: + logger.critical(str(e)) + + if 'result' not in response: + raise Exception('No result in response') + + 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('No results for keywords {}'.format(keyword)) + + return result + + def __api_suspended_doujinshi_parser(id_): if not isinstance(id_, (int,)) and (isinstance(id_, (str,)) and not id_.isdigit()): raise Exception('Doujinshi id({0}) is not valid'.format(id_))