|
62 | 62 | ss = requests.session()
|
63 | 63 | ss.headers.update(headers)
|
64 | 64 |
|
| 65 | +PROXY = None |
| 66 | + |
65 | 67 | class Error(Exception):
|
66 | 68 | def __init__(self, msg):
|
67 | 69 | self.msg = msg
|
@@ -119,12 +121,19 @@ def download_run(item):
|
119 | 121 | # num = random.randint(0, 7) % 8
|
120 | 122 | # col = s % (1, num + 90, filepath)
|
121 | 123 | # print ' ++ download: %s' % col
|
122 |
| - cmd = ' '.join([ |
123 |
| - 'wget', '-c', '-q', '-T', '10', |
124 |
| - '-O', '"%s.tmp"' % filepath, |
125 |
| - '--user-agent', '"%s"' % headers['User-Agent'], |
126 |
| - '"%s"' % item['durl'].replace('http:', 'https:') |
127 |
| - ]) |
| 124 | + |
| 125 | + if PROXY: |
| 126 | + cmd = ' '.join([ |
| 127 | + 'curl', '-s', '-x', '"%s"' % PROXY, '-o', '"%s.tmp"' % filepath, |
| 128 | + '-H', '"User-Agent: %s"' % headers['User-Agent'], |
| 129 | + '"%s"' % item['durl'] |
| 130 | + ]) |
| 131 | + else: |
| 132 | + cmd = ' '.join([ |
| 133 | + 'curl', '-s', '-o', '"%s.tmp"' % filepath, |
| 134 | + '-H', '"User-Agent: %s"' % headers['User-Agent'], |
| 135 | + '"%s"' % item['durl'] |
| 136 | + ]) |
128 | 137 | status = os.system(cmd)
|
129 | 138 | return status, filepath
|
130 | 139 |
|
@@ -165,16 +174,20 @@ def _request(self, base_hostname, target, type, params):
|
165 | 174 | api_url = '/'.join(['https://api.tumblr.com/v2/blog',
|
166 | 175 | base_hostname, target, type])
|
167 | 176 | params['api_key'] = API_KEY
|
| 177 | + if PROXY: |
| 178 | + proxies = {'http': PROXY, 'https': PROXY} |
| 179 | + else: |
| 180 | + proxies = None |
168 | 181 | while True:
|
169 | 182 | try:
|
170 |
| - res = ss.get(api_url, params=params, timeout=10) |
| 183 | + res = ss.get(api_url, params=params, proxies=proxies, timeout=10) |
171 | 184 | json_data = res.json()
|
172 | 185 | break
|
173 | 186 | except KeyboardInterrupt:
|
174 | 187 | sys.exit()
|
175 | 188 | except Exception as e:
|
176 | 189 | NET_ERRORS.value += 1 # count errors
|
177 |
| - # print s % (1, 93, '[Error at requests]:'), e |
| 190 | + print s % (1, 93, '[Error at requests]:'), e, '\n' |
178 | 191 | time.sleep(5)
|
179 | 192 | if json_data['meta']['msg'].lower() != 'ok':
|
180 | 193 | raise Error(s % (1, 91, json_data['meta']['msg']))
|
@@ -506,9 +519,19 @@ def args_handler(argv):
|
506 | 519 | help='update new things')
|
507 | 520 | p.add_argument('--redownload', action='store_true',
|
508 | 521 | help='redownload all things')
|
| 522 | + p.add_argument('-x', '--proxy', type=str, |
| 523 | + help='redownload all things') |
509 | 524 | args = p.parse_args(argv[1:])
|
510 | 525 | xxx = args.xxx
|
511 | 526 |
|
| 527 | + if args.proxy: |
| 528 | + if args.proxy[:4] not in ('http', 'sock'): |
| 529 | + print s % (1, 91, '[Error]:'), 'proxy must have a protocol:// prefix' |
| 530 | + sys.exit(1) |
| 531 | + else: |
| 532 | + global PROXY |
| 533 | + PROXY = args.proxy |
| 534 | + |
512 | 535 | if args.redownload: args.update = True
|
513 | 536 | return args, xxx
|
514 | 537 |
|
|
0 commit comments