fxyoutube/views.py (view raw)
1from fxyoutube import app
2from fxyoutube.db import get_video_from_cache, get_info, clear_cache
3import fxyoutube.constants as c
4
5from flask import request, redirect, abort, render_template, Response
6from requests import get
7import re
8
9def main_handler(request, video_id):
10 if video_id == "":
11 return redirect(c.REPO_URL)
12
13 user_agent = request.headers.get("User-Agent", "")
14 result = re.findall(c.UA_REGEX, user_agent, flags=re.I)
15 if len(result) == 0:
16 return redirect(c.BASE_URL + video_id)
17
18 info = get_info(video_id)
19 if info is None:
20 return abort(400)
21
22 return render_template("base.html", info=info, base_url=c.BASE_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 video = request.args.get('v', '')
32 return main_handler(request, video)
33
34@app.route('/', defaults={'video': ''})
35@app.route('/<path:video>')
36def main_route(video):
37 return main_handler(request, video)
38
39@app.route('/proxy/', defaults={'path': ''})
40@app.route('/proxy/<path:path>')
41def proxy(path):
42 result = get_video_from_cache(path)
43
44 if result is None:
45 return abort(400)
46
47 if result["video_ext"] is None:
48 return abort(400)
49
50 req = get(result["url"])
51 return Response(req.content, headers={ "Content-Type": "video/" + result["video_ext"] })