check for overwriting files in the downloader (fixes #3916, closes #3829)

pull/8/head
Pierre 10 years ago committed by Jaime Marquínez Ferrándiz
parent f3ff1a3696
commit 4340decad2

@ -1121,49 +1121,46 @@ class YoutubeDL(object):
(info_dict['thumbnail'], compat_str(err))) (info_dict['thumbnail'], compat_str(err)))
if not self.params.get('skip_download', False): if not self.params.get('skip_download', False):
if self.params.get('nooverwrites', False) and os.path.exists(encodeFilename(filename)): try:
success = True def dl(name, info):
else: fd = get_suitable_downloader(info)(self, self.params)
try: for ph in self._progress_hooks:
def dl(name, info): fd.add_progress_hook(ph)
fd = get_suitable_downloader(info)(self, self.params) if self.params.get('verbose'):
for ph in self._progress_hooks: self.to_stdout('[debug] Invoking downloader on %r' % info.get('url'))
fd.add_progress_hook(ph) return fd.download(name, info)
if self.params.get('verbose'): if info_dict.get('requested_formats') is not None:
self.to_stdout('[debug] Invoking downloader on %r' % info.get('url')) downloaded = []
return fd.download(name, info) success = True
if info_dict.get('requested_formats') is not None: merger = FFmpegMergerPP(self, not self.params.get('keepvideo'))
downloaded = [] if not merger._executable:
success = True postprocessors = []
merger = FFmpegMergerPP(self, not self.params.get('keepvideo')) self.report_warning('You have requested multiple '
if not merger._executable: 'formats but ffmpeg or avconv are not installed.'
postprocessors = [] ' The formats won\'t be merged')
self.report_warning('You have requested multiple '
'formats but ffmpeg or avconv are not installed.'
' The formats won\'t be merged')
else:
postprocessors = [merger]
for f in info_dict['requested_formats']:
new_info = dict(info_dict)
new_info.update(f)
fname = self.prepare_filename(new_info)
fname = prepend_extension(fname, 'f%s' % f['format_id'])
downloaded.append(fname)
partial_success = dl(fname, new_info)
success = success and partial_success
info_dict['__postprocessors'] = postprocessors
info_dict['__files_to_merge'] = downloaded
else: else:
# Just a single file postprocessors = [merger]
success = dl(filename, info_dict) for f in info_dict['requested_formats']:
except (compat_urllib_error.URLError, compat_http_client.HTTPException, socket.error) as err: new_info = dict(info_dict)
self.report_error('unable to download video data: %s' % str(err)) new_info.update(f)
return fname = self.prepare_filename(new_info)
except (OSError, IOError) as err: fname = prepend_extension(fname, 'f%s' % f['format_id'])
raise UnavailableVideoError(err) downloaded.append(fname)
except (ContentTooShortError, ) as err: partial_success = dl(fname, new_info)
self.report_error('content too short (expected %s bytes and served %s)' % (err.expected, err.downloaded)) success = success and partial_success
return info_dict['__postprocessors'] = postprocessors
info_dict['__files_to_merge'] = downloaded
else:
# Just a single file
success = dl(filename, info_dict)
except (compat_urllib_error.URLError, compat_http_client.HTTPException, socket.error) as err:
self.report_error('unable to download video data: %s' % str(err))
return
except (OSError, IOError) as err:
raise UnavailableVideoError(err)
except (ContentTooShortError, ) as err:
self.report_error('content too short (expected %s bytes and served %s)' % (err.expected, err.downloaded))
return
if success: if success:
# Fixup content # Fixup content

@ -284,8 +284,19 @@ class FileDownloader(object):
"""Download to a filename using the info from info_dict """Download to a filename using the info from info_dict
Return True on success and False otherwise Return True on success and False otherwise
""" """
nooverwrites_and_exists = (
self.params.get('nooverwrites', False)
and os.path.exists(encodeFilename(filename))
)
continuedl_and_exists = (
self.params.get('continuedl', False)
and os.path.isfile(encodeFilename(filename))
and not self.params.get('nopart', False)
)
# Check file already present # Check file already present
if filename != '-' and self.params.get('continuedl', False) and os.path.isfile(encodeFilename(filename)) and not self.params.get('nopart', False): if filename != '-' and nooverwrites_and_exists or continuedl_and_exists:
self.report_file_already_downloaded(filename) self.report_file_already_downloaded(filename)
self._hook_progress({ self._hook_progress({
'filename': filename, 'filename': filename,

Loading…
Cancel
Save