all repos — FixYouTube-legacy @ 942e0d577869657273327c2480cf05eb5b6758f7

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

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    url = result["url"]
48    if url is None:
49        return abort(400)
50
51    return Response(get(url).content, headers=c.PROXY_HEADERS)