-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfastdown.py
More file actions
221 lines (179 loc) · 7.35 KB
/
fastdown.py
File metadata and controls
221 lines (179 loc) · 7.35 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import ftplib
import re
import os
from concurrent.futures import ThreadPoolExecutor, Future
import requests
class AtomTask:
def __init__(self, url, filename, chunk_size=None, offset=None):
self.url = url
self.filename = filename
self.chunk_size = chunk_size
self.offset = offset
self.tries = 0
def __str__(self):
return self.filename
class BaseDownloader:
def __init__(self, chunk_parallel=1, chunk_size=8192, retry=5, timeout=3):
self.chunk_parallel = chunk_parallel
self.chunk_size = chunk_size
self.retry = retry
self.timeout = timeout
self.executor = None
self.basedir = ''
self.process_hook = None
def set_executor(self, executor):
self.executor = executor
def set_basedir(self, basedir):
self.basedir = basedir
def set_process_hook(self, func):
self.process_hook = func
def __call__(self, task):
pass
def failed(self, task):
print('Failed: {}'.format(task))
task.tries += 1
if task.tries < self.retry:
return self.executor.submit(self, task)
else:
raise IOError('Maximum tries exceed! Task: {}'.format(task))
class RequestsDownloader(BaseDownloader):
def __call__(self, task):
if task.chunk_size is None:
return self.single(task)
else:
raise NotImplementedError('Chunk download feature hasn\'t been implemented, please be patient!')
def get_filename_from_header(self, header):
disposition = header.get('content-disposition')
if not disposition:
raise ValueError('Cannot fetch filename!')
filenames = re.findall('filename=(.+)', disposition)
if len(filenames) == 0:
raise ValueError('Cannot fetch filename!')
return filenames[0]
def single(self, task):
try:
res = requests.get(task.url, allow_redirects=True, timeout=self.timeout)
except (requests.exceptions.Timeout, requests.exceptions.RequestException):
return self.failed(task)
if task.filename is None:
task.filename = self.get_filename_from_header(res.headers)
if self.process_hook:
self.process_hook(res.content)
else:
open(os.path.join(self.basedir, task.filename), 'wb').write(res.content)
print('Succeed: {}'.format(task))
class FastDown:
default_service = RequestsDownloader
def __init__(self, file_parallel=1, chunk_parallel=1, chunk_size=None, retry=5, timeout=3):
self.file_parallel = file_parallel
self.chunk_parallel = chunk_parallel
self.chunk_size = chunk_size
if self.chunk_size is None and self.chunk_parallel > 1:
self.chunk_size = 8192
self.retry = retry
self.timeout = timeout
self.service = self.default_service
self.basedir = ''
def use_downloader(self, service):
self.service = service
def set_basedir(self, basedir):
self.basedir = basedir
def set_task(self, task):
"""Task should be arranged like list of (url, filename) tuples."""
self.targets = []
if isinstance(task, tuple):
self.targets.extend(self.create_atom_task(*task))
elif isinstance(task, str):
self.targets.extend(self.create_atom_task(task, None))
else:
try:
for t in task:
if isinstance(t, tuple):
url, filename = t
else:
url, filename = t, None
self.targets.extend(self.create_atom_task(url, filename))
except TypeError:
raise ValueError('task must be length-2 tuple or iterable of length-2 tuples.')
def create_atom_task(self, url, filename):
if self.chunk_parallel == 1:
return [AtomTask(url, filename)]
return [AtomTask(url, filename, self.chunk_size, self.chunk_size * i)
for i in range(self.chunk_parallel)]
def download(self):
downloader = self.service(self.chunk_parallel, self.chunk_size, self.retry, self.timeout)
downloader.set_basedir(self.basedir)
with ThreadPoolExecutor(max_workers=self.file_parallel) as executor:
downloader.set_executor(executor)
futures = executor.map(downloader, self.targets)
self.targets = []
for future in futures:
while isinstance(future, Future):
future = future.result()
class FTPDownloader(BaseDownloader):
def __call__(self, task):
if task.chunk_size is None:
return self.single(task)
else:
raise NotImplementedError('Chunk download feature hasn\'t been implemented, please be patient!')
def set_ftp_handler(self, ftp):
self.ftp = ftp
def single(self, task):
try:
self.ftp.retrbinary(
'RETR {}'.format(task.url),
open(os.path.join(self.basedir, task.filename), 'wb').write
)
except Exception as err:
print('Download error', err)
class FTPFastDown:
default_service = FTPDownloader
def __init__(self, file_parallel=1, chunk_parallel=1, chunk_size=None, retry=5, timeout=3):
self.file_parallel = file_parallel
self.chunk_parallel = chunk_parallel
self.chunk_size = chunk_size
if self.chunk_size is None and self.chunk_parallel > 1:
self.chunk_size = 8192
self.retry = retry
self.timeout = timeout
self.service = self.default_service
self.basedir = ''
def use_downloader(self, service):
self.service = service
def set_ftp(self, ftp):
self.ftp = ftp
def set_basedir(self, basedir):
self.basedir = basedir
def set_task(self, task):
"""Task should be arranged like list of (url, filename) tuples."""
self.targets = []
if isinstance(task, tuple):
self.targets.extend(self.create_atom_task(*task))
elif isinstance(task, str):
self.targets.extend(self.create_atom_task(task, None))
else:
try:
for t in task:
if isinstance(t, tuple):
url, filename = t
else:
url, filename = t, None
self.targets.extend(self.create_atom_task(url, filename))
except TypeError:
raise ValueError('task must be length-2 tuple or iterable of length-2 tuples.')
def create_atom_task(self, url, filename):
if self.chunk_parallel == 1:
return [AtomTask(url, filename)]
return [AtomTask(url, filename, self.chunk_size, self.chunk_size * i)
for i in range(self.chunk_parallel)]
def download(self):
downloader = self.service(self.chunk_parallel, self.chunk_size, self.retry, self.timeout)
downloader.set_basedir(self.basedir)
downloader.set_ftp_handler(self.ftp)
with ThreadPoolExecutor(max_workers=self.file_parallel) as executor:
downloader.set_executor(executor)
futures = executor.map(downloader, self.targets)
self.targets = []
for future in futures:
while isinstance(future, Future):
future = future.result()