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