]>
git.gir.st - subscriptionfeed.git/blob - app/browse/__init__.py
3 from flask
import Blueprint
, render_template
, request
, flash
, g
, url_for
, redirect
4 from flask_login
import current_user
5 from werkzeug
.exceptions
import BadRequest
, NotFound
7 from ..common
.common
import *
9 from .innertube
import prepare_searchresults
, prepare_channel
, prepare_playlist
10 from .protobuf
import make_sp
, make_channel_params
, make_playlist_params
, Filters
12 frontend
= Blueprint('browse', __name__
,
13 template_folder
='templates',
14 static_folder
='static',
15 static_url_path
='/static/ys')
17 @frontend.route('/results')
18 @frontend.route('/search')
20 #token = getattr(current_user, 'token', 'guest')
21 q
= request
.args
.get('q') or request
.args
.get('search_query')
22 continuation
= request
.args
.get('continuation')
25 k
:v
for k
,v
in request
.args
.items()
26 if k
in ['sort','date','type','len']
28 f
for f
in request
.args
.getlist('feature')
29 if f
in Filters
.__dataclass
_fields
__.keys()
31 e
for e
in request
.args
.getlist('feature')
36 yt_results
= fetch_ajax("search", **(
37 {'continuation': continuation
} if continuation
else {'query': q
, 'params': sp
}
40 results
, extras
, continuation
= prepare_searchresults(yt_results
)
47 return render_template('search.html.j2', rows
=results
, query
=q
, continuation
=continuation
)
49 @frontend.route('/channel/<channel_id>/')
50 @frontend.route('/channel/<channel_id>/<subpage>')
51 def channel(channel_id
, subpage
="videos"):
52 token
= getattr(current_user
, 'token', 'guest')
53 if subpage
in ("videos", "streams", "shorts"): # "streams"==livestreams
54 sort_by
= request
.args
.get('sort') or "newest"
56 elif subpage
== "playlists":
57 sort_by
= request
.args
.get('sort', "modified")
59 elif subpage
== "search":
60 query
= request
.args
.get('q')
62 else: # we don't support /home, /about, ..., so redirect to /videos.
63 return redirect(url_for('.channel', channel_id
=channel_id
))
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
))
69 # if we don't have a continuation, we create parameters for page 1 manually:
70 continuation
= request
.args
.get('continuation') or \
71 make_channel_params(channel_id
, subpage
, 1, sort_by
, query
, v3
=(subpage
!= "search"))
72 result
= fetch_ajax("browse", continuation
=continuation
)
73 error
= find_and_parse_error(result
)
75 if result
is None: # if fetching from innertube failed, fall back to xmlfeed:
76 flash("unable to fetch results from ajax; displaying fallback results (15 newest)", "error")
77 return fallback_route(channel_id
, subpage
)
80 return error
, 400 # todo: ugly
82 # new seperated videos/livestreams/shorts don't return metadata
83 xmlfeed
= fetch_xml("channel_id", channel_id
)
85 title
, _
, _
, _
, _
= parse_xml(xmlfeed
)
87 _
, descr
, thumb
, rows
, continuation
= prepare_channel(result
, channel_id
, title
)
88 if not rows
: # overran end of list, or is special channel (e.g. music topic (sidebar 'best of youtube', UC-9-kyTW8ZkZNDHQJ6FgpwQ)
89 flash("ajax returned nothing; displaying fallback results (15 newest)", "error")
90 return fallback_route(channel_id
, subpage
)
93 # set pin/hide stati of retrieved videos:
94 video_ids
= [card
['content']['video_id'] for card
in rows
]
95 pinned
, hidden
= fetch_video_flags(token
, video_ids
)
97 {'type':v
['type'], 'content':{**v
['content'], 'pinned': v
['content']['video_id'] in pinned
}}
99 if v
['content']['video_id'] not in hidden
100 ], key
=lambda v
:v
['content']['pinned'], reverse
=True)
102 with sqlite3
.connect(cf
['global']['database']) as conn
:
107 WHERE channel_id = ? AND user = ?
108 """, (channel_id
, token
))
109 (is_subscribed
,) = c
.fetchone()
111 return render_template('channel.html.j2',
116 channel_id
=channel_id
,
119 is_subscribed
=is_subscribed
,
120 continuation
=continuation
)
122 @frontend.route('/<user>/<subpage>')
123 @frontend.route('/user/<user>/')
124 @frontend.route('/user/<user>/<subpage>')
125 @frontend.route('/c/<user>/')
126 @frontend.route('/c/<user>/<subpage>')
127 def channel_redirect(user
, subpage
=None):
129 The browse_ajax 'API' needs the UCID.
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 if subpage
not in (None, "home", "videos", "shorts", "streams", "playlists", "community", "channels", "about"):
137 raise NotFound("not a valid channel subpage")
139 channel_id
= canonicalize_channel(request
.path
)
141 raise NotFound("channel does not exist")
144 url_for('.channel', channel_id
=channel_id
, subpage
=subpage
), 308
147 @frontend.route('/playlist')
149 playlist_id
= request
.args
.get('list')
151 raise BadRequest("No playlist ID")
153 # if we don't have a continuation, we create parameters for page 1 manually:
154 continuation
= request
.args
.get('continuation') or \
155 make_playlist_params(playlist_id
, 0)
156 result
= fetch_ajax("browse", continuation
=continuation
)
157 error
= find_and_parse_error(result
)
160 flash(f
"1 {error}. Loading fallback.", 'error')
161 return fallback_route()
163 if not 'continuationContents' in result
:
164 flash(f
"2 {error}. Loading fallback.", 'error')
165 return fallback_route()
167 title
, author
, channel_id
, rows
, continuation
= prepare_playlist(result
)
169 return render_template('playlist.html.j2',
172 channel_id
=channel_id
,
174 continuation
=continuation
)
176 @frontend.route('/<something>', strict_slashes
=False)
177 def plain_user_or_video(something
):
178 # this is a near-copy of the same route in app/youtube, but using a
179 # different, more reliable endpoint to determine whether a channel exists.
181 # prevent a lot of false-positives (and reduce youtube api calls)
184 channel_id
= canonicalize_channel(something
) # /vanity or /@handle
186 return redirect(url_for('.channel', channel_id
=channel_id
))
187 elif re
.match(r
"^[-_0-9A-Za-z]{11}$", something
): # looks like a video id
188 return redirect(url_for('youtube.watch', v
=something
, t
=request
.args
.get('t')))
190 raise NotFound("Note: some usernames not recognized; try searching it")
192 @frontend.before_app_request
194 if not 'header_items' in g
:
196 g
.header_items
.append({
198 'url': url_for('browse.search'),
199 'parent': frontend
.name
,