Merge pull request #5 from navrudh/master

Python3 compatablility and new Singleton implementation
This commit is contained in:
Ricter Zheng 2016-08-11 21:07:02 +08:00 committed by GitHub
commit 8442f00c6c
10 changed files with 45 additions and 28 deletions

View File

@ -6,6 +6,9 @@ language: python
python: python:
- 2.7 - 2.7
- 2.6 - 2.6
- 3.3
- 3.4
- 3.5
install: install:
- python setup.py install - python setup.py install

View File

@ -1,3 +1,3 @@
__version__ = '0.1.5' __version__ = '0.2.0'
__author__ = 'Ricter' __author__ = 'Ricter'
__email__ = 'ricterzheng@gmail.com' __email__ = 'ricterzheng@gmail.com'

View File

@ -1,14 +1,13 @@
# coding: utf-8 # coding: utf-8
from __future__ import print_function from __future__ import print_function
from optparse import OptionParser from optparse import OptionParser
from logger import logger
try: try:
from itertools import ifilter as filter from itertools import ifilter as filter
except ImportError: except ImportError:
pass pass
import nhentai.constant as constant
import constant from nhentai.logger import logger
def banner(): def banner():

View File

@ -1,11 +1,12 @@
#!/usr/bin/env python2.7 #!/usr/bin/env python2.7
# coding: utf-8 # coding: utf-8
import signal import signal
from cmdline import cmd_parser, banner
from parser import doujinshi_parser, search_parser, print_doujinshi from nhentai.cmdline import cmd_parser, banner
from doujinshi import Doujinshi from nhentai.parser import doujinshi_parser, search_parser, print_doujinshi
from downloader import Downloader from nhentai.doujinshi import Doujinshi
from logger import logger from nhentai.downloader import Downloader
from nhentai.logger import logger
def main(): def main():

View File

@ -1,8 +1,10 @@
# coding: utf-8 # coding: utf-8
from __future__ import print_function from __future__ import print_function
from tabulate import tabulate from tabulate import tabulate
from constant import DETAIL_URL, IMAGE_URL from builtins import range
from logger import logger
from nhentai.constant import DETAIL_URL, IMAGE_URL
from nhentai.logger import logger
class DoujinshiInfo(dict): class DoujinshiInfo(dict):
@ -47,7 +49,7 @@ class Doujinshi(object):
logger.info('Start download doujinshi: %s' % self.name) logger.info('Start download doujinshi: %s' % self.name)
if self.downloader: if self.downloader:
download_queue = [] 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)) download_queue.append('%s/%d/%d.%s' % (IMAGE_URL, int(self.img_id), i, self.ext))
self.downloader.download(download_queue, self.id) self.downloader.download(download_queue, self.id)
else: else:

View File

@ -1,19 +1,19 @@
# coding: utf-8 # coding: utf-8
from builtins import str as text
import os import os
import requests import requests
import threadpool import threadpool
from urlparse import urlparse try:
from logger import logger from urllib.parse import urlparse
from parser import request except ImportError:
from urlparse import urlparse
from nhentai.logger import logger
from nhentai.parser import request
from nhentai.utils import Singleton
class Downloader(object): class Downloader(Singleton):
_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, timeout=30): def __init__(self, path='', thread=1, timeout=30):
if not isinstance(thread, (int, )) or thread < 1 or thread > 10: if not isinstance(thread, (int, )) or thread < 1 or thread > 10:
@ -54,7 +54,7 @@ class Downloader(object):
logger.log(15, '{0} download successfully'.format(result)) logger.log(15, '{0} download successfully'.format(result))
def download(self, queue, folder=''): def download(self, queue, folder=''):
if not isinstance(folder, (str, unicode)): if not isinstance(folder, (text)):
folder = str(folder) folder = str(folder)
if self.path: if self.path:

View File

@ -1,4 +1,3 @@
import logging
# #
# Copyright (C) 2010-2012 Vinay Sajip. All rights reserved. Licensed under the new BSD license. # Copyright (C) 2010-2012 Vinay Sajip. All rights reserved. Licensed under the new BSD license.
# #

View File

@ -1,12 +1,14 @@
# coding: utf-8 # coding: utf-8
from __future__ import print_function from __future__ import print_function
from bs4 import BeautifulSoup
import re import re
import requests import requests
from bs4 import BeautifulSoup
import constant
from logger import logger
from tabulate import tabulate from tabulate import tabulate
import nhentai.constant as constant
from nhentai.logger import logger
def request(method, url, **kwargs): def request(method, url, **kwargs):
if not hasattr(requests, method): if not hasattr(requests, method):

10
nhentai/utils.py Normal file
View File

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

View File

@ -2,3 +2,4 @@ requests>=2.5.0
BeautifulSoup4>=4.0.0 BeautifulSoup4>=4.0.0
threadpool>=1.2.7 threadpool>=1.2.7
tabulate>=0.7.5 tabulate>=0.7.5
future>=0.15.2