Last active
January 28, 2018 21:58
-
-
Save singhpratyush/03dc308a5a483183387d4c03ac9d92c1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def generate_data_from_response(resp, chunk=2048): | |
for data_chunk in resp.iter_content(chunk_size=chunk): | |
yield data_chunk | |
def serve_partial(url, range_header, mime, size=3145728): | |
from_bytes, until_bytes = range_header.replace('bytes=', '').split('-') | |
if not until_bytes: | |
until_bytes = int(from_bytes) + size # Default size is 3MB | |
# Make request to YouTube | |
headers = {'Range': 'bytes=%s-%s' % (from_bytes, until_bytes)} | |
r = requests.get(url, headers=headers, stream=True) | |
# Build response | |
rv = Response(generate_data_from_response(r), 206, mimetype=mime, | |
direct_passthrough=True) | |
rv.headers.add('Content-Range', r.headers.get('Content-Range')) | |
rv.headers.add('Content-Length', r.headers['Content-Length']) | |
return rv |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment