1
0
mirror of https://github.com/yt-dlp/yt-dlp.git synced 2024-11-02 01:02:48 +01:00

Apply --no-overwrites for --write-* files as well (Fixes #1980)

This commit is contained in:
Philipp Hagemeister 2013-12-16 04:39:04 +01:00
parent 525ef9227f
commit 7b6fefc9d4

View File

@ -366,22 +366,6 @@ def report_error(self, message, tb=None):
error_message = u'%s %s' % (_msg_header, message) error_message = u'%s %s' % (_msg_header, message)
self.trouble(error_message, tb) self.trouble(error_message, tb)
def report_writedescription(self, descfn):
""" Report that the description file is being written """
self.to_screen(u'[info] Writing video description to: ' + descfn)
def report_writesubtitles(self, sub_filename):
""" Report that the subtitles file is being written """
self.to_screen(u'[info] Writing video subtitles to: ' + sub_filename)
def report_writeinfojson(self, infofn):
""" Report that the metadata file has been written """
self.to_screen(u'[info] Video description metadata as JSON to: ' + infofn)
def report_writeannotations(self, annofn):
""" Report that the annotations file has been written. """
self.to_screen(u'[info] Writing video annotations to: ' + annofn)
def report_file_already_downloaded(self, file_name): def report_file_already_downloaded(self, file_name):
"""Report file has already been fully downloaded.""" """Report file has already been fully downloaded."""
try: try:
@ -791,28 +775,34 @@ def process_info(self, info_dict):
return return
if self.params.get('writedescription', False): if self.params.get('writedescription', False):
try: descfn = filename + u'.description'
descfn = filename + u'.description' if self.params.get('nooverwrites', False) and os.path.exists(encodeFilename(descfn)):
self.report_writedescription(descfn) self.to_screen(u'[info] Video description is already present')
with io.open(encodeFilename(descfn), 'w', encoding='utf-8') as descfile: else:
descfile.write(info_dict['description']) try:
except (KeyError, TypeError): self.to_screen(u'[info] Writing video description to: ' + descfn)
self.report_warning(u'There\'s no description to write.') with io.open(encodeFilename(descfn), 'w', encoding='utf-8') as descfile:
except (OSError, IOError): descfile.write(info_dict['description'])
self.report_error(u'Cannot write description file ' + descfn) except (KeyError, TypeError):
return self.report_warning(u'There\'s no description to write.')
except (OSError, IOError):
self.report_error(u'Cannot write description file ' + descfn)
return
if self.params.get('writeannotations', False): if self.params.get('writeannotations', False):
try: annofn = filename + u'.annotations.xml'
annofn = filename + u'.annotations.xml' if self.params.get('nooverwrites', False) and os.path.exists(encodeFilename(annofn)):
self.report_writeannotations(annofn) self.to_screen(u'[info] Video annotations are already present')
with io.open(encodeFilename(annofn), 'w', encoding='utf-8') as annofile: else:
annofile.write(info_dict['annotations']) try:
except (KeyError, TypeError): self.to_screen(u'[info] Writing video annotations to: ' + annofn)
self.report_warning(u'There are no annotations to write.') with io.open(encodeFilename(annofn), 'w', encoding='utf-8') as annofile:
except (OSError, IOError): annofile.write(info_dict['annotations'])
self.report_error(u'Cannot write annotations file: ' + annofn) except (KeyError, TypeError):
return self.report_warning(u'There are no annotations to write.')
except (OSError, IOError):
self.report_error(u'Cannot write annotations file: ' + annofn)
return
subtitles_are_requested = any([self.params.get('writesubtitles', False), subtitles_are_requested = any([self.params.get('writesubtitles', False),
self.params.get('writeautomaticsub')]) self.params.get('writeautomaticsub')])
@ -828,38 +818,48 @@ def process_info(self, info_dict):
continue continue
try: try:
sub_filename = subtitles_filename(filename, sub_lang, sub_format) sub_filename = subtitles_filename(filename, sub_lang, sub_format)
self.report_writesubtitles(sub_filename) if self.params.get('nooverwrites', False) and os.path.exists(encodeFilename(sub_filename)):
with io.open(encodeFilename(sub_filename), 'w', encoding='utf-8') as subfile: self.to_screen(u'[info] Video subtitle %s.%s is already_present' % (sub_lang, sub_format))
subfile.write(sub) else:
self.to_screen(u'[info] Writing video subtitles to: ' + sub_filename)
with io.open(encodeFilename(sub_filename), 'w', encoding='utf-8') as subfile:
subfile.write(sub)
except (OSError, IOError): except (OSError, IOError):
self.report_error(u'Cannot write subtitles file ' + descfn) self.report_error(u'Cannot write subtitles file ' + descfn)
return return
if self.params.get('writeinfojson', False): if self.params.get('writeinfojson', False):
infofn = os.path.splitext(filename)[0] + u'.info.json' infofn = os.path.splitext(filename)[0] + u'.info.json'
self.report_writeinfojson(infofn) if self.params.get('nooverwrites', False) and os.path.exists(encodeFilename(infofn)):
try: self.to_screen(u'[info] Video description metadata is already present')
json_info_dict = dict((k, v) for k, v in info_dict.items() if not k in ['urlhandle']) else:
write_json_file(json_info_dict, encodeFilename(infofn)) self.to_screen(u'[info] Writing video description metadata as JSON to: ' + infofn)
except (OSError, IOError): try:
self.report_error(u'Cannot write metadata to JSON file ' + infofn) json_info_dict = dict((k, v) for k, v in info_dict.items() if not k in ['urlhandle'])
return write_json_file(json_info_dict, encodeFilename(infofn))
except (OSError, IOError):
self.report_error(u'Cannot write metadata to JSON file ' + infofn)
return
if self.params.get('writethumbnail', False): if self.params.get('writethumbnail', False):
if info_dict.get('thumbnail') is not None: if info_dict.get('thumbnail') is not None:
thumb_format = determine_ext(info_dict['thumbnail'], u'jpg') thumb_format = determine_ext(info_dict['thumbnail'], u'jpg')
thumb_filename = os.path.splitext(filename)[0] + u'.' + thumb_format thumb_filename = os.path.splitext(filename)[0] + u'.' + thumb_format
self.to_screen(u'[%s] %s: Downloading thumbnail ...' % if self.params.get('nooverwrites', False) and os.path.exists(encodeFilename(infofn)):
(info_dict['extractor'], info_dict['id'])) self.to_screen(u'[%s] %s: Thumbnail is already present' %
try: (info_dict['extractor'], info_dict['id']))
uf = compat_urllib_request.urlopen(info_dict['thumbnail']) else:
with open(thumb_filename, 'wb') as thumbf: self.to_screen(u'[%s] %s: Downloading thumbnail ...' %
shutil.copyfileobj(uf, thumbf) (info_dict['extractor'], info_dict['id']))
self.to_screen(u'[%s] %s: Writing thumbnail to: %s' % try:
(info_dict['extractor'], info_dict['id'], thumb_filename)) uf = compat_urllib_request.urlopen(info_dict['thumbnail'])
except (compat_urllib_error.URLError, compat_http_client.HTTPException, socket.error) as err: with open(thumb_filename, 'wb') as thumbf:
self.report_warning(u'Unable to download thumbnail "%s": %s' % shutil.copyfileobj(uf, thumbf)
(info_dict['thumbnail'], compat_str(err))) self.to_screen(u'[%s] %s: Writing thumbnail to: %s' %
(info_dict['extractor'], info_dict['id'], thumb_filename))
except (compat_urllib_error.URLError, compat_http_client.HTTPException, socket.error) as err:
self.report_warning(u'Unable to download thumbnail "%s": %s' %
(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)): if self.params.get('nooverwrites', False) and os.path.exists(encodeFilename(filename)):