From cb0972c2eba5e296fff4c5f43f47baf2bb3f5766 Mon Sep 17 00:00:00 2001 From: ricterz Date: Tue, 21 Apr 2015 23:24:22 +0800 Subject: [PATCH] feature of download dojinshi --- hentai/cmdline.py | 2 ++ hentai/dojinshi.py | 2 +- hentai/downloader.py | 12 +++++++----- nhentai.py | 19 +++++++++++-------- 4 files changed, 21 insertions(+), 14 deletions(-) diff --git a/hentai/cmdline.py b/hentai/cmdline.py index 0a596e3..6bac081 100644 --- a/hentai/cmdline.py +++ b/hentai/cmdline.py @@ -44,4 +44,6 @@ def cmd_parser(): logger.critical(u'并没有做这个功能_(:3」∠)_') sys.exit() + args.ids = (args.id, ) if not args.ids else args.ids + return args diff --git a/hentai/dojinshi.py b/hentai/dojinshi.py index 36aa359..56b1c0e 100644 --- a/hentai/dojinshi.py +++ b/hentai/dojinshi.py @@ -32,7 +32,7 @@ class Dojinshi(object): download_queue.put('%s/%d/%d.%s' % (IMAGE_URL, int(self.img_id), i, self.ext)) self.downloader.download(download_queue, self.id) else: - raise Exception('Downloader has not be loaded') + logger.critical('Downloader has not be loaded') if __name__ == '__main__': diff --git a/hentai/downloader.py b/hentai/downloader.py index 9e714bf..3c8295a 100644 --- a/hentai/downloader.py +++ b/hentai/downloader.py @@ -7,9 +7,15 @@ import requests from urlparse import urlparse from hentai.logger import logger -shutdown = threading.Event() class Downloader(object): + _instance = None + + def __new__(cls, *args, **kwargs): + if not cls._instance: + cls._instance = super(Downloader, cls).__new__(cls, *args, **kwargs) + return cls._instance + def __init__(self, path='', thread=1): if not isinstance(thread, (int, )) or thread < 1 or thread > 10: raise ValueError('Invalid threads count') @@ -18,9 +24,6 @@ class Downloader(object): self.threads = [] def _download(self, url, folder='', filename=''): - if shutdown.is_set(): - return - if not os.path.exists(folder): try: os.mkdir(folder) @@ -83,4 +86,3 @@ class Downloader(object): thread.join() logger.log(15, u'🍺 All done, saved to \'%s\'!' % folder) - diff --git a/nhentai.py b/nhentai.py index 00d15e8..0fefc1b 100644 --- a/nhentai.py +++ b/nhentai.py @@ -12,23 +12,26 @@ __version__ = '0.1' def main(): banner() options = cmd_parser() - dojinshi = None logger.log(15, 'nHentai: あなたも変態。 いいね?') - if options.id: - dojinshi_info = dojinshi_parser(options.id) - dojinshi = Dojinshi(**dojinshi_info) + + dojinshi_list = [] + if options.ids: + for id in options.ids: + dojinshi_info = dojinshi_parser(id) + dojinshi_list.append(Dojinshi(**dojinshi_info)) elif options.keyword: pass else: raise SystemExit if options.is_download: - dojinshi.downloader = Downloader(path=options.saved_path, - thread=options.threads) - dojinshi.download() + downloader = Downloader(path=options.saved_path, thread=options.threads) + for dojinshi in dojinshi_list: + dojinshi.downloader = downloader + dojinshi.download() else: - dojinshi.show() + map(lambda dojinshi: dojinshi.show(), dojinshi_list) if __name__ == '__main__':