all repos — FixYouTube-legacy @ f947f56687b7c6f4280a9e90832d1ff4874d1973

A better way to embed YouTube videos everywhere (inspired by FixTweet).

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    try:
48        if result.url == "":
49            raise AttributeError
50    except AttributeError:
51        return abort(400)
52    
53    return Response(get(result.url).content, headers=c.PROXY_HEADERS)