summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorPhilipp Hagemeister <[email protected]>2014-04-30 01:50:33 +0200
committerPhilipp Hagemeister <[email protected]>2014-04-30 01:50:33 +0200
commit33ab8453c4a89564bf14fc800874f09d66cdcf4a (patch)
tree66956de3d0faef61398580e3aa97423b45caff69
parent57b8d84cd9e0bbd67fb6fc51ebea3732acbf2a25 (diff)
parentdf297c879434d5306afec95c9abdb39a7ba7e870 (diff)
downloadyoutube-dl-33ab8453c4a89564bf14fc800874f09d66cdcf4a.tar.gz
youtube-dl-33ab8453c4a89564bf14fc800874f09d66cdcf4a.zip
Merge pull request #2813 from dstftw/test-real-download-improvement
Improve download mechanism when Range HTTP header is ignored
-rw-r--r--youtube_dl/downloader/http.py22
1 files changed, 19 insertions, 3 deletions
diff --git a/youtube_dl/downloader/http.py b/youtube_dl/downloader/http.py
index cc8b9c9a7..7e1c1d715 100644
--- a/youtube_dl/downloader/http.py
+++ b/youtube_dl/downloader/http.py
@@ -14,6 +14,8 @@ from ..utils import (
class HttpFD(FileDownloader):
+ _TEST_FILE_SIZE = 10241
+
def real_download(self, filename, info_dict):
url = info_dict['url']
tmpfilename = self.temp_name(filename)
@@ -28,8 +30,10 @@ class HttpFD(FileDownloader):
basic_request = compat_urllib_request.Request(url, None, headers)
request = compat_urllib_request.Request(url, None, headers)
- if self.params.get('test', False):
- request.add_header('Range', 'bytes=0-10240')
+ is_test = self.params.get('test', False)
+
+ if is_test:
+ request.add_header('Range', 'bytes=0-%s' % str(self._TEST_FILE_SIZE - 1))
# Establish possible resume length
if os.path.isfile(encodeFilename(tmpfilename)):
@@ -100,6 +104,15 @@ class HttpFD(FileDownloader):
return False
data_len = data.info().get('Content-length', None)
+
+ # Range HTTP header may be ignored/unsupported by a webserver
+ # (e.g. extractor/scivee.py, extractor/bambuser.py).
+ # However, for a test we still would like to download just a piece of a file.
+ # To achieve this we limit data_len to _TEST_FILE_SIZE and manually control
+ # block size when downloading a file.
+ if is_test and data_len > self._TEST_FILE_SIZE:
+ data_len = self._TEST_FILE_SIZE
+
if data_len is not None:
data_len = int(data_len) + resume_len
min_data_len = self.params.get("min_filesize", None)
@@ -118,7 +131,7 @@ class HttpFD(FileDownloader):
while True:
# Download and write
before = time.time()
- data_block = data.read(block_size)
+ data_block = data.read(block_size if not is_test else min(block_size, data_len - byte_counter))
after = time.time()
if len(data_block) == 0:
break
@@ -162,6 +175,9 @@ class HttpFD(FileDownloader):
'speed': speed,
})
+ if is_test and byte_counter == data_len:
+ break
+
# Apply rate limit
self.slow_down(start, byte_counter - resume_len)