]> git.lizzy.rs Git - nhentai.git/commitdiff
Added sorting option
authorWaifu <wifu@protonmail.com>
Mon, 29 Jul 2019 07:11:45 +0000 (09:11 +0200)
committerWaifu <wifu@protonmail.com>
Mon, 29 Jul 2019 07:11:45 +0000 (09:11 +0200)
nhentai/__init__.py
nhentai/cmdline.py
nhentai/command.py
nhentai/parser.py

index 2bc558ab22481eaf848524366e71707f41c5984f..f0d779615becb1d79ce08abb5a3a1ae2bca5184d 100644 (file)
@@ -1,3 +1,3 @@
-__version__ = '0.3.6'
+__version__ = '0.3.7'
 __author__ = 'RicterZ'
 __email__ = 'ricterzheng@gmail.com'
index 2a58736f7378ff8954412a759346c81601be25da..475c662ff3f74b5f7588dd2e38d435f0254ddf24 100644 (file)
@@ -58,6 +58,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('--sorting', type='string', dest='sorting', action='store', default='date',
+                      help='sorting of doujinshi, e.g. date/popular')
 
     # download options
     parser.add_option('--output', '-o', type='string', dest='output_dir', action='store', default='',
index 10176ea8deefae192892412d247e4fa39707ea00..9d3cb686224b57cea563544a65adf4c108dcbb8c 100644 (file)
@@ -40,13 +40,13 @@ def main():
             doujinshi_ids = map(lambda d: d['id'], doujinshis)
 
     elif options.tag:
-        doujinshis = tag_parser(options.tag, max_page=options.max_page)
+        doujinshis = tag_parser(options.tag, options.sorting, max_page=options.max_page)
         print_doujinshi(doujinshis)
         if options.is_download and doujinshis:
             doujinshi_ids = map(lambda d: d['id'], doujinshis)
 
     elif options.keyword:
-        doujinshis = search_parser(options.keyword, options.page)
+        doujinshis = search_parser(options.keyword, options.sorting, options.page)
         print_doujinshi(doujinshis)
         if options.is_download:
             doujinshi_ids = map(lambda d: d['id'], doujinshis)
index cb53cc8894ac906f22d1d7c27cbd6b109f16dac4..b6a23e2cc3b01e9bf263e33295b2153eb3ba8234 100644 (file)
@@ -169,10 +169,10 @@ def doujinshi_parser(id_):
     return doujinshi
 
 
-def search_parser(keyword, page):
+def search_parser(keyword, sorting, page):
     logger.debug('Searching doujinshis of keyword {0}'.format(keyword))
     try:
-        response = request('get', url=constant.SEARCH_URL, params={'q': keyword, 'page': page}).content
+        response = request('get', url=constant.SEARCH_URL, params={'q': keyword, 'page': page, 'sort': sorting}).content
     except requests.ConnectionError as e:
         logger.critical(e)
         logger.warn('If you are in China, please configure the proxy to fu*k GFW.')
@@ -194,14 +194,17 @@ def print_doujinshi(doujinshi_list):
                 tabulate(tabular_data=doujinshi_list, headers=headers, tablefmt='rst'))
 
 
-def tag_parser(tag_name, max_page=1):
+def tag_parser(tag_name, sorting, max_page=1):
     result = []
     tag_name = tag_name.lower()
     tag_name = tag_name.replace(' ', '-')
 
+    if sorting == 'date':
+        sorting = ''
+
     for p in range(1, max_page + 1):
         logger.debug('Fetching page {0} for doujinshi with tag \'{1}\''.format(p, tag_name))
-        response = request('get', url='%s/%s/?page=%d' % (constant.TAG_URL, tag_name, p)).content
+        response = request('get', url='%s/%s/%s?page=%d' % (constant.TAG_URL, tag_name, sorting, p)).content
 
         result += _get_title_and_id(response)
         if not result:
@@ -214,13 +217,13 @@ def tag_parser(tag_name, max_page=1):
     return result
 
 
-def __api_suspended_search_parser(keyword, page):
+def __api_suspended_search_parser(keyword, sorting, page):
     logger.debug('Searching doujinshis using keywords {0}'.format(keyword))
     result = []
     i = 0
     while i < 5:
         try:
-            response = request('get', url=constant.SEARCH_URL, params={'query': keyword, 'page': page}).json()
+            response = request('get', url=constant.SEARCH_URL, params={'query': keyword, 'page': page, 'sort': sorting}).json()
         except Exception as e:
             i += 1
             if not i < 5:
@@ -244,10 +247,10 @@ def __api_suspended_search_parser(keyword, page):
     return result
 
 
-def __api_suspended_tag_parser(tag_id, max_page=1):
+def __api_suspended_tag_parser(tag_id, sorting, max_page=1):
     logger.info('Searching for doujinshi with tag id {0}'.format(tag_id))
     result = []
-    response = request('get', url=constant.TAG_API_URL, params={'sort': 'popular', 'tag_id': tag_id}).json()
+    response = request('get', url=constant.TAG_API_URL, params={'sort': sorting, 'tag_id': tag_id}).json()
     page = max_page if max_page <= response['num_pages'] else int(response['num_pages'])
 
     for i in range(1, page + 1):
@@ -255,7 +258,7 @@ def __api_suspended_tag_parser(tag_id, max_page=1):
 
         if page != 1:
             response = request('get', url=constant.TAG_API_URL,
-                               params={'sort': 'popular', 'tag_id': tag_id}).json()
+                               params={'sort': sorting, 'tag_id': tag_id}).json()
     for row in response['result']:
         title = row['title']['english']
         title = title[:85] + '..' if len(title) > 85 else title