]> git.lizzy.rs Git - nhentai.git/blobdiff - nhentai/parser.py
project is now Py3 and Py2 compatible
[nhentai.git] / nhentai / parser.py
index 01786e934d2ddfd58c7d623152bcfe38c3f8ae30..f8977540bde2fd2c54ba9e35bdd81ace4f3fc9b2 100644 (file)
@@ -1,35 +1,37 @@
 # coding: utf-8
 from __future__ import print_function
-import sys
+
+from bs4 import BeautifulSoup
 import re
 import requests
-from bs4 import BeautifulSoup
-import constant
-from logger import logger
 from tabulate import tabulate
 
+import nhentai.constant as constant
+from nhentai.logger import logger
+
 
 def request(method, url, **kwargs):
     if not hasattr(requests, method):
-        raise AttributeError('\'requests\' object has no attribute \'{}\''.format(method))
+        raise AttributeError('\'requests\' object has no attribute \'{0}\''.format(method))
 
     return requests.__dict__[method](url, proxies=constant.PROXY, **kwargs)
 
 
-def doujinshi_parser(id):
-    if not isinstance(id, (int, )) and (isinstance(id, (str, )) and not id.isdigit()):
-        raise Exception('Doujinshi id(%s) is not valid' % str(id))
-    id = int(id)
-    logger.debug('Fetching doujinshi information of id %d' % id)
+def 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_))
+
+    id_ = int(id_)
+    logger.log(15, 'Fetching doujinshi information of id {0}'.format(id_))
     doujinshi = dict()
-    doujinshi['id'] = id
-    url = '%s/%d/' % (constant.DETAIL_URL, id)
+    doujinshi['id'] = id_
+    url = '{0}/{1}/'.format(constant.DETAIL_URL, id_)
 
     try:
         response = request('get', url).content
     except Exception as e:
-        logger.critical('%s%s' % tuple(e.message))
-        sys.exit()
+        logger.critical(str(e))
+        exit(1)
 
     html = BeautifulSoup(response)
     doujinshi_info = html.find('div', attrs={'id': 'info'})
@@ -44,7 +46,8 @@ def doujinshi_parser(id):
     img_id = re.search('/galleries/([\d]+)/cover\.(jpg|png)$', doujinshi_cover.a.img['src'])
     if not img_id:
         logger.critical('Tried yo get image id failed')
-        sys.exit()
+        exit(1)
+
     doujinshi['img_id'] = img_id.group(1)
     doujinshi['ext'] = img_id.group(2)
 
@@ -55,18 +58,29 @@ def doujinshi_parser(id):
             pages = pages.group(1)
             break
     doujinshi['pages'] = int(pages)
+
+    # gain information of the doujinshi
+    information_fields = doujinshi_info.find_all('div', attrs={'class': 'field-name'})
+    needed_fields = ['Characters', 'Artists', 'Language', 'Tags']
+    for field in information_fields:
+        field_name = field.contents[0].strip().strip(':')
+        if field_name in needed_fields:
+            data = [sub_field.contents[0].strip() for sub_field in
+                    field.find_all('a', attrs={'class': 'tag'})]
+            doujinshi[field_name.lower()] = ', '.join(data)
+
     return doujinshi
 
 
 def search_parser(keyword, page):
-    logger.debug('Searching doujinshis of keyword %s' % keyword)
+    logger.debug('Searching doujinshis of keyword {0}'.format(keyword))
     result = []
     try:
         response = request('get', url=constant.SEARCH_URL, params={'q': keyword, 'page': page}).content
     except requests.ConnectionError as e:
         logger.critical(e)
         logger.warn('If you are in China, please configure the proxy to fu*k GFW.')
-        raise SystemExit
+        exit(1)
 
     html = BeautifulSoup(response)
     doujinshi_search_result = html.find_all('div', attrs={'class': 'gallery'})