+ --write-info-json

This commit is contained in:
Philipp Hagemeister 2011-07-10 21:39:36 +02:00
parent 437d76c19a
commit 6eb08fbf8b

View File

@ -413,6 +413,7 @@ class FileDownloader(object):
nopart: Do not use temporary .part files. nopart: Do not use temporary .part files.
updatetime: Use the Last-modified header to set output file timestamps. updatetime: Use the Last-modified header to set output file timestamps.
writedescription: Write the video description to a .description file writedescription: Write the video description to a .description file
writeinfojson: Write the video description to a .info.json file
""" """
params = None params = None
@ -609,8 +610,12 @@ class FileDownloader(object):
pass pass
def report_writedescription(self, descfn): def report_writedescription(self, descfn):
""" Report that the description file has been written """ """ Report that the description file is being written """
self.to_screen(u'[info] Video description written to: %s' % descfn, ignore_encoding_errors=True) self.to_screen(u'[info] Writing video description to: %s' % descfn, ignore_encoding_errors=True)
def report_writeinfojson(self, infofn):
""" Report that the metadata file has been written """
self.to_screen(u'[info] Video description metadata as JSON to: %s' % infofn, ignore_encoding_errors=True)
def report_destination(self, filename): def report_destination(self, filename):
"""Report destination filename.""" """Report destination filename."""
@ -701,13 +706,29 @@ class FileDownloader(object):
if self.params.get('writedescription', False): if self.params.get('writedescription', False):
try: try:
descfn = filename + '.description' descfn = filename + '.description'
self.report_writedescription(descfn)
with contextlib.closing(open(descfn, 'wb')) as descfile: with contextlib.closing(open(descfn, 'wb')) as descfile:
descfile.write(info_dict['description'].encode('utf-8')) descfile.write(info_dict['description'].encode('utf-8'))
self.report_writedescription(descfn)
except (OSError, IOError): except (OSError, IOError):
self.trouble(u'ERROR: Cannot write description file: %s' % str(descfn)) self.trouble(u'ERROR: Cannot write description file: %s' % str(descfn))
return return
print(repr(self.params))
if self.params.get('writeinfojson', False):
infofn = filename + '.info.json'
self.report_writeinfojson(infofn)
try:
json.dump
except (NameError,AttributeError):
self.trouble(u'ERROR: No JSON encoder found. Update to Python 2.6+, setup a json module, or leave out --write-info-json.')
return
try:
with contextlib.closing(open(infofn, 'wb')) as infof:
json.dump(info_dict, infof)
except (OSError, IOError):
self.trouble(u'ERROR: Cannot write metadata to JSON file: %s' % str(infofn))
return
try: try:
success = self._do_download(filename, info_dict['url'].encode('utf-8'), info_dict.get('player_url', None)) success = self._do_download(filename, info_dict['url'].encode('utf-8'), info_dict.get('player_url', None))
except (OSError, IOError), err: except (OSError, IOError), err:
@ -3031,6 +3052,9 @@ if __name__ == '__main__':
filesystem.add_option('--write-description', filesystem.add_option('--write-description',
action='store_true', dest='writedescription', action='store_true', dest='writedescription',
help='write video description to a .description file', default=False) help='write video description to a .description file', default=False)
filesystem.add_option('--write-info-json',
action='store_true', dest='writeinfojson',
help='write video metadata to a .info.json file', default=False)
parser.add_option_group(filesystem) parser.add_option_group(filesystem)
postproc = optparse.OptionGroup(parser, 'Post-processing Options') postproc = optparse.OptionGroup(parser, 'Post-processing Options')
@ -3169,6 +3193,7 @@ if __name__ == '__main__':
'nopart': opts.nopart, 'nopart': opts.nopart,
'updatetime': opts.updatetime, 'updatetime': opts.updatetime,
'writedescription': opts.writedescription, 'writedescription': opts.writedescription,
'writeinfojson': opts.writeinfojson,
}) })
fd.add_info_extractor(youtube_search_ie) fd.add_info_extractor(youtube_search_ie)
fd.add_info_extractor(youtube_pl_ie) fd.add_info_extractor(youtube_pl_ie)