X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=nhentai%2Fparser.py;h=deb40e2497f3496730fbcc27bb6be6ca25f4b7db;hb=e306d50b7eed8ff8b0376d9ad2c1ed69d23532dd;hp=e9cb4c56a5c085ce6d139d5046934022e3c9e6b2;hpb=3a52e8a8bcf26791dd24b33c89f27b430387b925;p=nhentai.git diff --git a/nhentai/parser.py b/nhentai/parser.py index e9cb4c5..deb40e2 100644 --- a/nhentai/parser.py +++ b/nhentai/parser.py @@ -1,19 +1,20 @@ # coding: utf-8 -from __future__ import print_function -import sys +from __future__ import unicode_literals, print_function + +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 \'{0}\''.format(method)) - return requests.__dict__[method](url, proxies=constant.PROXY, **kwargs) + return requests.__dict__[method](url, proxies=constant.PROXY, verify=False, **kwargs) def doujinshi_parser(id_): @@ -30,9 +31,9 @@ def doujinshi_parser(id_): response = request('get', url).content except Exception as e: logger.critical(str(e)) - sys.exit() + exit(1) - html = BeautifulSoup(response) + html = BeautifulSoup(response, 'html.parser') doujinshi_info = html.find('div', attrs={'id': 'info'}) title = doujinshi_info.find('h1').text @@ -42,10 +43,11 @@ def doujinshi_parser(id_): doujinshi['subtitle'] = subtitle.text if subtitle else '' doujinshi_cover = html.find('div', attrs={'id': 'cover'}) - img_id = re.search('/galleries/([\d]+)/cover\.(jpg|png)$', doujinshi_cover.a.img['src']) + img_id = re.search('/galleries/([\d]+)/cover\.(jpg|png)$', doujinshi_cover.a.img.attrs['data-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) @@ -80,7 +82,7 @@ def search_parser(keyword, page): logger.warn('If you are in China, please configure the proxy to fu*k GFW.') exit(1) - html = BeautifulSoup(response) + html = BeautifulSoup(response, 'html.parser') doujinshi_search_result = html.find_all('div', attrs={'class': 'gallery'}) for doujinshi in doujinshi_search_result: doujinshi_container = doujinshi.find('div', attrs={'class': 'caption'}) @@ -88,13 +90,16 @@ def search_parser(keyword, page): title = (title[:85] + '..') if len(title) > 85 else title id_ = re.search('/g/(\d+)/', doujinshi.a['href']).group(1) result.append({'id': id_, 'title': title}) + if not result: + logger.warn('Not found anything of keyword {}'.format(keyword)) + return result def print_doujinshi(doujinshi_list): if not doujinshi_list: return - doujinshi_list = [i.values() for i in doujinshi_list] + doujinshi_list = [(i['id'], i['title']) for i in doujinshi_list] headers = ['id', 'doujinshi'] logger.info('Search Result\n' + tabulate(tabular_data=doujinshi_list, headers=headers, tablefmt='rst'))