]> git.gir.st - subscriptionfeed.git/blob - app/dangerous/__init__.py
clean up v1/v2 channel protobuf handling
[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, redirect
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 if subpage == "videos":
47 page = int(request.args.get('page', 1))
48 sort_by = request.args.get('sort', "newest")
49 query = None
50 elif subpage == "playlists":
51 page = None # TODO: cursor
52 sort_by = request.args.get('sort', "modified")
53 query = None
54 elif subpage == "search":
55 query = request.args.get('q')
56 page = int(request.args.get('page', 1))
57 sort_by = None
58 else: # we don't support /home, /about, ..., so redirect to /videos.
59 return redirect(url_for('.channel', channel_id=channel_id))
60
61 result = fetch_ajax(make_channel_params(channel_id, subpage, page, sort_by, query))
62
63 # Note: as of 2020-08-15, using the v1 format sometimes returns an error. if that's the case, try v2.
64 alert = listget(listget(result,1,{}).get('response',{}).get('alerts',[]),0,{}).get('alertRenderer',{})
65 if alert.get('type','') == "ERROR": # alert['text']['simpleText'] == "Unknown error."
66 result = fetch_ajax(make_channel_params(channel_id, subpage, page, sort_by, query, v2=True))
67
68 title, descr, thumb, rows, more = prepare_channel(result, channel_id)
69 # TODO: add is_pinned/is_hidden
70
71 if title is None: # if both v1 and v2 failed, fall back to xmlfeed:
72 flash("unable to fetch results from ajax; displaying fallback results (15 newest)", "error")
73 return fallback_route(channel_id, subpage)
74
75 return render_template('channel.html.j2',
76 title=title,
77 subpage=subpage,
78 rows=rows,
79 channel_id=channel_id,
80 channel_img=thumb,
81 channel_desc=descr,
82 page=page,
83 has_more=more)
84
85 @frontend.route('/user/<user>')
86 @frontend.route('/user/<user>/<subpage>')
87 @frontend.route('/c/<user>')
88 @frontend.route('/c/<user>/<subpage>')
89 def channel_redirect(user, subpage=None):
90 """
91 The browse_ajax 'API' needs the UCID. We can get that from the RSS feeds.
92 """
93 xmlfeed = fetch_xml("user", user)
94 _, _, _, channel_id, _ = parse_xml(xmlfeed)
95 return redirect(
96 url_for('.channel', channel_id=channel_id, subpage=subpage), 308
97 )
98
99 @frontend.route('/playlist')
100 def playlist():
101 #TODO: if anything goes wrong, fall back to xmlfeed
102 playlist_id = request.args.get('list')
103 if not playlist_id:
104 return "bad list id", 400 # todo
105 page = int(request.args.get('page', 1))
106
107 xmlfeed = fetch_xml("playlist_id", playlist_id)
108 if not xmlfeed:
109 return "not found or something", 404 # XXX
110 title, author, _, channel_id, _ = parse_xml(xmlfeed)
111
112 offset = (page-1)*100 # each call returns 100 items
113 result = fetch_ajax(make_playlist_params(playlist_id, offset))
114
115 rows, more = prepare_playlist(result)
116
117 return render_template('playlist.html.j2',
118 title=title,
119 author=author,
120 channel_id=channel_id,
121 rows=rows,
122 page=page,
123 has_more=more)
124
125 @frontend.before_app_request
126 def inject_button():
127 if not 'header_items' in g:
128 g.header_items = []
129 g.header_items.append({
130 'name': 'search',
131 'url': url_for('dangerous.search'),
132 'parent': frontend.name,
133 'priority': 15,
134 })
Imprint / Impressum