Added --page-all option to download all search results

This commit is contained in:
Nontre12 2020-10-10 18:29:00 +02:00
parent b82201ff27
commit be05b9c0eb
3 changed files with 43 additions and 2 deletions

View File

@ -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', type='int', dest='page', action='store', default=1,
help='page number of search results')
parser.add_option('--page-range', type='string', dest='page_range', action='store',

View File

@ -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
@ -37,6 +37,13 @@ def main():
doujinshis = favorites_parser(options.page_range)
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:

View File

@ -215,7 +215,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'))
@ -247,6 +247,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_))