]>
git.gir.st - subscriptionfeed.git/blob - app/browse/__init__.py
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
6 from ..common
.common
import *
7 from ..common
.innertube
import *
9 from .protobuf
import make_sp
, make_channel_params
, make_playlist_params
, Filters
11 frontend
= Blueprint('browse', __name__
,
12 template_folder
='templates',
13 static_folder
='static',
14 static_url_path
='/static/ys')
16 @frontend.route('/results')
17 @frontend.route('/search')
19 #token = getattr(current_user, 'token', 'guest')
20 q
= request
.args
.get('q') or request
.args
.get('search_query')
21 continuation
= request
.args
.get('continuation')
24 k
:v
for k
,v
in request
.args
.items()
25 if k
in ['sort','date','type','len']
27 f
for f
in request
.args
.getlist('feature')
28 if f
in Filters
.__dataclass
_fields
__.keys()
30 e
for e
in request
.args
.getlist('feature')
35 yt_results
= fetch_ajax("search", **(
36 {'continuation': continuation
} if continuation
else {'query': q
, 'params': sp
}
39 results
, extras
, continuation
= prepare_searchresults(yt_results
)
46 return render_template('search.html.j2', rows
=results
, query
=q
, continuation
=continuation
)
48 @frontend.route('/channel/<channel_id>/')
49 @frontend.route('/channel/<channel_id>/<subpage>')
50 def channel(channel_id
, subpage
="videos"):
51 token
= getattr(current_user
, 'token', 'guest')
52 if subpage
in ("videos", "streams", "shorts"): # "streams"==livestreams
53 sort_by
= request
.args
.get('sort') or "newest"
55 elif subpage
== "playlists":
56 sort_by
= request
.args
.get('sort', "modified")
58 elif subpage
== "search":
59 query
= request
.args
.get('q')
61 else: # we don't support /home, /about, ..., so redirect to /videos.
62 return redirect(url_for('.channel', channel_id
=channel_id
))
64 # best effort; if it fails, it fails in the redirect.
65 if not re
.match(r
"(UC[A-Za-z0-9_-]{22})", channel_id
):
66 return redirect(url_for('.channel_redirect', user
=channel_id
))
68 # if we don't have a continuation, we create parameters for page 1 manually:
69 continuation
= request
.args
.get('continuation') or \
70 make_channel_params(channel_id
, subpage
, 1, sort_by
, query
, v3
=(subpage
!= "search"))
71 result
= fetch_ajax("browse", continuation
=continuation
)
72 error
= find_and_parse_error(result
)
74 if result
is None: # if fetching from innertube failed, fall back to xmlfeed:
75 flash("unable to fetch results from ajax; displaying fallback results (15 newest)", "error")
76 return fallback_route(channel_id
, subpage
)
79 return error
, 400 # todo: ugly
81 # new seperated videos/livestreams/shorts don't return metadata
82 xmlfeed
= fetch_xml("channel_id", channel_id
)
84 title
, _
, _
, _
, _
= parse_xml(xmlfeed
)
86 _
, descr
, thumb
, rows
, continuation
= prepare_channel(result
, channel_id
, title
)
87 if not rows
: # overran end of list, or is special channel (e.g. music topic (sidebar 'best of youtube', UC-9-kyTW8ZkZNDHQJ6FgpwQ)
88 flash("ajax returned nothing; displaying fallback results (15 newest)", "error")
89 return fallback_route(channel_id
, subpage
)
92 # set pin/hide stati of retrieved videos:
93 video_ids
= [card
['content']['video_id'] for card
in rows
]
94 pinned
, hidden
= fetch_video_flags(token
, video_ids
)
96 {'type':v
['type'], 'content':{**v
['content'], 'pinned': v
['content']['video_id'] in pinned
}}
98 if v
['content']['video_id'] not in hidden
99 ], key
=lambda v
:v
['content']['pinned'], reverse
=True)
101 with sqlite3
.connect(cf
['global']['database']) as conn
:
106 WHERE channel_id = ? AND user = ?
107 """, (channel_id
, token
))
108 (is_subscribed
,) = c
.fetchone()
110 return render_template('channel.html.j2',
115 channel_id
=channel_id
,
118 is_subscribed
=is_subscribed
,
119 continuation
=continuation
)
121 @frontend.route('/user/<user>/')
122 @frontend.route('/user/<user>/<subpage>')
123 @frontend.route('/c/<user>/')
124 @frontend.route('/c/<user>/<subpage>')
125 def channel_redirect(user
, subpage
=None):
127 The browse_ajax 'API' needs the UCID.
130 typ
= request
.path
.split("/")[1] # 'c' or 'user'
132 # inverse of the test in /channel/:
133 if re
.match(r
"(UC[A-Za-z0-9_-]{22})", user
):
134 return redirect(url_for('.channel', channel_id
=user
))
136 channel_id
= canonicalize_channel(user
, typ
)
138 raise NotFound("channel appears to not exist")
140 url_for('.channel', channel_id
=channel_id
, subpage
=subpage
), 308
143 @frontend.route('/playlist')
145 playlist_id
= request
.args
.get('list')
147 raise BadRequest("No playlist ID")
149 # if we don't have a continuation, we create parameters for page 1 manually:
150 continuation
= request
.args
.get('continuation') or \
151 make_playlist_params(playlist_id
, 0)
152 result
= fetch_ajax("browse", continuation
=continuation
)
153 error
= find_and_parse_error(result
)
156 flash(f
"1 {error}. Loading fallback.", 'error')
157 return fallback_route()
159 if not 'continuationContents' in result
:
160 flash(f
"2 {error}. Loading fallback.", 'error')
161 return fallback_route()
163 title
, author
, channel_id
, rows
, continuation
= prepare_playlist(result
)
165 return render_template('playlist.html.j2',
168 channel_id
=channel_id
,
170 continuation
=continuation
)
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.
177 # prevent a lot of false-positives (and reduce youtube api calls)
180 # possible channel names: need to distinguish /name from /@name
181 typ
= "c" if something
[0] != "@" else ""
182 channel_id
= canonicalize_channel(something
, typ
)
184 return redirect(url_for('.channel', channel_id
=channel_id
))
185 elif re
.match(r
"^[-_0-9A-Za-z]{11}$", something
): # looks like a video id
186 return redirect(url_for('youtube.watch', v
=something
, t
=request
.args
.get('t')))
188 raise NotFound("Note: some usernames not recognized; try searching it")
190 @frontend.before_app_request
192 if not 'header_items' in g
:
194 g
.header_items
.append({
196 'url': url_for('browse.search'),
197 'parent': frontend
.name
,