all repos — FixYouTube-legacy @ 2ea1ba90fb06ebe7f2a993e27e9320f149ccc4b1

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
 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 render_template("index.html", repo_url=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    try:
45        if result.url == "":
46            raise AttributeError
47    except AttributeError:
48        return abort(400)
49    
50    return Response(get(result.url).content, headers=c.PROXY_HEADERS)