8 from ..minicurses
import (
9 BreaklineStatusPrinter
,
12 QuietMultilinePrinter
,
16 LockingUnsupportedError
,
30 """File Downloader class.
32 File downloader objects are the ones responsible of downloading the
33 actual video file and writing it to disk.
35 File downloaders accept a lot of parameters. In order not to saturate
36 the object constructor with arguments, it receives a dictionary of
41 verbose: Print additional info to stdout.
42 quiet: Do not print messages to stdout.
43 ratelimit: Download speed limit, in bytes/sec.
44 throttledratelimit: Assume the download is being throttled below this speed (bytes/sec)
45 retries: Number of times to retry for HTTP error 5xx
46 file_access_retries: Number of times to retry on file access error
47 buffersize: Size of download buffer in bytes.
48 noresizebuffer: Do not automatically resize the download buffer.
49 continuedl: Try to continue downloads if possible.
50 noprogress: Do not print the progress bar.
51 nopart: Do not use temporary .part files.
52 updatetime: Use the Last-modified header to set output file timestamps.
53 test: Download only first bytes to test the downloader.
54 min_filesize: Skip files smaller than this size
55 max_filesize: Skip files larger than this size
56 xattr_set_filesize: Set ytdl.filesize user xattribute with expected size.
57 external_downloader_args: A dictionary of downloader keys (in lower case)
58 and a list of additional command-line arguments for the
59 executable. Use 'default' as the name for arguments to be
60 passed to all downloaders. For compatibility with youtube-dl,
61 a single list of args can also be used
62 hls_use_mpegts: Use the mpegts container for HLS videos.
63 http_chunk_size: Size of a chunk for chunk-based HTTP downloading. May be
64 useful for bypassing bandwidth throttling imposed by
65 a webserver (experimental)
66 progress_template: See YoutubeDL.py
68 Subclasses of this one must re-define the real_download method.
71 _TEST_FILE_SIZE
= 10241
74 def __init__(self
, ydl
, params
):
75 """Create a FileDownloader object with the given options."""
77 self
._progress
_hooks
= []
79 self
._prepare
_multiline
_status
()
80 self
.add_progress_hook(self
.report_progress
)
82 def _set_ydl(self
, ydl
):
86 'deprecation_warning',
88 'report_file_already_downloaded',
95 if not hasattr(self
, func
):
96 setattr(self
, func
, getattr(ydl
, func
))
98 def to_screen(self
, *args
, **kargs
):
99 self
.ydl
.to_screen(*args
, quiet
=self
.params
.get('quiet'), **kargs
)
102 def format_seconds(seconds
):
103 time
= timetuple_from_msec(seconds
* 1000)
107 return '%02d:%02d' % time
[1:-1]
108 return '%02d:%02d:%02d' % time
[:-1]
111 def calc_percent(byte_counter
, data_len
):
114 return float(byte_counter
) / float(data_len
) * 100.0
117 def format_percent(percent
):
122 return '%6s' % ('%3.1f%%' % percent
)
125 def calc_eta(start
, now
, total
, current
):
131 if current
== 0 or dif
< 0.001: # One millisecond
133 rate
= float(current
) / dif
134 return int((float(total
) - float(current
)) / rate
)
140 return FileDownloader
.format_seconds(eta
)
143 def calc_speed(start
, now
, bytes):
145 if bytes == 0 or dif
< 0.001: # One millisecond
147 return float(bytes) / dif
150 def format_speed(speed
):
152 return '%10s' % '---b/s'
153 return '%10s' % ('%s/s' % format_bytes(speed
))
156 def format_retries(retries
):
157 return 'inf' if retries
== float('inf') else '%.0f' % retries
160 def best_block_size(elapsed_time
, bytes):
161 new_min
= max(bytes / 2.0, 1.0)
162 new_max
= min(max(bytes * 2.0, 1.0), 4194304) # Do not surpass 4 MB
163 if elapsed_time
< 0.001:
165 rate
= bytes / elapsed_time
173 def parse_bytes(bytestr
):
174 """Parse a string indicating a byte quantity into an integer."""
175 matchobj
= re
.match(rf
'(?i)^({NUMBER_RE})([kMGTPEZY]?)$', bytestr
)
178 number
= float(matchobj
.group(1))
179 multiplier
= 1024.0 ** 'bkmgtpezy'.index(matchobj
.group(2).lower())
180 return int(round(number
* multiplier
))
182 def slow_down(self
, start_time
, now
, byte_counter
):
183 """Sleep if the download speed is over the rate limit."""
184 rate_limit
= self
.params
.get('ratelimit')
185 if rate_limit
is None or byte_counter
== 0:
189 elapsed
= now
- start_time
192 speed
= float(byte_counter
) / elapsed
193 if speed
> rate_limit
:
194 sleep_time
= float(byte_counter
) / rate_limit
- elapsed
196 time
.sleep(sleep_time
)
198 def temp_name(self
, filename
):
199 """Returns a temporary filename for the given filename."""
200 if self
.params
.get('nopart', False) or filename
== '-' or \
201 (os
.path
.exists(encodeFilename(filename
)) and not os
.path
.isfile(encodeFilename(filename
))):
203 return filename
+ '.part'
205 def undo_temp_name(self
, filename
):
206 if filename
.endswith('.part'):
207 return filename
[:-len('.part')]
210 def ytdl_filename(self
, filename
):
211 return filename
+ '.ytdl'
213 def wrap_file_access(action
, *, fatal
=False):
215 def inner(self
, *args
, **kwargs
):
216 file_access_retries
= self
.params
.get('file_access_retries', 0)
220 return func(self
, *args
, **kwargs
)
221 except OSError as err
:
223 if retry
> file_access_retries
or err
.errno
not in (errno
.EACCES
, errno
.EINVAL
):
225 self
.report_error(f
'unable to {action} file: {err}')
229 f
'[download] Unable to {action} file due to file access error. '
230 f
'Retrying (attempt {retry} of {self.format_retries(file_access_retries)}) ...')
235 @wrap_file_access('open', fatal
=True)
236 def sanitize_open(self
, filename
, open_mode
):
237 f
, filename
= sanitize_open(filename
, open_mode
)
238 if not getattr(f
, 'locked', None):
239 self
.write_debug(f
'{LockingUnsupportedError.msg}. Proceeding without locking', only_once
=True)
242 @wrap_file_access('remove')
243 def try_remove(self
, filename
):
246 @wrap_file_access('rename')
247 def try_rename(self
, old_filename
, new_filename
):
248 if old_filename
== new_filename
:
250 os
.replace(old_filename
, new_filename
)
252 def try_utime(self
, filename
, last_modified_hdr
):
253 """Try to set the last-modified time of the given file."""
254 if last_modified_hdr
is None:
256 if not os
.path
.isfile(encodeFilename(filename
)):
258 timestr
= last_modified_hdr
261 filetime
= timeconvert(timestr
)
264 # Ignore obviously invalid dates
267 with contextlib
.suppress(Exception):
268 os
.utime(filename
, (time
.time(), filetime
))
271 def report_destination(self
, filename
):
272 """Report destination filename."""
273 self
.to_screen('[download] Destination: ' + filename
)
275 def _prepare_multiline_status(self
, lines
=1):
276 if self
.params
.get('noprogress'):
277 self
._multiline
= QuietMultilinePrinter()
278 elif self
.ydl
.params
.get('logger'):
279 self
._multiline
= MultilineLogger(self
.ydl
.params
['logger'], lines
)
280 elif self
.params
.get('progress_with_newline'):
281 self
._multiline
= BreaklineStatusPrinter(self
.ydl
._out
_files
['screen'], lines
)
283 self
._multiline
= MultilinePrinter(self
.ydl
._out
_files
['screen'], lines
, not self
.params
.get('quiet'))
284 self
._multiline
.allow_colors
= self
._multiline
._HAVE
_FULLCAP
and not self
.params
.get('no_color')
286 def _finish_multiline_status(self
):
287 self
._multiline
.end()
289 ProgressStyles
= Namespace(
290 downloaded_bytes
='light blue',
291 percent
='light blue',
294 elapsed
='bold white',
296 total_bytes_estimate
='',
299 def _report_progress_status(self
, s
, default_template
):
300 for name
, style
in self
.ProgressStyles
._asdict
().items():
301 name
= f
'_{name}_str'
304 s
[name
] = self
._format
_progress
(s
[name
], style
)
305 s
['_default_template'] = default_template
% s
307 progress_dict
= s
.copy()
308 progress_dict
.pop('info_dict')
309 progress_dict
= {'info': s['info_dict'], 'progress': progress_dict}
311 progress_template
= self
.params
.get('progress_template', {})
312 self
._multiline
.print_at_line(self
.ydl
.evaluate_outtmpl(
313 progress_template
.get('download') or '[download] %(progress._default_template)s',
314 progress_dict
), s
.get('progress_idx') or 0)
315 self
.to_console_title(self
.ydl
.evaluate_outtmpl(
316 progress_template
.get('download-title') or 'yt-dlp %(progress._default_template)s',
319 def _format_progress(self
, *args
, **kwargs
):
320 return self
.ydl
._format
_text
(
321 self
._multiline
.stream
, self
._multiline
.allow_colors
, *args
, **kwargs
)
323 def report_progress(self
, s
):
324 if s
['status'] == 'finished':
325 if self
.params
.get('noprogress'):
326 self
.to_screen('[download] Download completed')
327 msg_template
= '100%%'
328 if s
.get('total_bytes') is not None:
329 s
['_total_bytes_str'] = format_bytes(s
['total_bytes'])
330 msg_template
+= ' of %(_total_bytes_str)s'
331 if s
.get('elapsed') is not None:
332 s
['_elapsed_str'] = self
.format_seconds(s
['elapsed'])
333 msg_template
+= ' in %(_elapsed_str)s'
334 s
['_percent_str'] = self
.format_percent(100)
335 self
._report
_progress
_status
(s
, msg_template
)
338 if s
['status'] != 'downloading':
341 if s
.get('eta') is not None:
342 s
['_eta_str'] = self
.format_eta(s
['eta'])
344 s
['_eta_str'] = 'Unknown'
346 if s
.get('total_bytes') and s
.get('downloaded_bytes') is not None:
347 s
['_percent_str'] = self
.format_percent(100 * s
['downloaded_bytes'] / s
['total_bytes'])
348 elif s
.get('total_bytes_estimate') and s
.get('downloaded_bytes') is not None:
349 s
['_percent_str'] = self
.format_percent(100 * s
['downloaded_bytes'] / s
['total_bytes_estimate'])
351 if s
.get('downloaded_bytes') == 0:
352 s
['_percent_str'] = self
.format_percent(0)
354 s
['_percent_str'] = 'Unknown %'
356 if s
.get('speed') is not None:
357 s
['_speed_str'] = self
.format_speed(s
['speed'])
359 s
['_speed_str'] = 'Unknown speed'
361 if s
.get('total_bytes') is not None:
362 s
['_total_bytes_str'] = format_bytes(s
['total_bytes'])
363 msg_template
= '%(_percent_str)s of %(_total_bytes_str)s at %(_speed_str)s ETA %(_eta_str)s'
364 elif s
.get('total_bytes_estimate') is not None:
365 s
['_total_bytes_estimate_str'] = format_bytes(s
['total_bytes_estimate'])
366 msg_template
= '%(_percent_str)s of ~%(_total_bytes_estimate_str)s at %(_speed_str)s ETA %(_eta_str)s'
368 if s
.get('downloaded_bytes') is not None:
369 s
['_downloaded_bytes_str'] = format_bytes(s
['downloaded_bytes'])
371 s
['_elapsed_str'] = self
.format_seconds(s
['elapsed'])
372 msg_template
= '%(_downloaded_bytes_str)s at %(_speed_str)s (%(_elapsed_str)s)'
374 msg_template
= '%(_downloaded_bytes_str)s at %(_speed_str)s'
376 msg_template
= '%(_percent_str)s at %(_speed_str)s ETA %(_eta_str)s'
377 if s
.get('fragment_index') and s
.get('fragment_count'):
378 msg_template
+= ' (frag %(fragment_index)s/%(fragment_count)s)'
379 elif s
.get('fragment_index'):
380 msg_template
+= ' (frag %(fragment_index)s)'
381 self
._report
_progress
_status
(s
, msg_template
)
383 def report_resuming_byte(self
, resume_len
):
384 """Report attempt to resume at given byte."""
385 self
.to_screen('[download] Resuming download at byte %s' % resume_len
)
387 def report_retry(self
, err
, count
, retries
):
388 """Report retry in case of HTTP error 5xx"""
390 '[download] Got server HTTP error: %s. Retrying (attempt %d of %s) ...'
391 % (error_to_compat_str(err
), count
, self
.format_retries(retries
)))
393 def report_unable_to_resume(self
):
394 """Report it was impossible to resume download."""
395 self
.to_screen('[download] Unable to resume')
398 def supports_manifest(manifest
):
399 """ Whether the downloader can download the fragments from the manifest.
400 Redefine in subclasses if needed. """
403 def download(self
, filename
, info_dict
, subtitle
=False):
404 """Download to a filename using the info from info_dict
405 Return True on success and False otherwise
408 nooverwrites_and_exists
= (
409 not self
.params
.get('overwrites', True)
410 and os
.path
.exists(encodeFilename(filename
))
413 if not hasattr(filename
, 'write'):
414 continuedl_and_exists
= (
415 self
.params
.get('continuedl', True)
416 and os
.path
.isfile(encodeFilename(filename
))
417 and not self
.params
.get('nopart', False)
420 # Check file already present
421 if filename
!= '-' and (nooverwrites_and_exists
or continuedl_and_exists
):
422 self
.report_file_already_downloaded(filename
)
423 self
._hook
_progress
({
424 'filename': filename
,
425 'status': 'finished',
426 'total_bytes': os
.path
.getsize(encodeFilename(filename
)),
428 self
._finish
_multiline
_status
()
432 sleep_interval
= self
.params
.get('sleep_interval_subtitles') or 0
434 min_sleep_interval
= self
.params
.get('sleep_interval') or 0
435 sleep_interval
= random
.uniform(
436 min_sleep_interval
, self
.params
.get('max_sleep_interval') or min_sleep_interval
)
437 if sleep_interval
> 0:
438 self
.to_screen(f
'[download] Sleeping {sleep_interval:.2f} seconds ...')
439 time
.sleep(sleep_interval
)
441 ret
= self
.real_download(filename
, info_dict
)
442 self
._finish
_multiline
_status
()
445 def real_download(self
, filename
, info_dict
):
446 """Real download process. Redefine in subclasses."""
447 raise NotImplementedError('This method must be implemented by subclasses')
449 def _hook_progress(self
, status
, info_dict
):
450 if not self
._progress
_hooks
:
452 status
['info_dict'] = info_dict
453 # youtube-dl passes the same status object to all the hooks.
454 # Some third party scripts seems to be relying on this.
455 # So keep this behavior if possible
456 for ph
in self
._progress
_hooks
:
459 def add_progress_hook(self
, ph
):
460 # See YoutubeDl.py (search for progress_hooks) for a description of
462 self
._progress
_hooks
.append(ph
)
464 def _debug_cmd(self
, args
, exe
=None):
465 if not self
.params
.get('verbose', False):
468 str_args
= [decodeArgument(a
) for a
in args
]
471 exe
= os
.path
.basename(str_args
[0])
473 self
.write_debug(f
'{exe} command line: {shell_quote(str_args)}')