]> git.gir.st - subscriptionfeed.git/blob - app/browse/__init__.py
add browse/ override for /<something> endpoint
[subscriptionfeed.git] / app / browse / __init__.py
1 import requests
2 from flask import Blueprint, render_template, request, flash, g, url_for, redirect
3 from flask_login import current_user
4 from werkzeug.exceptions import BadRequest, NotFound
5
6 from ..common.common import *
7 from ..common.innertube import *
8 from .lib import *
9 from .protobuf import make_sp, make_channel_params, make_playlist_params
10
11 frontend = Blueprint('browse', __name__,
12 template_folder='templates',
13 static_folder='static',
14 static_url_path='/static/ys')
15
16 @frontend.route('/results')
17 @frontend.route('/search')
18 def search():
19 #token = getattr(current_user, 'token', 'guest')
20 q = request.args.get('q') or request.args.get('search_query')
21 page = int(request.args.get('page') or 1)
22
23 sp = make_sp(page, **{
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, 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 token = getattr(current_user, 'token', 'guest')
47 if subpage == "videos":
48 page = int(request.args.get('page', 1))
49 sort_by = request.args.get('sort') or "newest"
50 query = None
51 elif subpage == "playlists":
52 page = request.args.get('cursor')
53 sort_by = request.args.get('sort', "modified")
54 query = None
55 elif subpage == "search":
56 query = request.args.get('q')
57 page = int(request.args.get('page', 1))
58 sort_by = None
59 else: # we don't support /home, /about, ..., so redirect to /videos.
60 return redirect(url_for('.channel', channel_id=channel_id))
61
62 # best effort; if it fails, it fails in the redirect.
63 if not re.match(r"(UC[A-Za-z0-9_-]{22})", channel_id):
64 return redirect(url_for('.channel_redirect', user=channel_id))
65
66 result = fetch_ajax(make_channel_params(channel_id, subpage, page, sort_by, query, v3=(subpage == "videos")))
67
68 title, descr, thumb, rows, more = prepare_channel(result, channel_id)
69
70 if title is None or not rows: # if fetching from innertube failed, fall back to xmlfeed:
71 flash("unable to fetch results from ajax; displaying fallback results (15 newest)", "error")
72 return fallback_route(channel_id, subpage)
73
74 # set pin/hide stati of retrieved videos:
75 video_ids = [card['content']['video_id'] for card in rows]
76 pinned, hidden = fetch_video_flags(token, video_ids)
77 rows = sorted([
78 {'type':v['type'], 'content':{**v['content'], 'pinned': v['content']['video_id'] in pinned}}
79 for v in rows
80 if v['content']['video_id'] not in hidden
81 ], key=lambda v:v['content']['pinned'], reverse=True)
82
83 with sqlite3.connect(cf['global']['database']) as conn:
84 c = conn.cursor()
85 c.execute("""
86 SELECT COUNT(*)
87 FROM subscriptions
88 WHERE channel_id = ? AND user = ?
89 """, (channel_id, token))
90 (is_subscribed,) = c.fetchone()
91
92 # XXX: ad-hoc protobuf ctoken extractor: This is one big hack that needs to
93 # get cleanded up. Since the ctoken only has fileds with rigid lengths, we
94 # can just extract the bytes at the correct position to get at the cursor
95 # token we need.
96 if subpage == "playlists":
97 page = result[1].get('response',{}).get('continuationContents',{}) \
98 .get('sectionListContinuation',{}).get('continuations',[{}])[0] \
99 .get('nextContinuationData',{}).get('continuation')
100 if page:
101 import base64
102 page = base64.urlsafe_b64decode(page+u'===')[34:]
103 page = base64.urlsafe_b64decode(page+b'===')[21:]
104 page = page[:102].decode('ascii')
105 # end XXX
106 return render_template('channel.html.j2',
107 title=title,
108 subpage=subpage,
109 rows=rows,
110 channel_id=channel_id,
111 channel_img=thumb,
112 channel_desc=descr,
113 is_subscribed=is_subscribed,
114 page=page,
115 has_more=more)
116
117 @frontend.route('/user/<user>/')
118 @frontend.route('/user/<user>/<subpage>')
119 @frontend.route('/c/<user>/')
120 @frontend.route('/c/<user>/<subpage>')
121 def channel_redirect(user, subpage=None):
122 """
123 The browse_ajax 'API' needs the UCID.
124 """
125
126 # inverse of the test in /channel/:
127 if re.match(r"(UC[A-Za-z0-9_-]{22})", user):
128 return redirect(url_for('.channel', channel_id=user))
129
130 channel_id = canonicalize_channel(user)
131 if not channel_id:
132 raise NotFound("channel appears to not exist")
133 return redirect(
134 url_for('.channel', channel_id=channel_id, subpage=subpage), 308
135 )
136
137 @frontend.route('/playlist')
138 def playlist():
139 #TODO: if anything goes wrong, fall back to xmlfeed
140 playlist_id = request.args.get('list')
141 if not playlist_id:
142 raise BadRequest("No playlist ID")
143 page = int(request.args.get('page', 1))
144
145 xmlfeed = fetch_xml("playlist_id", playlist_id)
146 if not xmlfeed:
147 raise NotFound("Unable to fetch playlist")
148 title, author, _, channel_id, _ = parse_xml(xmlfeed)
149
150 offset = (page-1)*100 # each call returns 100 items
151 result = fetch_ajax(make_playlist_params(playlist_id, offset))
152
153 if not 'continuationContents' in result[1]['response']: # XXX: this needs cleanup!
154 # code:"CONDITION_NOT_MET", debugInfo:"list type not viewable"
155 # on playlist https://www.youtube.com/watch?v=6y_NJg-xoeE&list=RDgohHV9ryp-A&index=24 (not openable on yt.com)
156 error = result[1]['response']['responseContext']['errors']['error'][0]
157 flash(f"{error['code']}: {error['debugInfo'] or error['externalErrorMessage']}", 'error')
158 return fallback_route()
159 rows, more = prepare_playlist(result)
160
161 return render_template('playlist.html.j2',
162 title=title,
163 author=author,
164 channel_id=channel_id,
165 rows=rows,
166 page=page,
167 has_more=more)
168
169 @frontend.route('/<something>', strict_slashes=False)
170 def plain_user_or_video(something):
171 # this is a near-copy of the same route in app/youtube, but using a
172 # different, more reliable endpoint to determine whether a channel exists.
173 if '.' in something:
174 # prevent a lot of false-positives (and reduce youtube api calls)
175 raise NotFound
176
177 channel_id = canonicalize_channel(something)
178 if channel_id:
179 return redirect(url_for('.channel', channel_id=channel_id))
180 elif re.match(r"^[-_0-9A-Za-z]{11}$", something): # looks like a video id
181 return redirect(url_for('youtube.watch', v=something, t=request.args.get('t')))
182 else: # ¯\_(ツ)_/¯
183 raise NotFound("Note: some usernames not recognized; try searching it")
184
185 @frontend.before_app_request
186 def inject_button():
187 if not 'header_items' in g:
188 g.header_items = []
189 g.header_items.append({
190 'name': 'search',
191 'url': url_for('browse.search'),
192 'parent': frontend.name,
193 'priority': 15,
194 })
Imprint / Impressum