common.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. if now is None:
  67. now = time.time()
  68. dif = now - start
  69. if current == 0 or dif < 0.001: # One millisecond
  70. return None
  71. rate = float(current) / dif
  72. return int((float(total) - float(current)) / rate)
  73. @staticmethod
  74. def format_eta(eta):
  75. if eta is None:
  76. return '--:--'
  77. return FileDownloader.format_seconds(eta)
  78. @staticmethod
  79. def calc_speed(start, now, bytes):
  80. dif = now - start
  81. if bytes == 0 or dif < 0.001: # One millisecond
  82. return None
  83. return float(bytes) / dif
  84. @staticmethod
  85. def format_speed(speed):
  86. if speed is None:
  87. return '%10s' % '---b/s'
  88. return '%10s' % ('%s/s' % format_bytes(speed))
  89. @staticmethod
  90. def best_block_size(elapsed_time, bytes):
  91. new_min = max(bytes / 2.0, 1.0)
  92. new_max = min(max(bytes * 2.0, 1.0), 4194304) # Do not surpass 4 MB
  93. if elapsed_time < 0.001:
  94. return int(new_max)
  95. rate = bytes / elapsed_time
  96. if rate > new_max:
  97. return int(new_max)
  98. if rate < new_min:
  99. return int(new_min)
  100. return int(rate)
  101. @staticmethod
  102. def parse_bytes(bytestr):
  103. """Parse a string indicating a byte quantity into an integer."""
  104. matchobj = re.match(r'(?i)^(\d+(?:\.\d+)?)([kMGTPEZY]?)$', bytestr)
  105. if matchobj is None:
  106. return None
  107. number = float(matchobj.group(1))
  108. multiplier = 1024.0 ** 'bkmgtpezy'.index(matchobj.group(2).lower())
  109. return int(round(number * multiplier))
  110. def to_screen(self, *args, **kargs):
  111. self.ydl.to_screen(*args, **kargs)
  112. def to_stderr(self, message):
  113. self.ydl.to_screen(message)
  114. def to_console_title(self, message):
  115. self.ydl.to_console_title(message)
  116. def trouble(self, *args, **kargs):
  117. self.ydl.trouble(*args, **kargs)
  118. def report_warning(self, *args, **kargs):
  119. self.ydl.report_warning(*args, **kargs)
  120. def report_error(self, *args, **kargs):
  121. self.ydl.report_error(*args, **kargs)
  122. def slow_down(self, start_time, now, byte_counter):
  123. """Sleep if the download speed is over the rate limit."""
  124. rate_limit = self.params.get('ratelimit', None)
  125. if rate_limit is None or byte_counter == 0:
  126. return
  127. if now is None:
  128. now = time.time()
  129. elapsed = now - start_time
  130. if elapsed <= 0.0:
  131. return
  132. speed = float(byte_counter) / elapsed
  133. if speed > rate_limit:
  134. time.sleep(max((byte_counter / rate_limit) - elapsed, 0))
  135. def temp_name(self, filename):
  136. """Returns a temporary filename for the given filename."""
  137. if self.params.get('nopart', False) or filename == u'-' or \
  138. (os.path.exists(encodeFilename(filename)) and not os.path.isfile(encodeFilename(filename))):
  139. return filename
  140. return filename + u'.part'
  141. def undo_temp_name(self, filename):
  142. if filename.endswith(u'.part'):
  143. return filename[:-len(u'.part')]
  144. return filename
  145. def try_rename(self, old_filename, new_filename):
  146. try:
  147. if old_filename == new_filename:
  148. return
  149. os.rename(encodeFilename(old_filename), encodeFilename(new_filename))
  150. except (IOError, OSError) as err:
  151. self.report_error(u'unable to rename file: %s' % compat_str(err))
  152. def try_utime(self, filename, last_modified_hdr):
  153. """Try to set the last-modified time of the given file."""
  154. if last_modified_hdr is None:
  155. return
  156. if not os.path.isfile(encodeFilename(filename)):
  157. return
  158. timestr = last_modified_hdr
  159. if timestr is None:
  160. return
  161. filetime = timeconvert(timestr)
  162. if filetime is None:
  163. return filetime
  164. # Ignore obviously invalid dates
  165. if filetime == 0:
  166. return
  167. try:
  168. os.utime(filename, (time.time(), filetime))
  169. except:
  170. pass
  171. return filetime
  172. def report_destination(self, filename):
  173. """Report destination filename."""
  174. self.to_screen(u'[download] Destination: ' + filename)
  175. def _report_progress_status(self, msg, is_last_line=False):
  176. fullmsg = u'[download] ' + msg
  177. if self.params.get('progress_with_newline', False):
  178. self.to_screen(fullmsg)
  179. else:
  180. if os.name == 'nt':
  181. prev_len = getattr(self, '_report_progress_prev_line_length',
  182. 0)
  183. if prev_len > len(fullmsg):
  184. fullmsg += u' ' * (prev_len - len(fullmsg))
  185. self._report_progress_prev_line_length = len(fullmsg)
  186. clear_line = u'\r'
  187. else:
  188. clear_line = (u'\r\x1b[K' if sys.stderr.isatty() else u'\r')
  189. self.to_screen(clear_line + fullmsg, skip_eol=not is_last_line)
  190. self.to_console_title(u'youtube-dl ' + msg)
  191. def report_progress(self, percent, data_len_str, speed, eta):
  192. """Report download progress."""
  193. if self.params.get('noprogress', False):
  194. return
  195. if eta is not None:
  196. eta_str = self.format_eta(eta)
  197. else:
  198. eta_str = 'Unknown ETA'
  199. if percent is not None:
  200. percent_str = self.format_percent(percent)
  201. else:
  202. percent_str = 'Unknown %'
  203. speed_str = self.format_speed(speed)
  204. msg = (u'%s of %s at %s ETA %s' %
  205. (percent_str, data_len_str, speed_str, eta_str))
  206. self._report_progress_status(msg)
  207. def report_progress_live_stream(self, downloaded_data_len, speed, elapsed):
  208. if self.params.get('noprogress', False):
  209. return
  210. downloaded_str = format_bytes(downloaded_data_len)
  211. speed_str = self.format_speed(speed)
  212. elapsed_str = FileDownloader.format_seconds(elapsed)
  213. msg = u'%s at %s (%s)' % (downloaded_str, speed_str, elapsed_str)
  214. self._report_progress_status(msg)
  215. def report_finish(self, data_len_str, tot_time):
  216. """Report download finished."""
  217. if self.params.get('noprogress', False):
  218. self.to_screen(u'[download] Download completed')
  219. else:
  220. self._report_progress_status(
  221. (u'100%% of %s in %s' %
  222. (data_len_str, self.format_seconds(tot_time))),
  223. is_last_line=True)
  224. def report_resuming_byte(self, resume_len):
  225. """Report attempt to resume at given byte."""
  226. self.to_screen(u'[download] Resuming download at byte %s' % resume_len)
  227. def report_retry(self, count, retries):
  228. """Report retry in case of HTTP error 5xx"""
  229. self.to_screen(u'[download] Got server HTTP error. Retrying (attempt %d of %d)...' % (count, retries))
  230. def report_file_already_downloaded(self, file_name):
  231. """Report file has already been fully downloaded."""
  232. try:
  233. self.to_screen(u'[download] %s has already been downloaded' % file_name)
  234. except UnicodeEncodeError:
  235. self.to_screen(u'[download] The file has already been downloaded')
  236. def report_unable_to_resume(self):
  237. """Report it was impossible to resume download."""
  238. self.to_screen(u'[download] Unable to resume')
  239. def download(self, filename, info_dict):
  240. """Download to a filename using the info from info_dict
  241. Return True on success and False otherwise
  242. """
  243. # Check file already present
  244. if self.params.get('continuedl', False) and os.path.isfile(encodeFilename(filename)) and not self.params.get('nopart', False):
  245. self.report_file_already_downloaded(filename)
  246. self._hook_progress({
  247. 'filename': filename,
  248. 'status': 'finished',
  249. 'total_bytes': os.path.getsize(encodeFilename(filename)),
  250. })
  251. return True
  252. return self.real_download(filename, info_dict)
  253. def real_download(self, filename, info_dict):
  254. """Real download process. Redefine in subclasses."""
  255. raise NotImplementedError(u'This method must be implemented by sublcasses')
  256. def _hook_progress(self, status):
  257. for ph in self._progress_hooks:
  258. ph(status)
  259. def add_progress_hook(self, ph):
  260. """ ph gets called on download progress, with a dictionary with the entries
  261. * filename: The final filename
  262. * status: One of "downloading" and "finished"
  263. It can also have some of the following entries:
  264. * downloaded_bytes: Bytes on disks
  265. * total_bytes: Total bytes, None if unknown
  266. * tmpfilename: The filename we're currently writing to
  267. * eta: The estimated time in seconds, None if unknown
  268. * speed: The download speed in bytes/second, None if unknown
  269. Hooks are guaranteed to be called at least once (with status "finished")
  270. if the download is successful.
  271. """
  272. self._progress_hooks.append(ph)