mirror of
https://github.com/RicterZ/nhentai.git
synced 2025-04-19 10:21:19 +02:00
feature: proxy support
This commit is contained in:
parent
84fdda8d5e
commit
4a0f09b052
1
.gitignore
vendored
1
.gitignore
vendored
@ -2,4 +2,5 @@
|
||||
.idea/
|
||||
build
|
||||
dist/
|
||||
*.egg-info
|
||||
|
||||
|
@ -8,6 +8,9 @@ except ImportError:
|
||||
pass
|
||||
|
||||
|
||||
import constant
|
||||
|
||||
|
||||
def banner():
|
||||
print(''' _ _ _ _
|
||||
_ __ | | | | ___ _ __ | |_ __ _(_)
|
||||
@ -31,6 +34,8 @@ def cmd_parser():
|
||||
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:
|
||||
@ -55,4 +60,12 @@ def cmd_parser():
|
||||
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
|
||||
|
@ -48,6 +48,8 @@ def signal_handler(signal, frame):
|
||||
logger.error('Ctrl-C signal received. Quit.')
|
||||
raise SystemExit
|
||||
|
||||
|
||||
signal.signal(signal.SIGINT, signal_handler)
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
@ -3,3 +3,4 @@ URL = '%snhentai.net' % SCHEMA
|
||||
DETAIL_URL = '%s/g' % URL
|
||||
IMAGE_URL = '%si.nhentai.net/galleries' % SCHEMA
|
||||
SEARCH_URL = '%s/search/' % URL
|
||||
PROXY = {}
|
||||
|
@ -4,6 +4,7 @@ import requests
|
||||
import threadpool
|
||||
from urlparse import urlparse
|
||||
from logger import logger
|
||||
from parser import request
|
||||
|
||||
|
||||
class Downloader(object):
|
||||
@ -27,7 +28,7 @@ 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)
|
||||
|
@ -4,11 +4,18 @@ import sys
|
||||
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))
|
||||
@ -16,10 +23,10 @@ def doujinshi_parser(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()
|
||||
@ -54,7 +61,13 @@ def doujinshi_parser(id):
|
||||
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:
|
||||
|
Loading…
x
Reference in New Issue
Block a user