317 lines
11 KiB

11 years ago
  1. import os
  2. import re
  3. import sys
  4. import time
  5. from ..utils import (
  6. compat_str,
  7. encodeFilename,
  8. format_bytes,
  9. timeconvert,
  10. )
  11. class FileDownloader(object):
  12. """File Downloader class.
  13. File downloader objects are the ones responsible of downloading the
  14. actual video file and writing it to disk.
  15. File downloaders accept a lot of parameters. In order not to saturate
  16. the object constructor with arguments, it receives a dictionary of
  17. options instead.
  18. Available options:
  19. verbose: Print additional info to stdout.
  20. quiet: Do not print messages to stdout.
  21. ratelimit: Download speed limit, in bytes/sec.
  22. retries: Number of times to retry for HTTP error 5xx
  23. buffersize: Size of download buffer in bytes.
  24. noresizebuffer: Do not automatically resize the download buffer.
  25. continuedl: Try to continue downloads if possible.
  26. noprogress: Do not print the progress bar.
  27. logtostderr: Log messages to stderr instead of stdout.
  28. consoletitle: Display progress in console window's titlebar.
  29. nopart: Do not use temporary .part files.
  30. updatetime: Use the Last-modified header to set output file timestamps.
  31. test: Download only first bytes to test the downloader.
  32. min_filesize: Skip files smaller than this size
  33. max_filesize: Skip files larger than this size
  34. Subclasses of this one must re-define the real_download method.
  35. """
  36. params = None
  37. def __init__(self, ydl, params):
  38. """Create a FileDownloader object with the given options."""
  39. self.ydl = ydl
  40. self._progress_hooks = []
  41. self.params = params
  42. @staticmethod
  43. def format_seconds(seconds):
  44. (mins, secs) = divmod(seconds, 60)
  45. (hours, mins) = divmod(mins, 60)
  46. if hours > 99:
  47. return '--:--:--'
  48. if hours == 0:
  49. return '%02d:%02d' % (mins, secs)
  50. else:
  51. return '%02d:%02d:%02d' % (hours, mins, secs)
  52. @staticmethod
  53. def calc_percent(byte_counter, data_len):
  54. if data_len is None:
  55. return None
  56. return float(byte_counter) / float(data_len) * 100.0
  57. @staticmethod
  58. def format_percent(percent):
  59. if percent is None:
  60. return '---.-%'
  61. return '%6s' % ('%3.1f%%' % percent)
  62. @staticmethod
  63. def calc_eta(start, now, total, current):
  64. if total is None:
  65. return None
  66. dif = now - start
  67. if current == 0 or dif < 0.001: # One millisecond
  68. return None
  69. rate = float(current) / dif
  70. return int((float(total) - float(current)) / rate)
  71. @staticmethod
  72. def format_eta(eta):
  73. if eta is None:
  74. return '--:--'
  75. return FileDownloader.format_seconds(eta)
  76. @staticmethod
  77. def calc_speed(start, now, bytes):
  78. dif = now - start
  79. if bytes == 0 or dif < 0.001: # One millisecond
  80. return None
  81. return float(bytes) / dif
  82. @staticmethod
  83. def format_speed(speed):
  84. if speed is None:
  85. return '%10s' % '---b/s'
  86. return '%10s' % ('%s/s' % format_bytes(speed))
  87. @staticmethod
  88. def best_block_size(elapsed_time, bytes):
  89. new_min = max(bytes / 2.0, 1.0)
  90. new_max = min(max(bytes * 2.0, 1.0), 4194304) # Do not surpass 4 MB
  91. if elapsed_time < 0.001:
  92. return int(new_max)
  93. rate = bytes / elapsed_time
  94. if rate > new_max:
  95. return int(new_max)
  96. if rate < new_min:
  97. return int(new_min)
  98. return int(rate)
  99. @staticmethod
  100. def parse_bytes(bytestr):
  101. """Parse a string indicating a byte quantity into an integer."""
  102. matchobj = re.match(r'(?i)^(\d+(?:\.\d+)?)([kMGTPEZY]?)$', bytestr)
  103. if matchobj is None:
  104. return None
  105. number = float(matchobj.group(1))
  106. multiplier = 1024.0 ** 'bkmgtpezy'.index(matchobj.group(2).lower())
  107. return int(round(number * multiplier))
  108. def to_screen(self, *args, **kargs):
  109. self.ydl.to_screen(*args, **kargs)
  110. def to_stderr(self, message):
  111. self.ydl.to_screen(message)
  112. def to_console_title(self, message):
  113. self.ydl.to_console_title(message)
  114. def trouble(self, *args, **kargs):
  115. self.ydl.trouble(*args, **kargs)
  116. def report_warning(self, *args, **kargs):
  117. self.ydl.report_warning(*args, **kargs)
  118. def report_error(self, *args, **kargs):
  119. self.ydl.report_error(*args, **kargs)
  120. def slow_down(self, start_time, byte_counter):
  121. """Sleep if the download speed is over the rate limit."""
  122. rate_limit = self.params.get('ratelimit', None)
  123. if rate_limit is None or byte_counter == 0:
  124. return
  125. now = time.time()
  126. elapsed = now - start_time
  127. if elapsed <= 0.0:
  128. return
  129. speed = float(byte_counter) / elapsed
  130. if speed > rate_limit:
  131. time.sleep((byte_counter - rate_limit * (now - start_time)) / rate_limit)
  132. def temp_name(self, filename):
  133. """Returns a temporary filename for the given filename."""
  134. if self.params.get('nopart', False) or filename == u'-' or \
  135. (os.path.exists(encodeFilename(filename)) and not os.path.isfile(encodeFilename(filename))):
  136. return filename
  137. return filename + u'.part'
  138. def undo_temp_name(self, filename):
  139. if filename.endswith(u'.part'):
  140. return filename[:-len(u'.part')]
  141. return filename
  142. def try_rename(self, old_filename, new_filename):
  143. try:
  144. if old_filename == new_filename:
  145. return
  146. os.rename(encodeFilename(old_filename), encodeFilename(new_filename))
  147. except (IOError, OSError) as err:
  148. self.report_error(u'unable to rename file: %s' % compat_str(err))
  149. def try_utime(self, filename, last_modified_hdr):
  150. """Try to set the last-modified time of the given file."""
  151. if last_modified_hdr is None:
  152. return
  153. if not os.path.isfile(encodeFilename(filename)):
  154. return
  155. timestr = last_modified_hdr
  156. if timestr is None:
  157. return
  158. filetime = timeconvert(timestr)
  159. if filetime is None:
  160. return filetime
  161. # Ignore obviously invalid dates
  162. if filetime == 0:
  163. return
  164. try:
  165. os.utime(filename, (time.time(), filetime))
  166. except:
  167. pass
  168. return filetime
  169. def report_destination(self, filename):
  170. """Report destination filename."""
  171. self.to_screen(u'[download] Destination: ' + filename)
  172. def _report_progress_status(self, msg, is_last_line=False):
  173. fullmsg = u'[download] ' + msg
  174. if self.params.get('progress_with_newline', False):
  175. self.to_screen(fullmsg)
  176. else:
  177. if os.name == 'nt':
  178. prev_len = getattr(self, '_report_progress_prev_line_length',
  179. 0)
  180. if prev_len > len(fullmsg):
  181. fullmsg += u' ' * (prev_len - len(fullmsg))
  182. self._report_progress_prev_line_length = len(fullmsg)
  183. clear_line = u'\r'
  184. else:
  185. clear_line = (u'\r\x1b[K' if sys.stderr.isatty() else u'\r')
  186. self.to_screen(clear_line + fullmsg, skip_eol=not is_last_line)
  187. self.to_console_title(u'youtube-dl ' + msg)
  188. def report_progress(self, percent, data_len_str, speed, eta):
  189. """Report download progress."""
  190. if self.params.get('noprogress', False):
  191. return
  192. if eta is not None:
  193. eta_str = self.format_eta(eta)
  194. else:
  195. eta_str = 'Unknown ETA'
  196. if percent is not None:
  197. percent_str = self.format_percent(percent)
  198. else:
  199. percent_str = 'Unknown %'
  200. speed_str = self.format_speed(speed)
  201. msg = (u'%s of %s at %s ETA %s' %
  202. (percent_str, data_len_str, speed_str, eta_str))
  203. self._report_progress_status(msg)
  204. def report_progress_live_stream(self, downloaded_data_len, speed, elapsed):
  205. if self.params.get('noprogress', False):
  206. return
  207. downloaded_str = format_bytes(downloaded_data_len)
  208. speed_str = self.format_speed(speed)
  209. elapsed_str = FileDownloader.format_seconds(elapsed)
  210. msg = u'%s at %s (%s)' % (downloaded_str, speed_str, elapsed_str)
  211. self._report_progress_status(msg)
  212. def report_finish(self, data_len_str, tot_time):
  213. """Report download finished."""
  214. if self.params.get('noprogress', False):
  215. self.to_screen(u'[download] Download completed')
  216. else:
  217. self._report_progress_status(
  218. (u'100%% of %s in %s' %
  219. (data_len_str, self.format_seconds(tot_time))),
  220. is_last_line=True)
  221. def report_resuming_byte(self, resume_len):
  222. """Report attempt to resume at given byte."""
  223. self.to_screen(u'[download] Resuming download at byte %s' % resume_len)
  224. def report_retry(self, count, retries):
  225. """Report retry in case of HTTP error 5xx"""
  226. self.to_screen(u'[download] Got server HTTP error. Retrying (attempt %d of %d)...' % (count, retries))
  227. def report_file_already_downloaded(self, file_name):
  228. """Report file has already been fully downloaded."""
  229. try:
  230. self.to_screen(u'[download] %s has already been downloaded' % file_name)
  231. except UnicodeEncodeError:
  232. self.to_screen(u'[download] The file has already been downloaded')
  233. def report_unable_to_resume(self):
  234. """Report it was impossible to resume download."""
  235. self.to_screen(u'[download] Unable to resume')
  236. def download(self, filename, info_dict):
  237. """Download to a filename using the info from info_dict
  238. Return True on success and False otherwise
  239. """
  240. # Check file already present
  241. if self.params.get('continuedl', False) and os.path.isfile(encodeFilename(filename)) and not self.params.get('nopart', False):
  242. self.report_file_already_downloaded(filename)
  243. self._hook_progress({
  244. 'filename': filename,
  245. 'status': 'finished',
  246. 'total_bytes': os.path.getsize(encodeFilename(filename)),
  247. })
  248. return True
  249. return self.real_download(filename, info_dict)
  250. def real_download(self, filename, info_dict):
  251. """Real download process. Redefine in subclasses."""
  252. raise NotImplementedError(u'This method must be implemented by sublcasses')
  253. def _hook_progress(self, status):
  254. for ph in self._progress_hooks:
  255. ph(status)
  256. def add_progress_hook(self, ph):
  257. """ ph gets called on download progress, with a dictionary with the entries
  258. * filename: The final filename
  259. * status: One of "downloading" and "finished"
  260. It can also have some of the following entries:
  261. * downloaded_bytes: Bytes on disks
  262. * total_bytes: Total bytes, None if unknown
  263. * tmpfilename: The filename we're currently writing to
  264. * eta: The estimated time in seconds, None if unknown
  265. * speed: The download speed in bytes/second, None if unknown
  266. Hooks are guaranteed to be called at least once (with status "finished")
  267. if the download is successful.
  268. """
  269. self._progress_hooks.append(ph)