mirror of
https://github.com/RicterZ/nhentai.git
synced 2025-04-20 02:41: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/
|
.idea/
|
||||||
build
|
build
|
||||||
dist/
|
dist/
|
||||||
|
*.egg-info
|
||||||
|
|
||||||
|
@ -8,6 +8,9 @@ except ImportError:
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
import constant
|
||||||
|
|
||||||
|
|
||||||
def banner():
|
def banner():
|
||||||
print(''' _ _ _ _
|
print(''' _ _ _ _
|
||||||
_ __ | | | | ___ _ __ | |_ __ _(_)
|
_ __ | | | | ___ _ __ | |_ __ _(_)
|
||||||
@ -31,6 +34,8 @@ def cmd_parser():
|
|||||||
help='thread count of download doujinshi')
|
help='thread count of download doujinshi')
|
||||||
parser.add_option('--timeout', type='int', dest='timeout', action='store', default=30,
|
parser.add_option('--timeout', type='int', dest='timeout', action='store', default=30,
|
||||||
help='timeout of download doujinshi')
|
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()
|
args, _ = parser.parse_args()
|
||||||
|
|
||||||
if args.ids:
|
if args.ids:
|
||||||
@ -55,4 +60,12 @@ def cmd_parser():
|
|||||||
logger.critical('Maximum number of used threads is 10')
|
logger.critical('Maximum number of used threads is 10')
|
||||||
raise SystemExit
|
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
|
return args
|
||||||
|
@ -48,6 +48,8 @@ def signal_handler(signal, frame):
|
|||||||
logger.error('Ctrl-C signal received. Quit.')
|
logger.error('Ctrl-C signal received. Quit.')
|
||||||
raise SystemExit
|
raise SystemExit
|
||||||
|
|
||||||
|
|
||||||
signal.signal(signal.SIGINT, signal_handler)
|
signal.signal(signal.SIGINT, signal_handler)
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
@ -3,3 +3,4 @@ URL = '%snhentai.net' % SCHEMA
|
|||||||
DETAIL_URL = '%s/g' % URL
|
DETAIL_URL = '%s/g' % URL
|
||||||
IMAGE_URL = '%si.nhentai.net/galleries' % SCHEMA
|
IMAGE_URL = '%si.nhentai.net/galleries' % SCHEMA
|
||||||
SEARCH_URL = '%s/search/' % URL
|
SEARCH_URL = '%s/search/' % URL
|
||||||
|
PROXY = {}
|
||||||
|
@ -4,6 +4,7 @@ import requests
|
|||||||
import threadpool
|
import threadpool
|
||||||
from urlparse import urlparse
|
from urlparse import urlparse
|
||||||
from logger import logger
|
from logger import logger
|
||||||
|
from parser import request
|
||||||
|
|
||||||
|
|
||||||
class Downloader(object):
|
class Downloader(object):
|
||||||
@ -27,7 +28,7 @@ class Downloader(object):
|
|||||||
filename = filename if filename else os.path.basename(urlparse(url).path)
|
filename = filename if filename else os.path.basename(urlparse(url).path)
|
||||||
try:
|
try:
|
||||||
with open(os.path.join(folder, filename), "wb") as f:
|
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')
|
length = response.headers.get('content-length')
|
||||||
if length is None:
|
if length is None:
|
||||||
f.write(response.content)
|
f.write(response.content)
|
||||||
|
@ -4,11 +4,18 @@ import sys
|
|||||||
import re
|
import re
|
||||||
import requests
|
import requests
|
||||||
from bs4 import BeautifulSoup
|
from bs4 import BeautifulSoup
|
||||||
from constant import DETAIL_URL, SEARCH_URL
|
import constant
|
||||||
from logger import logger
|
from logger import logger
|
||||||
from tabulate import tabulate
|
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):
|
def doujinshi_parser(id):
|
||||||
if not isinstance(id, (int, )) and (isinstance(id, (str, )) and not id.isdigit()):
|
if not isinstance(id, (int, )) and (isinstance(id, (str, )) and not id.isdigit()):
|
||||||
raise Exception('Doujinshi id(%s) is not valid' % str(id))
|
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)
|
logger.debug('Fetching doujinshi information of id %d' % id)
|
||||||
doujinshi = dict()
|
doujinshi = dict()
|
||||||
doujinshi['id'] = id
|
doujinshi['id'] = id
|
||||||
url = '%s/%d/' % (DETAIL_URL, id)
|
url = '%s/%d/' % (constant.DETAIL_URL, id)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
response = requests.get(url).content
|
response = request('get', url).content
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.critical('%s%s' % tuple(e.message))
|
logger.critical('%s%s' % tuple(e.message))
|
||||||
sys.exit()
|
sys.exit()
|
||||||
@ -54,7 +61,13 @@ def doujinshi_parser(id):
|
|||||||
def search_parser(keyword, page):
|
def search_parser(keyword, page):
|
||||||
logger.debug('Searching doujinshis of keyword %s' % keyword)
|
logger.debug('Searching doujinshis of keyword %s' % keyword)
|
||||||
result = []
|
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)
|
html = BeautifulSoup(response)
|
||||||
doujinshi_search_result = html.find_all('div', attrs={'class': 'gallery'})
|
doujinshi_search_result = html.find_all('div', attrs={'class': 'gallery'})
|
||||||
for doujinshi in doujinshi_search_result:
|
for doujinshi in doujinshi_search_result:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user