]> git.lizzy.rs Git - nhentai.git/commitdiff
Merge pull request #163 from Nontre12/dev-page-range
authorRicter Zheng <RicterZheng@gmail.com>
Sat, 10 Oct 2020 16:58:57 +0000 (00:58 +0800)
committerGitHub <noreply@github.com>
Sat, 10 Oct 2020 16:58:57 +0000 (00:58 +0800)
Added --page-all option to download all search results

nhentai/cmdline.py
nhentai/command.py
nhentai/parser.py

index ac52a35ee88f13b1bdb7a91531fa9dc7fb4ce6ae..39787cd6f28a2123f25cc3c2ba5f05e7948f62d7 100644 (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', '--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',
index 5222951b1db5d7ec5dbd0751bdc7c85de01808bb..f22e94626bdb4fd832a78f6f91cf31d22e7b4168 100644 (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
@@ -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:
index fd08d33af98483e3cb772963b96e780345b6dc55..bd60e7a003fb9c479485923082142fe70a2c505e 100644 (file)
@@ -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_))