]> git.gir.st - subscriptionfeed.git/blob - app/invidious/__init__.py
add raw video id enpoint; make fallback_route return 404 if none available
[subscriptionfeed.git] / app / invidious / __init__.py
1 import requests
2 from flask import Blueprint, render_template, request, flash
3
4 from ..common.common import fallback_route
5
6 frontend = Blueprint('invidious', __name__,
7 template_folder='templates',
8 static_folder='static',
9 static_url_path='/static/iv')
10
11 # TODO: minimal implementation (invidio.us hardcoded, videos only, no optional_params)
12 @frontend.route('/search')
13 def search():
14 #token = getattr(current_user, 'token', 'guest')
15 q = request.args.get('q')
16 page = int(request.args.get('page', 1))
17 provider = request.args.get('provider', 'invidio.us:443')
18
19 """
20 sort_by: "relevance", "rating", "upload_date", "view_count"
21 date: "hour", "today", "week", "month", "year"
22 duration: "short", "long"
23 type: "video", "playlist", "channel", "all", (default: video)
24 features: "hd", "subtitles", "creative_commons", "3d", "live",
25 "purchased", "4k", "360", "location", "hdr" (comma separated)
26 region: ISO 3166 country code (default: "US")
27 """
28 optional_params = {
29 p: request.args.get(p)
30 for p in ['sort_by', 'date', 'duration', 'type', 'features', 'region']
31 }
32
33 if not q:
34 return "no search query", 400
35
36 r = requests.get(f"https://invidio.us/api/v1/search", {
37 'q': q,
38 'page': page,
39 # **optional_params,
40 })
41 if not r.ok:
42 return "error fetching search results", 502 # TODO: better
43
44 # XXX: should check r.json if it really are search results (if other provider)
45 # XXX: should transform invidious-json to our naming scheme
46 return render_template('search.html.j2', rows=r.json(), query=q, page=page,
47 optional_params=optional_params)
48
49 @frontend.route('/channel/<channel_id>')
50 @frontend.route('/user/<channel_id>')
51 def channel(channel_id):
52 # proxying invidious is way slower, so we don't do it for page 0 (i.e the 15 newest videos we can get from youtube ourselves)
53 if 'page' not in request.args and not str(request.url_rule).startswith("/user/"):
54 return fallback_route(channel_id)
55
56 page = int(request.args.get('page', 1))
57 sort_by = request.args.get('sort_by', 'newest')
58 provider = request.args.get('provider', 'invidio.us:443')
59
60 r = requests.get(f"https://invidio.us/api/v1/channels/{channel_id}/videos", {
61 'sort_by': sort_by,
62 'page': page,
63 }, allow_redirects=False)
64 if not r.ok or r.status_code != 200:
65 flash("invidious did not find the channel or is blocked. showing minimal response", "error")
66 return fallback_route(channel_id)
67
68 return render_template('channel.html.j2', rows=r.json(), sort_by=sort_by, page=page)
69
70 @frontend.route('/playlist')
71 def playlist():
72 # proxying invidious is way slower, so we don't do it for page 0 (i.e the 15 newest videos we can get from youtube ourselves)
73 if 'page' not in request.args:
74 return fallback_route()
75
76 playlist_id = request.args.get('list')
77 if not playlist_id:
78 return "bad list id", 400 # todo
79
80 page = int(request.args.get('page', 1))
81 provider = request.args.get('provider', 'invidio.us:443')
82
83 r = requests.get(f"https://invidio.us/api/v1/playlists/{playlist_id}", {
84 'page': page,
85 }, allow_redirects=False)
86 if not r.ok or r.status_code != 200:
87 flash("invidious did not find the channel or is blocked. showing minimal response", "error")
88 return fallback_route()
89
90 return render_template('channel.html.j2', rows=r.json().get('videos',[]), page=page)
Imprint / Impressum