use format

This commit is contained in:
Ricter Z 2016-02-04 18:47:23 +08:00
parent 376cca82a3
commit e56a241849
5 changed files with 28 additions and 26 deletions

View File

@ -12,7 +12,8 @@ import constant
def banner():
print(''' _ _ _ _
logger.info('''nHentai: あなたも変態。 いいね?
_ _ _ _
_ __ | | | | ___ _ __ | |_ __ _(_)
| '_ \| |_| |/ _ \ '_ \| __/ _` | |
| | | | _ | __/ | | | || (_| | |

View File

@ -12,8 +12,6 @@ def main():
banner()
options = cmd_parser()
logger.log(15, 'nHentai: あなたも変態。 いいね?')
doujinshi_ids = []
doujinshi_list = []

View File

@ -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 '<Doujinshi: %s>' % self.name
return '<Doujinshi: {}>'.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)

View File

@ -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]

View File

@ -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