[downloader/{common,fragment}] Fix total retries reporting on python 2.6
[youtube-dl] / youtube_dl / downloader / common.py
1 from __future__ import division, unicode_literals
2
3 import os
4 import re
5 import sys
6 import time
7
8 from ..compat import compat_os_name
9 from ..utils import (
10     encodeFilename,
11     error_to_compat_str,
12     decodeArgument,
13     format_bytes,
14     timeconvert,
15 )
16
17
18 class FileDownloader(object):
19     """File Downloader class.
20
21     File downloader objects are the ones responsible of downloading the
22     actual video file and writing it to disk.
23
24     File downloaders accept a lot of parameters. In order not to saturate
25     the object constructor with arguments, it receives a dictionary of
26     options instead.
27
28     Available options:
29
30     verbose:            Print additional info to stdout.
31     quiet:              Do not print messages to stdout.
32     ratelimit:          Download speed limit, in bytes/sec.
33     retries:            Number of times to retry for HTTP error 5xx
34     buffersize:         Size of download buffer in bytes.
35     noresizebuffer:     Do not automatically resize the download buffer.
36     continuedl:         Try to continue downloads if possible.
37     noprogress:         Do not print the progress bar.
38     logtostderr:        Log messages to stderr instead of stdout.
39     consoletitle:       Display progress in console window's titlebar.
40     nopart:             Do not use temporary .part files.
41     updatetime:         Use the Last-modified header to set output file timestamps.
42     test:               Download only first bytes to test the downloader.
43     min_filesize:       Skip files smaller than this size
44     max_filesize:       Skip files larger than this size
45     xattr_set_filesize: Set ytdl.filesize user xattribute with expected size.
46                         (experimental)
47     external_downloader_args:  A list of additional command-line arguments for the
48                         external downloader.
49     hls_use_mpegts:     Use the mpegts container for HLS videos.
50
51     Subclasses of this one must re-define the real_download method.
52     """
53
54     _TEST_FILE_SIZE = 10241
55     params = None
56
57     def __init__(self, ydl, params):
58         """Create a FileDownloader object with the given options."""
59         self.ydl = ydl
60         self._progress_hooks = []
61         self.params = params
62         self.add_progress_hook(self.report_progress)
63
64     @staticmethod
65     def format_seconds(seconds):
66         (mins, secs) = divmod(seconds, 60)
67         (hours, mins) = divmod(mins, 60)
68         if hours > 99:
69             return '--:--:--'
70         if hours == 0:
71             return '%02d:%02d' % (mins, secs)
72         else:
73             return '%02d:%02d:%02d' % (hours, mins, secs)
74
75     @staticmethod
76     def calc_percent(byte_counter, data_len):
77         if data_len is None:
78             return None
79         return float(byte_counter) / float(data_len) * 100.0
80
81     @staticmethod
82     def format_percent(percent):
83         if percent is None:
84             return '---.-%'
85         return '%6s' % ('%3.1f%%' % percent)
86
87     @staticmethod
88     def calc_eta(start, now, total, current):
89         if total is None:
90             return None
91         if now is None:
92             now = time.time()
93         dif = now - start
94         if current == 0 or dif < 0.001:  # One millisecond
95             return None
96         rate = float(current) / dif
97         return int((float(total) - float(current)) / rate)
98
99     @staticmethod
100     def format_eta(eta):
101         if eta is None:
102             return '--:--'
103         return FileDownloader.format_seconds(eta)
104
105     @staticmethod
106     def calc_speed(start, now, bytes):
107         dif = now - start
108         if bytes == 0 or dif < 0.001:  # One millisecond
109             return None
110         return float(bytes) / dif
111
112     @staticmethod
113     def format_speed(speed):
114         if speed is None:
115             return '%10s' % '---b/s'
116         return '%10s' % ('%s/s' % format_bytes(speed))
117
118     @staticmethod
119     def format_retries(retries):
120         return 'inf' if retries == float('inf') else '%.0f' % retries
121
122     @staticmethod
123     def best_block_size(elapsed_time, bytes):
124         new_min = max(bytes / 2.0, 1.0)
125         new_max = min(max(bytes * 2.0, 1.0), 4194304)  # Do not surpass 4 MB
126         if elapsed_time < 0.001:
127             return int(new_max)
128         rate = bytes / elapsed_time
129         if rate > new_max:
130             return int(new_max)
131         if rate < new_min:
132             return int(new_min)
133         return int(rate)
134
135     @staticmethod
136     def parse_bytes(bytestr):
137         """Parse a string indicating a byte quantity into an integer."""
138         matchobj = re.match(r'(?i)^(\d+(?:\.\d+)?)([kMGTPEZY]?)$', bytestr)
139         if matchobj is None:
140             return None
141         number = float(matchobj.group(1))
142         multiplier = 1024.0 ** 'bkmgtpezy'.index(matchobj.group(2).lower())
143         return int(round(number * multiplier))
144
145     def to_screen(self, *args, **kargs):
146         self.ydl.to_screen(*args, **kargs)
147
148     def to_stderr(self, message):
149         self.ydl.to_screen(message)
150
151     def to_console_title(self, message):
152         self.ydl.to_console_title(message)
153
154     def trouble(self, *args, **kargs):
155         self.ydl.trouble(*args, **kargs)
156
157     def report_warning(self, *args, **kargs):
158         self.ydl.report_warning(*args, **kargs)
159
160     def report_error(self, *args, **kargs):
161         self.ydl.report_error(*args, **kargs)
162
163     def slow_down(self, start_time, now, byte_counter):
164         """Sleep if the download speed is over the rate limit."""
165         rate_limit = self.params.get('ratelimit')
166         if rate_limit is None or byte_counter == 0:
167             return
168         if now is None:
169             now = time.time()
170         elapsed = now - start_time
171         if elapsed <= 0.0:
172             return
173         speed = float(byte_counter) / elapsed
174         if speed > rate_limit:
175             time.sleep(max((byte_counter // rate_limit) - elapsed, 0))
176
177     def temp_name(self, filename):
178         """Returns a temporary filename for the given filename."""
179         if self.params.get('nopart', False) or filename == '-' or \
180                 (os.path.exists(encodeFilename(filename)) and not os.path.isfile(encodeFilename(filename))):
181             return filename
182         return filename + '.part'
183
184     def undo_temp_name(self, filename):
185         if filename.endswith('.part'):
186             return filename[:-len('.part')]
187         return filename
188
189     def try_rename(self, old_filename, new_filename):
190         try:
191             if old_filename == new_filename:
192                 return
193             os.rename(encodeFilename(old_filename), encodeFilename(new_filename))
194         except (IOError, OSError) as err:
195             self.report_error('unable to rename file: %s' % error_to_compat_str(err))
196
197     def try_utime(self, filename, last_modified_hdr):
198         """Try to set the last-modified time of the given file."""
199         if last_modified_hdr is None:
200             return
201         if not os.path.isfile(encodeFilename(filename)):
202             return
203         timestr = last_modified_hdr
204         if timestr is None:
205             return
206         filetime = timeconvert(timestr)
207         if filetime is None:
208             return filetime
209         # Ignore obviously invalid dates
210         if filetime == 0:
211             return
212         try:
213             os.utime(filename, (time.time(), filetime))
214         except Exception:
215             pass
216         return filetime
217
218     def report_destination(self, filename):
219         """Report destination filename."""
220         self.to_screen('[download] Destination: ' + filename)
221
222     def _report_progress_status(self, msg, is_last_line=False):
223         fullmsg = '[download] ' + msg
224         if self.params.get('progress_with_newline', False):
225             self.to_screen(fullmsg)
226         else:
227             if compat_os_name == 'nt':
228                 prev_len = getattr(self, '_report_progress_prev_line_length',
229                                    0)
230                 if prev_len > len(fullmsg):
231                     fullmsg += ' ' * (prev_len - len(fullmsg))
232                 self._report_progress_prev_line_length = len(fullmsg)
233                 clear_line = '\r'
234             else:
235                 clear_line = ('\r\x1b[K' if sys.stderr.isatty() else '\r')
236             self.to_screen(clear_line + fullmsg, skip_eol=not is_last_line)
237         self.to_console_title('youtube-dl ' + msg)
238
239     def report_progress(self, s):
240         if s['status'] == 'finished':
241             if self.params.get('noprogress', False):
242                 self.to_screen('[download] Download completed')
243             else:
244                 s['_total_bytes_str'] = format_bytes(s['total_bytes'])
245                 if s.get('elapsed') is not None:
246                     s['_elapsed_str'] = self.format_seconds(s['elapsed'])
247                     msg_template = '100%% of %(_total_bytes_str)s in %(_elapsed_str)s'
248                 else:
249                     msg_template = '100%% of %(_total_bytes_str)s'
250                 self._report_progress_status(
251                     msg_template % s, is_last_line=True)
252
253         if self.params.get('noprogress'):
254             return
255
256         if s['status'] != 'downloading':
257             return
258
259         if s.get('eta') is not None:
260             s['_eta_str'] = self.format_eta(s['eta'])
261         else:
262             s['_eta_str'] = 'Unknown ETA'
263
264         if s.get('total_bytes') and s.get('downloaded_bytes') is not None:
265             s['_percent_str'] = self.format_percent(100 * s['downloaded_bytes'] / s['total_bytes'])
266         elif s.get('total_bytes_estimate') and s.get('downloaded_bytes') is not None:
267             s['_percent_str'] = self.format_percent(100 * s['downloaded_bytes'] / s['total_bytes_estimate'])
268         else:
269             if s.get('downloaded_bytes') == 0:
270                 s['_percent_str'] = self.format_percent(0)
271             else:
272                 s['_percent_str'] = 'Unknown %'
273
274         if s.get('speed') is not None:
275             s['_speed_str'] = self.format_speed(s['speed'])
276         else:
277             s['_speed_str'] = 'Unknown speed'
278
279         if s.get('total_bytes') is not None:
280             s['_total_bytes_str'] = format_bytes(s['total_bytes'])
281             msg_template = '%(_percent_str)s of %(_total_bytes_str)s at %(_speed_str)s ETA %(_eta_str)s'
282         elif s.get('total_bytes_estimate') is not None:
283             s['_total_bytes_estimate_str'] = format_bytes(s['total_bytes_estimate'])
284             msg_template = '%(_percent_str)s of ~%(_total_bytes_estimate_str)s at %(_speed_str)s ETA %(_eta_str)s'
285         else:
286             if s.get('downloaded_bytes') is not None:
287                 s['_downloaded_bytes_str'] = format_bytes(s['downloaded_bytes'])
288                 if s.get('elapsed'):
289                     s['_elapsed_str'] = self.format_seconds(s['elapsed'])
290                     msg_template = '%(_downloaded_bytes_str)s at %(_speed_str)s (%(_elapsed_str)s)'
291                 else:
292                     msg_template = '%(_downloaded_bytes_str)s at %(_speed_str)s'
293             else:
294                 msg_template = '%(_percent_str)s % at %(_speed_str)s ETA %(_eta_str)s'
295
296         self._report_progress_status(msg_template % s)
297
298     def report_resuming_byte(self, resume_len):
299         """Report attempt to resume at given byte."""
300         self.to_screen('[download] Resuming download at byte %s' % resume_len)
301
302     def report_retry(self, count, retries):
303         """Report retry in case of HTTP error 5xx"""
304         self.to_screen(
305             '[download] Got server HTTP error. Retrying (attempt %d of %s)...'
306             % (count, self.format_retries(retries)))
307
308     def report_file_already_downloaded(self, file_name):
309         """Report file has already been fully downloaded."""
310         try:
311             self.to_screen('[download] %s has already been downloaded' % file_name)
312         except UnicodeEncodeError:
313             self.to_screen('[download] The file has already been downloaded')
314
315     def report_unable_to_resume(self):
316         """Report it was impossible to resume download."""
317         self.to_screen('[download] Unable to resume')
318
319     def download(self, filename, info_dict):
320         """Download to a filename using the info from info_dict
321         Return True on success and False otherwise
322         """
323
324         nooverwrites_and_exists = (
325             self.params.get('nooverwrites', False) and
326             os.path.exists(encodeFilename(filename))
327         )
328
329         continuedl_and_exists = (
330             self.params.get('continuedl', True) and
331             os.path.isfile(encodeFilename(filename)) and
332             not self.params.get('nopart', False)
333         )
334
335         # Check file already present
336         if filename != '-' and (nooverwrites_and_exists or continuedl_and_exists):
337             self.report_file_already_downloaded(filename)
338             self._hook_progress({
339                 'filename': filename,
340                 'status': 'finished',
341                 'total_bytes': os.path.getsize(encodeFilename(filename)),
342             })
343             return True
344
345         sleep_interval = self.params.get('sleep_interval')
346         if sleep_interval:
347             self.to_screen('[download] Sleeping %s seconds...' % sleep_interval)
348             time.sleep(sleep_interval)
349
350         return self.real_download(filename, info_dict)
351
352     def real_download(self, filename, info_dict):
353         """Real download process. Redefine in subclasses."""
354         raise NotImplementedError('This method must be implemented by subclasses')
355
356     def _hook_progress(self, status):
357         for ph in self._progress_hooks:
358             ph(status)
359
360     def add_progress_hook(self, ph):
361         # See YoutubeDl.py (search for progress_hooks) for a description of
362         # this interface
363         self._progress_hooks.append(ph)
364
365     def _debug_cmd(self, args, exe=None):
366         if not self.params.get('verbose', False):
367             return
368
369         str_args = [decodeArgument(a) for a in args]
370
371         if exe is None:
372             exe = os.path.basename(str_args[0])
373
374         try:
375             import pipes
376             shell_quote = lambda args: ' '.join(map(pipes.quote, str_args))
377         except ImportError:
378             shell_quote = repr
379         self.to_screen('[debug] %s command line: %s' % (
380             exe, shell_quote(str_args)))