fixyoutube/views.py (view raw)
1from fixyoutube import app
2from fixyoutube.db import get_video_from_cache, get_info, clear_cache
3import fixyoutube.constants as c
4from flask import request, redirect, abort, render_template, Response
5from requests import get
6import re
7
8def main_handler(request, video_id):
9 user_agent = request.headers.get("User-Agent", "")
10 result = re.findall(c.UA_REGEX, user_agent, flags=re.I)
11 if len(result) == 0:
12 return redirect(c.BASE_URL + video_id)
13
14 info = get_info(video_id)
15 if info is None:
16 return abort(400)
17
18 return render_template("base.html", info=info, base_url=c.BASE_URL)
19
20@app.route("/")
21def index_route():
22 return render_template("index.html", repo_url=c.REPO_URL)
23
24@app.route("/clear")
25def clear_route():
26 clear_cache()
27 return { "done": True }
28
29@app.route("/watch")
30def watch_route():
31 try:
32 video_id = request.args["v"]
33 if video_id == "":
34 raise KeyError
35 except KeyError:
36 return redirect("/")
37 return main_handler(request, video_id)
38
39@app.route('/<video_id>')
40def main_route(video_id):
41 return main_handler(request, video_id)
42
43@app.route('/proxy/<video_id>')
44def proxy(video_id):
45 result = get_video_from_cache(video_id)
46
47 if result is None:
48 return abort(400)
49
50 req = get(result.url, headers=c.PROXY_HEADERS_REQUEST)
51 return Response(req.content, headers=c.PROXY_HEADERS_RESPONSE)