mirror of
				https://github.com/RicterZ/nhentai.git
				synced 2025-11-04 11:00:55 +01:00 
			
		
		
		
	use format
This commit is contained in:
		@@ -12,7 +12,8 @@ import constant
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def banner():
 | 
					def banner():
 | 
				
			||||||
    print('''       _   _            _        _
 | 
					    logger.info('''nHentai: あなたも変態。 いいね?
 | 
				
			||||||
 | 
					       _   _            _        _
 | 
				
			||||||
 _ __ | | | | ___ _ __ | |_ __ _(_)
 | 
					 _ __ | | | | ___ _ __ | |_ __ _(_)
 | 
				
			||||||
| '_ \| |_| |/ _ \ '_ \| __/ _` | |
 | 
					| '_ \| |_| |/ _ \ '_ \| __/ _` | |
 | 
				
			||||||
| | | |  _  |  __/ | | | || (_| | |
 | 
					| | | |  _  |  __/ | | | || (_| | |
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -12,8 +12,6 @@ def main():
 | 
				
			|||||||
    banner()
 | 
					    banner()
 | 
				
			||||||
    options = cmd_parser()
 | 
					    options = cmd_parser()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    logger.log(15, 'nHentai: あなたも変態。 いいね?')
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    doujinshi_ids = []
 | 
					    doujinshi_ids = []
 | 
				
			||||||
    doujinshi_list = []
 | 
					    doujinshi_list = []
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,5 +1,6 @@
 | 
				
			|||||||
# coding: utf-8
 | 
					# coding: utf-8
 | 
				
			||||||
from __future__ import print_function
 | 
					from __future__ import print_function
 | 
				
			||||||
 | 
					from tabulate import tabulate
 | 
				
			||||||
from constant import DETAIL_URL, IMAGE_URL
 | 
					from constant import DETAIL_URL, IMAGE_URL
 | 
				
			||||||
from logger import logger
 | 
					from logger import logger
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -16,14 +17,16 @@ class Doujinshi(object):
 | 
				
			|||||||
        self.url = '%s/%d' % (DETAIL_URL, self.id)
 | 
					        self.url = '%s/%d' % (DETAIL_URL, self.id)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def __repr__(self):
 | 
					    def __repr__(self):
 | 
				
			||||||
        return '<Doujinshi: %s>' % self.name
 | 
					        return '<Doujinshi: {}>'.format(self.name)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def show(self):
 | 
					    def show(self):
 | 
				
			||||||
        logger.info('Print doujinshi information')
 | 
					        table = [
 | 
				
			||||||
        print('Doujinshi: %s' % self.name)
 | 
					            ["Doujinshi", self.name],
 | 
				
			||||||
        print('Subtitle: %s' % self.subtitle)
 | 
					            ["Subtitle", self.subtitle],
 | 
				
			||||||
        print('URL: %s' % self.url)
 | 
					            ["URL", self.url],
 | 
				
			||||||
        print('Pages: %d' % self.pages)
 | 
					            ["Pages", self.pages],
 | 
				
			||||||
 | 
					        ]
 | 
				
			||||||
 | 
					        logger.info(u'Print doujinshi information\n{}'.format(tabulate(table)))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def download(self):
 | 
					    def download(self):
 | 
				
			||||||
        logger.info('Start download doujinshi: %s' % self.name)
 | 
					        logger.info('Start download doujinshi: %s' % self.name)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -24,7 +24,7 @@ class Downloader(object):
 | 
				
			|||||||
        self.timeout = timeout
 | 
					        self.timeout = timeout
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def _download(self, url, folder='', filename='', retried=False):
 | 
					    def _download(self, url, folder='', filename='', retried=False):
 | 
				
			||||||
        logger.info('Start downloading: %s ...' % url)
 | 
					        logger.info('Start downloading: {} ...'.format(url))
 | 
				
			||||||
        filename = filename if filename else os.path.basename(urlparse(url).path)
 | 
					        filename = filename if filename else os.path.basename(urlparse(url).path)
 | 
				
			||||||
        try:
 | 
					        try:
 | 
				
			||||||
            with open(os.path.join(folder, filename), "wb") as f:
 | 
					            with open(os.path.join(folder, filename), "wb") as f:
 | 
				
			||||||
@@ -37,12 +37,12 @@ class Downloader(object):
 | 
				
			|||||||
                        f.write(chunk)
 | 
					                        f.write(chunk)
 | 
				
			||||||
        except requests.HTTPError as e:
 | 
					        except requests.HTTPError as e:
 | 
				
			||||||
            if not retried:
 | 
					            if not retried:
 | 
				
			||||||
                logger.error('Error: %s, retrying' % str(e))
 | 
					                logger.error('Error: {}, retrying'.format(str(e)))
 | 
				
			||||||
                return self._download(url=url, folder=folder, filename=filename, retried=True)
 | 
					                return self._download(url=url, folder=folder, filename=filename, retried=True)
 | 
				
			||||||
            else:
 | 
					            else:
 | 
				
			||||||
                return None
 | 
					                return None
 | 
				
			||||||
        except Exception as e:
 | 
					        except Exception as e:
 | 
				
			||||||
            logger.critical('CRITICAL: %s' % str(e))
 | 
					            logger.critical(str(e))
 | 
				
			||||||
            return None
 | 
					            return None
 | 
				
			||||||
        return url
 | 
					        return url
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -50,7 +50,7 @@ class Downloader(object):
 | 
				
			|||||||
        if not result:
 | 
					        if not result:
 | 
				
			||||||
            logger.critical('Too many errors occurred, quit.')
 | 
					            logger.critical('Too many errors occurred, quit.')
 | 
				
			||||||
            raise SystemExit
 | 
					            raise SystemExit
 | 
				
			||||||
        logger.log(15, '%s download successfully' % result)
 | 
					        logger.log(15, '{} download successfully'.format(result))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def download(self, queue, folder=''):
 | 
					    def download(self, queue, folder=''):
 | 
				
			||||||
        if not isinstance(folder, (str, unicode)):
 | 
					        if not isinstance(folder, (str, unicode)):
 | 
				
			||||||
@@ -60,14 +60,14 @@ class Downloader(object):
 | 
				
			|||||||
            folder = os.path.join(self.path, folder)
 | 
					            folder = os.path.join(self.path, folder)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if not os.path.exists(folder):
 | 
					        if not os.path.exists(folder):
 | 
				
			||||||
            logger.warn('Path \'%s\' not exist.' % folder)
 | 
					            logger.warn('Path \'{}\' not exist.'.format(folder))
 | 
				
			||||||
            try:
 | 
					            try:
 | 
				
			||||||
                os.makedirs(folder)
 | 
					                os.makedirs(folder)
 | 
				
			||||||
            except EnvironmentError as e:
 | 
					            except EnvironmentError as e:
 | 
				
			||||||
                logger.critical('Error: %s' % str(e))
 | 
					                logger.critical('Error: {}'.format(str(e)))
 | 
				
			||||||
                raise SystemExit
 | 
					                raise SystemExit
 | 
				
			||||||
        else:
 | 
					        else:
 | 
				
			||||||
            logger.warn('Path \'%s\' already exist.' % folder)
 | 
					            logger.warn('Path \'{}\' already exist.'.format(folder))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        queue = [([url], {'folder': folder}) for url in queue]
 | 
					        queue = [([url], {'folder': folder}) for url in queue]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -16,19 +16,20 @@ def request(method, url, **kwargs):
 | 
				
			|||||||
    return requests.__dict__[method](url, proxies=constant.PROXY, **kwargs)
 | 
					    return requests.__dict__[method](url, proxies=constant.PROXY, **kwargs)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def doujinshi_parser(id):
 | 
					def doujinshi_parser(id_):
 | 
				
			||||||
    if not isinstance(id, (int, )) and (isinstance(id, (str, )) and not id.isdigit()):
 | 
					    if not isinstance(id_, (int,)) and (isinstance(id_, (str,)) and not id_.isdigit()):
 | 
				
			||||||
        raise Exception('Doujinshi id(%s) is not valid' % str(id))
 | 
					        raise Exception('Doujinshi id({}) is not valid'.format(id_))
 | 
				
			||||||
    id = int(id)
 | 
					
 | 
				
			||||||
    logger.debug('Fetching doujinshi information of id %d' % id)
 | 
					    id_ = int(id_)
 | 
				
			||||||
 | 
					    logger.log(15, 'Fetching doujinshi information of id {}'.format(id_))
 | 
				
			||||||
    doujinshi = dict()
 | 
					    doujinshi = dict()
 | 
				
			||||||
    doujinshi['id'] = id
 | 
					    doujinshi['id'] = id_
 | 
				
			||||||
    url = '%s/%d/' % (constant.DETAIL_URL, id)
 | 
					    url = '{}/{}/'.format(constant.DETAIL_URL, id_)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    try:
 | 
					    try:
 | 
				
			||||||
        response = request('get', url).content
 | 
					        response = request('get', url).content
 | 
				
			||||||
    except Exception as e:
 | 
					    except Exception as e:
 | 
				
			||||||
        logger.critical('%s%s' % tuple(e.message))
 | 
					        logger.critical(str(e))
 | 
				
			||||||
        sys.exit()
 | 
					        sys.exit()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    html = BeautifulSoup(response)
 | 
					    html = BeautifulSoup(response)
 | 
				
			||||||
@@ -36,7 +37,6 @@ def doujinshi_parser(id):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    title = doujinshi_info.find('h1').text
 | 
					    title = doujinshi_info.find('h1').text
 | 
				
			||||||
    subtitle = doujinshi_info.find('h2')
 | 
					    subtitle = doujinshi_info.find('h2')
 | 
				
			||||||
 | 
					 | 
				
			||||||
    doujinshi['name'] = title
 | 
					    doujinshi['name'] = title
 | 
				
			||||||
    doujinshi['subtitle'] = subtitle.text if subtitle else ''
 | 
					    doujinshi['subtitle'] = subtitle.text if subtitle else ''
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -59,7 +59,7 @@ def doujinshi_parser(id):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def search_parser(keyword, page):
 | 
					def search_parser(keyword, page):
 | 
				
			||||||
    logger.debug('Searching doujinshis of keyword %s' % keyword)
 | 
					    logger.debug('Searching doujinshis of keyword {}'.format(keyword))
 | 
				
			||||||
    result = []
 | 
					    result = []
 | 
				
			||||||
    try:
 | 
					    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}).content
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user