From f4afcd549e63114d16ceab3a1026cd617da65b9e Mon Sep 17 00:00:00 2001
From: Waifu <wifu@protonmail.com>
Date: Mon, 29 Jul 2019 09:11:45 +0200
Subject: [PATCH] Added sorting option

---
 nhentai/__init__.py |  2 +-
 nhentai/cmdline.py  |  2 ++
 nhentai/command.py  |  4 ++--
 nhentai/parser.py   | 21 ++++++++++++---------
 4 files changed, 17 insertions(+), 12 deletions(-)

diff --git a/nhentai/__init__.py b/nhentai/__init__.py
index 2bc558a..f0d7796 100644
--- a/nhentai/__init__.py
+++ b/nhentai/__init__.py
@@ -1,3 +1,3 @@
-__version__ = '0.3.6'
+__version__ = '0.3.7'
 __author__ = 'RicterZ'
 __email__ = 'ricterzheng@gmail.com'
diff --git a/nhentai/cmdline.py b/nhentai/cmdline.py
index 2a58736..475c662 100644
--- a/nhentai/cmdline.py
+++ b/nhentai/cmdline.py
@@ -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='',
diff --git a/nhentai/command.py b/nhentai/command.py
index 10176ea..9d3cb68 100644
--- a/nhentai/command.py
+++ b/nhentai/command.py
@@ -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)
diff --git a/nhentai/parser.py b/nhentai/parser.py
index cb53cc8..b6a23e2 100644
--- a/nhentai/parser.py
+++ b/nhentai/parser.py
@@ -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