Skip to content

Instantly share code, notes, and snippets.

@singhpratyush
Last active January 28, 2018 21:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save singhpratyush/03dc308a5a483183387d4c03ac9d92c1 to your computer and use it in GitHub Desktop.
Save singhpratyush/03dc308a5a483183387d4c03ac9d92c1 to your computer and use it in GitHub Desktop.
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