From 0f89ff4d6321036d6667bf4f33e6a7c256556a5e Mon Sep 17 00:00:00 2001 From: Dhruvan Ganesh Date: Wed, 10 Aug 2016 15:41:51 +0530 Subject: [PATCH 1/4] implemented Py2 & Py3 compatible Singleton --- nhentai/downloader.py | 8 ++------ nhentai/utils.py | 10 ++++++++++ 2 files changed, 12 insertions(+), 6 deletions(-) create mode 100644 nhentai/utils.py diff --git a/nhentai/downloader.py b/nhentai/downloader.py index d15c26d..8916fb2 100644 --- a/nhentai/downloader.py +++ b/nhentai/downloader.py @@ -6,14 +6,10 @@ from urlparse import urlparse from logger import logger from parser import request +from nhentai.utils import Singleton -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 +class Downloader(Singleton): def __init__(self, path='', thread=1, timeout=30): if not isinstance(thread, (int, )) or thread < 1 or thread > 10: diff --git a/nhentai/utils.py b/nhentai/utils.py new file mode 100644 index 0000000..dfbc21d --- /dev/null +++ b/nhentai/utils.py @@ -0,0 +1,10 @@ +# coding: utf-8 +class _Singleton(type): + """ A metaclass that creates a Singleton base class when called. """ + _instances = {} + def __call__(cls, *args, **kwargs): + if cls not in cls._instances: + cls._instances[cls] = super(_Singleton, cls).__call__(*args, **kwargs) + return cls._instances[cls] + +class Singleton(_Singleton('SingletonMeta', (object,), {})): pass \ No newline at end of file From 9fe43dc219622a0bc50b8eb0302e3592d7c1fd50 Mon Sep 17 00:00:00 2001 From: Dhruvan Ganesh Date: Wed, 10 Aug 2016 15:43:03 +0530 Subject: [PATCH 2/4] project is now Py3 and Py2 compatible --- nhentai/cmdline.py | 5 ++--- nhentai/command.py | 11 ++++++----- nhentai/doujinshi.py | 8 +++++--- nhentai/downloader.py | 12 ++++++++---- nhentai/logger.py | 1 - nhentai/parser.py | 8 +++++--- requirements.txt | 1 + 7 files changed, 27 insertions(+), 19 deletions(-) diff --git a/nhentai/cmdline.py b/nhentai/cmdline.py index 826ea7d..64235a4 100644 --- a/nhentai/cmdline.py +++ b/nhentai/cmdline.py @@ -1,14 +1,13 @@ # coding: utf-8 from __future__ import print_function from optparse import OptionParser -from logger import logger try: from itertools import ifilter as filter except ImportError: pass - -import constant +import nhentai.constant as constant +from nhentai.logger import logger def banner(): diff --git a/nhentai/command.py b/nhentai/command.py index b7df177..b48fd65 100644 --- a/nhentai/command.py +++ b/nhentai/command.py @@ -1,11 +1,12 @@ #!/usr/bin/env python2.7 # coding: utf-8 import signal -from cmdline import cmd_parser, banner -from parser import doujinshi_parser, search_parser, print_doujinshi -from doujinshi import Doujinshi -from downloader import Downloader -from logger import logger + +from nhentai.cmdline import cmd_parser, banner +from nhentai.parser import doujinshi_parser, search_parser, print_doujinshi +from nhentai.doujinshi import Doujinshi +from nhentai.downloader import Downloader +from nhentai.logger import logger def main(): diff --git a/nhentai/doujinshi.py b/nhentai/doujinshi.py index 87157ad..4bcee71 100644 --- a/nhentai/doujinshi.py +++ b/nhentai/doujinshi.py @@ -1,8 +1,10 @@ # coding: utf-8 from __future__ import print_function from tabulate import tabulate -from constant import DETAIL_URL, IMAGE_URL -from logger import logger +from builtins import range + +from nhentai.constant import DETAIL_URL, IMAGE_URL +from nhentai.logger import logger class DoujinshiInfo(dict): @@ -47,7 +49,7 @@ class Doujinshi(object): logger.info('Start download doujinshi: %s' % self.name) if self.downloader: download_queue = [] - for i in xrange(1, self.pages + 1): + for i in range(1, self.pages + 1): download_queue.append('%s/%d/%d.%s' % (IMAGE_URL, int(self.img_id), i, self.ext)) self.downloader.download(download_queue, self.id) else: diff --git a/nhentai/downloader.py b/nhentai/downloader.py index 8916fb2..5eeab90 100644 --- a/nhentai/downloader.py +++ b/nhentai/downloader.py @@ -1,11 +1,15 @@ # coding: utf-8 +from builtins import str as text import os import requests import threadpool -from urlparse import urlparse -from logger import logger -from parser import request +try: + from urllib.parse import urlparse +except ImportError: + from urlparse import urlparse +from nhentai.logger import logger +from nhentai.parser import request from nhentai.utils import Singleton @@ -50,7 +54,7 @@ class Downloader(Singleton): logger.log(15, '{0} download successfully'.format(result)) def download(self, queue, folder=''): - if not isinstance(folder, (str, unicode)): + if not isinstance(folder, (text)): folder = str(folder) if self.path: diff --git a/nhentai/logger.py b/nhentai/logger.py index 157bf1f..88273ab 100644 --- a/nhentai/logger.py +++ b/nhentai/logger.py @@ -1,4 +1,3 @@ -import logging # # Copyright (C) 2010-2012 Vinay Sajip. All rights reserved. Licensed under the new BSD license. # diff --git a/nhentai/parser.py b/nhentai/parser.py index 25bf785..f897754 100644 --- a/nhentai/parser.py +++ b/nhentai/parser.py @@ -1,12 +1,14 @@ # coding: utf-8 from __future__ import 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): diff --git a/requirements.txt b/requirements.txt index b48abcf..df0a2ee 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,3 +2,4 @@ requests>=2.5.0 BeautifulSoup4>=4.0.0 threadpool>=1.2.7 tabulate>=0.7.5 +future>=0.15.2 \ No newline at end of file From 5d6a77346080da811c6a4dbf9f47388d4f587eca Mon Sep 17 00:00:00 2001 From: Dhruvan Ganesh Date: Wed, 10 Aug 2016 15:44:42 +0530 Subject: [PATCH 3/4] bumping major version due to dependency changes --- nhentai/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nhentai/__init__.py b/nhentai/__init__.py index e738d59..3c539e2 100644 --- a/nhentai/__init__.py +++ b/nhentai/__init__.py @@ -1,3 +1,3 @@ -__version__ = '0.1.5' +__version__ = '0.2.0' __author__ = 'Ricter' __email__ = 'ricterzheng@gmail.com' From 43e59b724a7bb92afe3e60001507127509327a21 Mon Sep 17 00:00:00 2001 From: Dhruvan Ganesh Date: Wed, 10 Aug 2016 20:05:15 +0530 Subject: [PATCH 4/4] Update .travis.yml --- .travis.yml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 5fc1c66..b6de096 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,10 +6,13 @@ language: python python: - 2.7 - 2.6 + - 3.3 + - 3.4 + - 3.5 install: - python setup.py install script: - nhentai --search umaru - - nhentai --ids=152503,146134 -t 10 --download --path=/tmp/ \ No newline at end of file + - nhentai --ids=152503,146134 -t 10 --download --path=/tmp/