]> git.lizzy.rs Git - nhentai.git/commitdiff
feature: proxy support
authorRicter Z <ricterzheng@gmail.com>
Thu, 4 Feb 2016 02:25:35 +0000 (10:25 +0800)
committerRicter Z <ricterzheng@gmail.com>
Thu, 4 Feb 2016 02:25:35 +0000 (10:25 +0800)
.gitignore
nhentai/cmdline.py
nhentai/command.py
nhentai/constant.py
nhentai/downloader.py
nhentai/parser.py

index a56afbc64d77a6014e94b494830e94d8893e9e4b..d8339d7f06e227946ada8864671f53a44d7f6754 100644 (file)
@@ -2,4 +2,5 @@
 .idea/
 build
 dist/
+*.egg-info
 
index 891d1ab59a0b3944c316b810e73672b71d679a9d..0ab883049f549e3c5206a481e213b497c0c0b162 100644 (file)
@@ -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
index 647952ccd213d9cf3ad98f7ccb25935b2ff6f723..e22deffc4421aaf9b74585be286d01b49b3b5945 100644 (file)
@@ -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()
index 5196bd7f6cf3e51c1071d4af0b205a9fa2aa8c07..54393841404cfda35af6ea70ccbc1e3606888d7b 100644 (file)
@@ -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 = {}
index 09c1a5c99cf1c11490934b4c5f3aa716e062b7b3..402a7a84eaa0a9225146e6918fd8ba8fcaf59864 100644 (file)
@@ -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)
index 7585a2950c88198a818dcd0524fb670b43915cf8..01786e934d2ddfd58c7d623152bcfe38c3f8ae30 100644 (file)
@@ -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: