From e56a2418493fa9d11af56addc0432e9da2c07f6f Mon Sep 17 00:00:00 2001 From: Ricter Z Date: Thu, 4 Feb 2016 18:47:23 +0800 Subject: [PATCH] use format --- nhentai/cmdline.py | 3 ++- nhentai/command.py | 2 -- nhentai/doujinshi.py | 15 +++++++++------ nhentai/downloader.py | 14 +++++++------- nhentai/parser.py | 20 ++++++++++---------- 5 files changed, 28 insertions(+), 26 deletions(-) diff --git a/nhentai/cmdline.py b/nhentai/cmdline.py index 2f276a3..2f0e754 100644 --- a/nhentai/cmdline.py +++ b/nhentai/cmdline.py @@ -12,7 +12,8 @@ import constant def banner(): - print(''' _ _ _ _ + logger.info('''nHentai: あなたも変態。 いいね? + _ _ _ _ _ __ | | | | ___ _ __ | |_ __ _(_) | '_ \| |_| |/ _ \ '_ \| __/ _` | | | | | | _ | __/ | | | || (_| | | diff --git a/nhentai/command.py b/nhentai/command.py index e22deff..a50e8eb 100644 --- a/nhentai/command.py +++ b/nhentai/command.py @@ -12,8 +12,6 @@ def main(): banner() options = cmd_parser() - logger.log(15, 'nHentai: あなたも変態。 いいね?') - doujinshi_ids = [] doujinshi_list = [] diff --git a/nhentai/doujinshi.py b/nhentai/doujinshi.py index a954b80..164edaa 100644 --- a/nhentai/doujinshi.py +++ b/nhentai/doujinshi.py @@ -1,5 +1,6 @@ # coding: utf-8 from __future__ import print_function +from tabulate import tabulate from constant import DETAIL_URL, IMAGE_URL from logger import logger @@ -16,14 +17,16 @@ class Doujinshi(object): self.url = '%s/%d' % (DETAIL_URL, self.id) def __repr__(self): - return '' % self.name + return ''.format(self.name) def show(self): - logger.info('Print doujinshi information') - print('Doujinshi: %s' % self.name) - print('Subtitle: %s' % self.subtitle) - print('URL: %s' % self.url) - print('Pages: %d' % self.pages) + table = [ + ["Doujinshi", self.name], + ["Subtitle", self.subtitle], + ["URL", self.url], + ["Pages", self.pages], + ] + logger.info(u'Print doujinshi information\n{}'.format(tabulate(table))) def download(self): logger.info('Start download doujinshi: %s' % self.name) diff --git a/nhentai/downloader.py b/nhentai/downloader.py index 402a7a8..4f292b9 100644 --- a/nhentai/downloader.py +++ b/nhentai/downloader.py @@ -24,7 +24,7 @@ class Downloader(object): self.timeout = timeout 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) try: with open(os.path.join(folder, filename), "wb") as f: @@ -37,12 +37,12 @@ class Downloader(object): f.write(chunk) except requests.HTTPError as e: 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) else: return None except Exception as e: - logger.critical('CRITICAL: %s' % str(e)) + logger.critical(str(e)) return None return url @@ -50,7 +50,7 @@ class Downloader(object): if not result: logger.critical('Too many errors occurred, quit.') raise SystemExit - logger.log(15, '%s download successfully' % result) + logger.log(15, '{} download successfully'.format(result)) def download(self, queue, folder=''): if not isinstance(folder, (str, unicode)): @@ -60,14 +60,14 @@ class Downloader(object): folder = os.path.join(self.path, folder) if not os.path.exists(folder): - logger.warn('Path \'%s\' not exist.' % folder) + logger.warn('Path \'{}\' not exist.'.format(folder)) try: os.makedirs(folder) except EnvironmentError as e: - logger.critical('Error: %s' % str(e)) + logger.critical('Error: {}'.format(str(e))) raise SystemExit else: - logger.warn('Path \'%s\' already exist.' % folder) + logger.warn('Path \'{}\' already exist.'.format(folder)) queue = [([url], {'folder': folder}) for url in queue] diff --git a/nhentai/parser.py b/nhentai/parser.py index 01786e9..cb19dfc 100644 --- a/nhentai/parser.py +++ b/nhentai/parser.py @@ -16,19 +16,20 @@ def request(method, url, **kwargs): 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({}) is not valid'.format(id_)) + + id_ = int(id_) + logger.log(15, 'Fetching doujinshi information of id {}'.format(id_)) doujinshi = dict() - doujinshi['id'] = id - url = '%s/%d/' % (constant.DETAIL_URL, id) + doujinshi['id'] = id_ + url = '{}/{}/'.format(constant.DETAIL_URL, id_) try: response = request('get', url).content except Exception as e: - logger.critical('%s%s' % tuple(e.message)) + logger.critical(str(e)) sys.exit() html = BeautifulSoup(response) @@ -36,7 +37,6 @@ def doujinshi_parser(id): title = doujinshi_info.find('h1').text subtitle = doujinshi_info.find('h2') - doujinshi['name'] = title doujinshi['subtitle'] = subtitle.text if subtitle else '' @@ -59,7 +59,7 @@ def doujinshi_parser(id): def search_parser(keyword, page): - logger.debug('Searching doujinshis of keyword %s' % keyword) + logger.debug('Searching doujinshis of keyword {}'.format(keyword)) result = [] try: response = request('get', url=constant.SEARCH_URL, params={'q': keyword, 'page': page}).content