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

Handle file open mode correctly (fixes issue #76)

This commit is contained in:
Ricardo Garcia 2009-12-21 21:43:15 +01:00
parent c39c05cdd7
commit 9c457d2a20

View File

@ -392,19 +392,21 @@ class FileDownloader(object):
def _do_download(self, filename, url): def _do_download(self, filename, url):
stream = None stream = None
open_mode = 'ab' open_mode = 'wb'
basic_request = urllib2.Request(url, None, std_headers) basic_request = urllib2.Request(url, None, std_headers)
request = urllib2.Request(url, None, std_headers) request = urllib2.Request(url, None, std_headers)
# Attempt to resume download with "continuedl" option # Establish possible resume length
if os.path.isfile(filename): if os.path.isfile(filename):
resume_len = os.path.getsize(filename) resume_len = os.path.getsize(filename)
else: else:
resume_len = 0 resume_len = 0
# Request parameters in case of being able to resume
if self.params.get('continuedl', False) and resume_len != 0: if self.params.get('continuedl', False) and resume_len != 0:
self.report_resuming_byte(resume_len) self.report_resuming_byte(resume_len)
request.add_header('Range','bytes=%d-' % resume_len) request.add_header('Range','bytes=%d-' % resume_len)
open_mode = 'ab'
# Establish connection # Establish connection
try: try:
@ -412,12 +414,16 @@ class FileDownloader(object):
except (urllib2.HTTPError, ), err: except (urllib2.HTTPError, ), err:
if err.code != 416: # 416 is 'Requested range not satisfiable' if err.code != 416: # 416 is 'Requested range not satisfiable'
raise raise
# Unable to resume
data = urllib2.urlopen(basic_request) data = urllib2.urlopen(basic_request)
content_length = data.info()['Content-Length'] content_length = data.info()['Content-Length']
if content_length is not None and long(content_length) == resume_len: if content_length is not None and long(content_length) == resume_len:
# Because the file had already been fully downloaded
self.report_file_already_downloaded(filename) self.report_file_already_downloaded(filename)
return True return True
else: else:
# Because the server didn't let us
self.report_unable_to_resume() self.report_unable_to_resume()
open_mode = 'wb' open_mode = 'wb'