X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=youtube_dl%2FFileDownloader.py;h=d9a4ecd3a99c2127105d5ce3f87a4fe038fa1040;hb=99b0a1292b6ceccca576c8555e03bc13fb8e4bdd;hp=e8c62ce07adbff3b10942c0d21700b06592bdad1;hpb=ce4be3a91d5fe8f2970d00be5663a7aad12d2a6d;p=youtube-dl diff --git a/youtube_dl/FileDownloader.py b/youtube_dl/FileDownloader.py index e8c62ce07..d9a4ecd3a 100644 --- a/youtube_dl/FileDownloader.py +++ b/youtube_dl/FileDownloader.py @@ -421,11 +421,8 @@ class FileDownloader(object): try: descfn = filename + u'.description' self.report_writedescription(descfn) - descfile = open(encodeFilename(descfn), 'wb') - try: - descfile.write(info_dict['description'].encode('utf-8')) - finally: - descfile.close() + with io.open(encodeFilename(descfn), 'w', encoding='utf-8') as descfile: + descfile.write(info_dict['description']) except (OSError, IOError): self.trouble(u'ERROR: Cannot write description file ' + descfn) return @@ -436,11 +433,8 @@ class FileDownloader(object): try: srtfn = filename.rsplit('.', 1)[0] + u'.srt' self.report_writesubtitles(srtfn) - srtfile = open(encodeFilename(srtfn), 'wb') - try: - srtfile.write(info_dict['subtitles'].encode('utf-8')) - finally: - srtfile.close() + with io.open(encodeFilename(srtfn), 'w', encoding='utf-8') as srtfile: + srtfile.write(info_dict['subtitles']) except (OSError, IOError): self.trouble(u'ERROR: Cannot write subtitles file ' + descfn) return @@ -449,14 +443,8 @@ class FileDownloader(object): infofn = filename + u'.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 io.open(encodeFilename(infofn), 'w', 'utf-8') as infof: - json_info_dict = dict((k, v) for k,v in info_dict.items() if not k in ['urlhandle']) - json.dump(json_info_dict, infof) + json_info_dict = dict((k, v) for k,v in info_dict.items() if not k in ['urlhandle']) + write_json_file(json_info_dict, encodeFilename(infofn)) except (OSError, IOError): self.trouble(u'ERROR: Cannot write metadata to JSON file ' + infofn) return