]> git.lizzy.rs Git - nhentai.git/blobdiff - nhentai/parser.py
resoved issue #249
[nhentai.git] / nhentai / parser.py
index 0df571005a29a0db1f908e4bd026db0d3bf5fd56..3ebcabb1666c98dc0b4827ca9fd24365fa33de51 100644 (file)
@@ -1,5 +1,4 @@
 # coding: utf-8
-from __future__ import unicode_literals, print_function
 
 import os
 import re
@@ -116,7 +115,7 @@ def doujinshi_parser(id_):
 
     try:
         response = request('get', url)
-        if response.status_code in (200,):
+        if response.status_code in (200, ):
             response = response.content
         elif response.status_code in (404,):
             logger.error("Doujinshi with id {0} cannot be found".format(id_))
@@ -127,20 +126,23 @@ def doujinshi_parser(id_):
             return doujinshi_parser(str(id_))
 
     except Exception as e:
-        logger.warn('Error: {}, ignored'.format(str(e)))
+        logger.warning('Error: {}, ignored'.format(str(e)))
         return None
 
     html = BeautifulSoup(response, 'html.parser')
     doujinshi_info = html.find('div', attrs={'id': 'info'})
 
     title = doujinshi_info.find('h1').text
+    pretty_name = doujinshi_info.find('h1').find('span', attrs={'class': 'pretty'}).text
     subtitle = doujinshi_info.find('h2')
 
     doujinshi['name'] = title
+    doujinshi['pretty_name'] = pretty_name
     doujinshi['subtitle'] = subtitle.text if subtitle else ''
 
     doujinshi_cover = html.find('div', attrs={'id': 'cover'})
-    img_id = re.search('/galleries/([\d]+)/cover\.(jpg|png|gif)$', doujinshi_cover.a.img.attrs['data-src'])
+    img_id = re.search('/galleries/([0-9]+)/cover.(jpg|png|gif)$',
+                       doujinshi_cover.a.img.attrs['data-src'])
 
     ext = []
     for i in html.find_all('div', attrs={'class': 'thumb-container'}):
@@ -181,7 +183,7 @@ def old_search_parser(keyword, sorting='date', page=1):
 
     result = _get_title_and_id(response)
     if not result:
-        logger.warn('Not found anything of keyword {}'.format(keyword))
+        logger.warning('Not found anything of keyword {}'.format(keyword))
 
     return result
 
@@ -198,6 +200,7 @@ def print_doujinshi(doujinshi_list):
 def search_parser(keyword, sorting, page, is_page_all=False):
     # keyword = '+'.join([i.strip().replace(' ', '-').lower() for i in keyword.split(',')])
     result = []
+    response = None
     if not page:
         page = [1]
 
@@ -207,6 +210,7 @@ def search_parser(keyword, sorting, page, is_page_all=False):
         page = range(1, init_response['num_pages']+1)
 
     total = '/{0}'.format(page[-1]) if is_page_all else ''
+    not_exists_persist = False
     for p in page:
         i = 0
 
@@ -218,20 +222,23 @@ def search_parser(keyword, sorting, page, is_page_all=False):
                 response = request('get', url.replace('%2B', '+')).json()
             except Exception as e:
                 logger.critical(str(e))
-
+                response = None
             break
 
-        if 'result' not in response:
-            logger.warn('No result in response in page {}'.format(p))
-            break
+        if response is None or 'result' not in response:
+            logger.warning('No result in response in page {}'.format(p))
+            if not_exists_persist is True:
+                break
+            continue
 
         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})
 
+        not_exists_persist = False
         if not result:
-            logger.warn('No results for keywords {}'.format(keyword))
+            logger.warning('No results for keywords {}'.format(keyword))
 
     return result