]> git.gir.st - subscriptionfeed.git/blob - app/proxy/__init__.py
move anticsrf out of __init__, provide decorator for opting out
[subscriptionfeed.git] / app / proxy / __init__.py
1 import requests, requests_cache
2 from flask import Flask, Blueprint, request, Response
3 from flask_login import current_user
4
5 frontend = Blueprint('proxy', __name__)
6
7 def app():
8 app = Flask(__name__)
9 app.register_blueprint(frontend)
10 return app
11
12 # https://github.com/psf/requests/issues/5612
13 def silence_errors(generator):
14 try:
15 yield from generator
16 except requests.exceptions.RequestException:
17 return None
18
19 @frontend.route("/videoplayback")
20 def videoplayback():
21 fvip = request.args.get("fvip", "3")
22 mn = request.args.get("mn", "").split(",")[-1]
23 host = f"r{fvip}---{mn}.googlevideo.com"
24
25 REQUEST_HEADERS_WHITELIST = {
26 "accept",
27 "accept-encoding",
28 "cache-control",
29 "range",
30 }
31 RESPONSE_HEADERS_WHITELIST = {
32 "accept-ranges",
33 "cache-control",
34 "content-length",
35 "content-range",
36 "content-type",
37 "expires",
38 "x-content-type-options",
39 }
40
41 req_headers = {
42 k:v for k,v in request.headers
43 if k.lower() in REQUEST_HEADERS_WHITELIST
44 }
45 with requests_cache.disabled():
46 r = requests.get(f"https://{host}/videoplayback", request.args.to_dict(), headers=req_headers, stream=True)
47
48 resp_headers = {
49 k:v for k,v in r.headers.items()
50 if k.lower() in RESPONSE_HEADERS_WHITELIST
51 }
52 response = Response(silence_errors(r.iter_content(chunk_size=8192)), status=r.status_code, headers=resp_headers)
53 response.call_on_close(lambda: r.close())
54 return response
55
56
57 if __name__ == '__main__':
58 app().run(debug=True)
Imprint / Impressum