pass
+import constant
+
+
def banner():
print(''' _ _ _ _
_ __ | | | | ___ _ __ | |_ __ _(_)
help='thread count of download doujinshi')
parser.add_option('--timeout', type='int', dest='timeout', action='store', default=30,
help='timeout of download doujinshi')
+ parser.add_option('--proxy', type='string', dest='proxy', action='store', default='',
+ help='use proxy, example: socks5://127.0.0.1:1080')
args, _ = parser.parse_args()
if args.ids:
logger.critical('Maximum number of used threads is 10')
raise SystemExit
+ if args.proxy:
+ import urlparse
+ proxy_url = urlparse.urlparse(args.proxy)
+ if proxy_url.scheme not in ('http', 'https'):
+ logger.error('Invalid protocol \'{}\' of proxy, ignored'.format(proxy_url.scheme))
+ else:
+ constant.PROXY = {proxy_url.scheme: args.proxy}
+
return args
import threadpool
from urlparse import urlparse
from logger import logger
+from parser import request
class Downloader(object):
filename = filename if filename else os.path.basename(urlparse(url).path)
try:
with open(os.path.join(folder, filename), "wb") as f:
- response = requests.get(url, stream=True, timeout=self.timeout)
+ response = request('get', url, stream=True, timeout=self.timeout)
length = response.headers.get('content-length')
if length is None:
f.write(response.content)
import re
import requests
from bs4 import BeautifulSoup
-from constant import DETAIL_URL, SEARCH_URL
+import constant
from logger import logger
from tabulate import tabulate
+def request(method, url, **kwargs):
+ if not hasattr(requests, method):
+ raise AttributeError('\'requests\' object has no attribute \'{}\''.format(method))
+
+ 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))
logger.debug('Fetching doujinshi information of id %d' % id)
doujinshi = dict()
doujinshi['id'] = id
- url = '%s/%d/' % (DETAIL_URL, id)
+ url = '%s/%d/' % (constant.DETAIL_URL, id)
try:
- response = requests.get(url).content
+ response = request('get', url).content
except Exception as e:
logger.critical('%s%s' % tuple(e.message))
sys.exit()
def search_parser(keyword, page):
logger.debug('Searching doujinshis of keyword %s' % keyword)
result = []
- response = requests.get(SEARCH_URL, params={'q': keyword, 'page': page}).content
+ try:
+ response = request('get', url=constant.SEARCH_URL, params={'q': keyword, 'page': page}).content
+ except requests.ConnectionError as e:
+ logger.critical(e)
+ logger.warn('If you are in China, please configure the proxy to fu*k GFW.')
+ raise SystemExit
+
html = BeautifulSoup(response)
doujinshi_search_result = html.find_all('div', attrs={'class': 'gallery'})
for doujinshi in doujinshi_search_result: