Skip to content

Commit 3c4dfad

Browse files
committed
[tumblr.py] support proxy, #125
* using proxy: -x 'protocol://address:port' protocol can be http, https, socks5
1 parent 6fdc7ef commit 3c4dfad

File tree

1 file changed

+31
-8
lines changed

1 file changed

+31
-8
lines changed

tumblr.py

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@
6262
ss = requests.session()
6363
ss.headers.update(headers)
6464

65+
PROXY = None
66+
6567
class Error(Exception):
6668
def __init__(self, msg):
6769
self.msg = msg
@@ -119,12 +121,19 @@ def download_run(item):
119121
# num = random.randint(0, 7) % 8
120122
# col = s % (1, num + 90, filepath)
121123
# 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+
])
128137
status = os.system(cmd)
129138
return status, filepath
130139

@@ -165,16 +174,20 @@ def _request(self, base_hostname, target, type, params):
165174
api_url = '/'.join(['https://api.tumblr.com/v2/blog',
166175
base_hostname, target, type])
167176
params['api_key'] = API_KEY
177+
if PROXY:
178+
proxies = {'http': PROXY, 'https': PROXY}
179+
else:
180+
proxies = None
168181
while True:
169182
try:
170-
res = ss.get(api_url, params=params, timeout=10)
183+
res = ss.get(api_url, params=params, proxies=proxies, timeout=10)
171184
json_data = res.json()
172185
break
173186
except KeyboardInterrupt:
174187
sys.exit()
175188
except Exception as e:
176189
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'
178191
time.sleep(5)
179192
if json_data['meta']['msg'].lower() != 'ok':
180193
raise Error(s % (1, 91, json_data['meta']['msg']))
@@ -506,9 +519,19 @@ def args_handler(argv):
506519
help='update new things')
507520
p.add_argument('--redownload', action='store_true',
508521
help='redownload all things')
522+
p.add_argument('-x', '--proxy', type=str,
523+
help='redownload all things')
509524
args = p.parse_args(argv[1:])
510525
xxx = args.xxx
511526

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+
512535
if args.redownload: args.update = True
513536
return args, xxx
514537

0 commit comments

Comments
 (0)