all repos — FixYouTube-legacy @ a13a971a88c1b0db81a1df2db381ac58a5ce94e0

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, 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 abort(400)
12    user_agent = request.headers.get("User-Agent", "")
13    result = re.findall(c.UA_REGEX, user_agent, flags=re.I)
14
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(path)
43    try:
44        url = result[0][9]
45        ext = result[0][6]
46    except IndexError:
47        return abort(400)
48    
49    if ext is None:
50        return abort(400)
51    
52    result = get(url)
53    return Response(result.content, headers={ "Content-Type": "video/" + ext })