]> git.gir.st - subscriptionfeed.git/blob - app/dangerous/__init__.py
add has_more, cleanup, test for error-alerts
[subscriptionfeed.git] / app / dangerous / __init__.py
1 # this is an alternative to proxying through invidious. the search endpoint has only been (loosely) tested by
2 #17:50 < perflyst[m]> appears to be working
3 #, so i hopeā„¢ this works. if not, that's why it's in the 'dangerous' blueprint
4 import requests
5 from flask import Blueprint, render_template, request, flash, g, url_for
6
7 from ..common.common import *
8 from ..common.innertube import *
9 from .lib import *
10 from .protobuf import make_sp, make_channel_params, make_playlist_params
11
12 frontend = Blueprint('dangerous', __name__,
13 template_folder='templates',
14 static_folder='static',
15 static_url_path='/static/ys')
16
17 @frontend.route('/search')
18 def search():
19 #token = getattr(current_user, 'token', 'guest')
20 q = request.args.get('q')
21 page = int(request.args.get('page', 1))
22
23 sp = make_sp(**{
24 k:v for k,v in request.args.items()
25 if k in ['sort','date','type','len']
26 }, extras=['dont_fix_spelling']*0) # extras disabled
27
28 if q:
29 yt_results = fetch_searchresults(q, page, sp)
30
31 results, extras = prepare_searchresults(yt_results)
32
33 for extra in extras:
34 flash(extra, 'info')
35 else:
36 results = None
37
38 return render_template('search.html.j2', rows=results, query=q, page=page)
39
40 # TODO: channels, playlists:
41 # https://github.com/iv-org/invidious/blob/452d1e8307d6344dd51c5437ccd032a566291c34/src/invidious/channels.cr#L399
42
43 @frontend.route('/channel/<channel_id>/')
44 @frontend.route('/channel/<channel_id>/<subpage>')
45 def channel(channel_id, subpage="videos"):
46 #TODO: if anything goes wrong, fall back to xmlfeed
47 if subpage == "videos":
48 page = int(request.args.get('page', 1))
49 sort_by = request.args.get('sort', "newest")
50 elif subpage == "playlists":
51 page = None # TODO: cursor
52 sort_by = request.args.get('sort', "modified")
53 else:
54 return "not found", 404
55
56 result = fetch_ajax(make_channel_params(channel_id, subpage, page, sort_by))
57
58 title, descr, thumb, rows, more = prepare_channel(result, channel_id)
59 # TODO: add is_pinned/is_hidden
60
61 return render_template('channel.html.j2',
62 title=title,
63 subpage=subpage,
64 rows=rows,
65 channel_id=channel_id,
66 channel_img=thumb,
67 channel_desc=descr,
68 page=page,
69 has_more=more)
70
71 @frontend.route('/playlist')
72 def playlist():
73 #TODO: if anything goes wrong, fall back to xmlfeed
74 playlist_id = request.args.get('list')
75 if not playlist_id:
76 return "bad list id", 400 # todo
77 page = int(request.args.get('page', 1))
78
79 offset = (page-1)*100 # each call returns 100 items
80 result = fetch_ajax(make_playlist_params(playlist_id, offset))
81
82 rows = prepare_playlist(result)
83
84 return render_template('playlist.html.j2',
85 title="playlist", # XXX: can't get playlist metadata from this, get from xmlfeed!
86 rows=rows,
87 page=page)
88
89 @frontend.before_app_request
90 def inject_button():
91 if not 'header_items' in g:
92 g.header_items = []
93 g.header_items.append({
94 'name': 'search',
95 'url': url_for('dangerous.search'),
96 'parent': frontend.name,
97 'priority': 15,
98 })
Imprint / Impressum