Stream responses on /vi and /ggpht endpoints

The chunk_size=None parameter to iter_content lets us consume data as
soon as it arrives
https://docs.python-requests.org/en/master/api/#requests.Response.iter_content
This commit is contained in:
Lomanic 2021-11-06 20:10:27 +01:00 committed by Cadence Ember
parent 66b7d1bec8
commit 0a13ab88cb
No known key found for this signature in database
GPG Key ID: BC1C2C61CF521B17
1 changed files with 4 additions and 4 deletions

View File

@ -123,17 +123,17 @@ class NewLeaf(object):
@cherrypy.expose @cherrypy.expose
def vi(self, id, file): def vi(self, id, file):
with requests.get("https://i.ytimg.com/vi/{}/{}".format(id, file)) as r: with requests.get("https://i.ytimg.com/vi/{}/{}".format(id, file), stream=True) as r:
r.raise_for_status() r.raise_for_status()
cherrypy.response.headers["content-type"] = r.headers["content-type"] cherrypy.response.headers["content-type"] = r.headers["content-type"]
return r # no idea if this is a good way to do it, but it definitely works! :D return next(r.iter_content(chunk_size=None))
@cherrypy.expose @cherrypy.expose
def ggpht(self, *path): def ggpht(self, *path):
with requests.get("https://yt3.ggpht.com/{}".format("/".join(path))) as r: with requests.get("https://yt3.ggpht.com/{}".format("/".join(path)), stream=True) as r:
r.raise_for_status() r.raise_for_status()
cherrypy.response.headers["content-type"] = r.headers["content-type"] cherrypy.response.headers["content-type"] = r.headers["content-type"]
return r return next(r.iter_content(chunk_size=None))
bind_port = getattr(configuration, "bind_port", 3000) bind_port = getattr(configuration, "bind_port", 3000)
bind_host = getattr(configuration, "bind_host", "0.0.0.0") bind_host = getattr(configuration, "bind_host", "0.0.0.0")