forked from iambus/xunlei-lixian
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lixian_download_tools.py
77 lines (64 loc) · 2.82 KB
/
lixian_download_tools.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
from lixian_config import *
import subprocess
import urllib2
import os.path
def check_bin(bin):
import distutils.spawn
assert distutils.spawn.find_executable(bin), "Can't find %s" % bin
def urllib2_download(client, download_url, filename, resuming=False):
'''In the case you don't even have wget...'''
assert not resuming
print 'Downloading', download_url, 'to', filename, '...'
request = urllib2.Request(download_url, headers={'Cookie': 'gdriveid='+client.get_gdriveid()})
response = urllib2.urlopen(request)
import shutil
with open(filename, 'wb') as output:
shutil.copyfileobj(response, output)
def asyn_download(client, download_url, filename, resuming=False):
import lixian_download_asyn
lixian_download_asyn.download(download_url, filename, headers={'Cookie': 'gdriveid='+str(client.get_gdriveid())}, resuming=resuming)
def wget_download(client, download_url, filename, resuming=False):
gdriveid = str(client.get_gdriveid())
wget_opts = ['wget', '--header=Cookie: gdriveid='+gdriveid, download_url, '-O', filename]
if resuming:
wget_opts.append('-c')
wget_opts.extend(get_config('wget-opts', '').split())
check_bin(wget_opts[0])
exit_code = subprocess.call(wget_opts)
if exit_code != 0:
raise Exception('wget exited abnormaly')
def curl_download(client, download_url, filename, resuming=False):
gdriveid = str(client.get_gdriveid())
curl_opts = ['curl', '-L', download_url, '--cookie', 'gdriveid='+gdriveid, '--output', filename]
if resuming:
curl_opts += ['--continue-at', '-']
curl_opts.extend(get_config('curl-opts', '').split())
check_bin(curl_opts[0])
exit_code = subprocess.call(curl_opts)
if exit_code != 0:
raise Exception('curl exited abnormaly')
def aria2_download(client, download_url, path, resuming=False):
gdriveid = str(client.get_gdriveid())
dir = os.path.dirname(path)
filename = os.path.basename(path)
aria2_opts = ['aria2c', '--header=Cookie: gdriveid='+gdriveid, download_url, '--out', filename, '--file-allocation=none']
if dir:
aria2_opts.extend(('--dir', dir))
if resuming:
aria2_opts.append('-c')
aria2_opts.extend(get_config('aria2-opts', '').split())
check_bin(aria2_opts[0])
exit_code = subprocess.call(aria2_opts)
if exit_code != 0:
raise Exception('aria2c exited abnormaly')
def axel_download(client, download_url, path, resuming=False):
gdriveid = str(client.get_gdriveid())
axel_opts = ['axel', '--header=Cookie: gdriveid='+gdriveid, download_url, '--output', path]
axel_opts.extend(get_config('axel-opts', '').split())
check_bin(axel_opts[0])
exit_code = subprocess.call(axel_opts)
if exit_code != 0:
raise Exception('axel exited abnormaly')
# TODO: support axel, ProZilla
def get_tool(name):
return {'wget':wget_download, 'curl': curl_download, 'aria2':aria2_download, 'aria2c':aria2_download, 'axel':axel_download, 'asyn':asyn_download, 'urllib2':urllib2_download}[name]