]>
git.gir.st - subscriptionfeed.git/blob - 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
5 from flask
import Blueprint
, render_template
, request
, flash
, g
, url_for
7 from ..common
.common
import *
8 from ..common
.innertube
import *
10 from .protobuf
import make_sp
, make_channel_params
, make_playlist_params
12 frontend
= Blueprint('dangerous', __name__
,
13 template_folder
='templates',
14 static_folder
='static',
15 static_url_path
='/static/ys')
17 @frontend.route('/search')
19 #token = getattr(current_user, 'token', 'guest')
20 q
= request
.args
.get('q')
21 page
= int(request
.args
.get('page', 1))
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
29 yt_results
= fetch_searchresults(q
, page
, sp
)
31 results
, extras
= prepare_searchresults(yt_results
)
38 return render_template('search.html.j2', rows
=results
, query
=q
, page
=page
)
40 # TODO: channels, playlists:
41 # https://github.com/iv-org/invidious/blob/452d1e8307d6344dd51c5437ccd032a566291c34/src/invidious/channels.cr#L399
43 @frontend.route('/channel/<channel_id>/')
44 @frontend.route('/channel/<channel_id>/<subpage>')
45 def channel(channel_id
, subpage
="videos"):
46 #TODO: if anything goes wrong, fall back to xmlfeed
47 if subpage
== "videos":
48 page
= int(request
.args
.get('page', 1))
49 sort_by
= request
.args
.get('sort', "newest")
50 elif subpage
== "playlists":
51 page
= None # TODO: cursor
52 sort_by
= request
.args
.get('sort', "modified")
54 return "not found", 404
56 result
= fetch_ajax(make_channel_params(channel_id
, subpage
, page
, sort_by
))
58 title
, descr
, thumb
, rows
= prepare_channel_items(result
, channel_id
)
59 # TODO: add is_pinned/is_hidden
61 return render_template('channel.html.j2',
65 channel_id
=channel_id
,
72 # TODO: this belongs in common.innertube
73 def prepare_channel_items(result
, channel_id
):
74 response
= listfind(result
,'response')
76 meta1
= response
.get('metadata',{}).get('channelMetadataRenderer',{})
77 meta2
= response
.get('microformat',{}).get('microformatDataRenderer',{})
78 title
= meta1
.get('title', meta2
.get('title'))
79 descr
= meta1
.get('description', meta2
.get('description')) # meta2.description is capped at 160chars
80 thumb
= mkthumbs(meta2
.get('thumbnail',meta1
.get('avatar',{})).get('thumbnails',{})) # .avatar ~ 900px
82 if 'continuationContents' in response
.keys():
83 contents
= response
['continuationContents']
85 items
= parse_channel_items(contents
['gridContinuation']['items'], channel_id
, title
)
88 items
= parse_channel_items(contents
['sectionListContinuation']['contents'], channel_id
, title
)
90 from flask
import current_app
91 current_app
.logger
.error(result
)
93 else: # if absent, we reach end of list
96 return title
, descr
, thumb
, items
98 def parse_channel_items(items
, channel_id
, author
):
101 key
= next(iter(item
.keys()), None)
103 if key
== "gridVideoRenderer" or key
== "videoRenderer":
104 result
.append({'type': 'VIDEO', 'content': {
105 'video_id': content
['videoId'],
106 'title': content
['title']['simpleText'],
108 'channel_id': channel_id
,
109 'length': listfind(content
.get('thumbnailOverlays',[]),'thumbnailOverlayTimeStatusRenderer').get('text',{}).get('simpleText'),
110 'views': toInt(content
.get('viewCountText',{}).get('simpleText')),
111 'published': age(content
.get('publishedTimeText',{}).get('simpleText')),
113 elif key
== "gridPlaylistRenderer" or key
== "playlistRenderer":
114 result
.append({'type': 'PLAYLIST', 'content': {
115 'playlist_id': content
['navigationEndpoint']['watchEndpoint']['playlistId'],
116 'video_id': content
['navigationEndpoint']['watchEndpoint']['videoId'],
117 'title': (content
['title'].get('simpleText') or # playlistRenderer
118 content
['title']['runs'][0]['text']), # gridPlaylistRenderer
120 'channel_id': channel_id
,
121 'n_videos': toInt(content
.get('videoCount') or # playlistRenderer
122 content
.get('videoCountShortText',{}).get('simpleText') or # grid(1)
123 content
.get('videoCountText',{}).get('runs',[{}])[0].get('text')), # grid(2)
125 elif key
== "itemSectionRenderer":
126 result
.extend(parse_channel_items(content
['contents'], channel_id
, author
))
128 raise Exception(item
) # XXX TODO
132 def prepare_playlist(result
):
133 contents
= listfind(result
,'response')['continuationContents']['playlistVideoListContinuation'] \
134 .get('contents',[]) # no .contents if overran end of playlist
135 return list(filter(None, map(parse_playlist
, contents
)))
137 def parse_playlist(item
):
138 key
= next(iter(item
.keys()), None)
140 if key
== "playlistVideoRenderer":
141 if not content
.get('isPlayable', False):
142 return None # private or deleted video
144 return {'type': 'VIDEO', 'content': {
145 'video_id': content
['videoId'],
146 'title': (content
['title'].get('simpleText') or # playable videos
147 content
['title'].get('runs',[{}])[0].get('text')), # "[Private video]"
148 'playlist_id': content
['navigationEndpoint']['watchEndpoint']['playlistId'],
149 'index': content
['navigationEndpoint']['watchEndpoint']['index'], #or int(content['index']['simpleText'])
150 # rest is missing from unplayable videos:
151 'author': content
.get('shortBylineText',{}).get('runs',[{}])[0].get('text'),
152 'channel_id':content
.get('shortBylineText',{}).get('runs',[{}])[0].get('navigationEndpoint',{}).get('browseEndpoint',{}).get('browseId'),
153 'length': (content
.get("lengthText",{}).get("simpleText") or # "8:51"
154 int(content
.get("lengthSeconds", 0))), # "531"
155 'starttime': content
['navigationEndpoint']['watchEndpoint'].get('startTimeSeconds'),
158 raise Exception(item
) # XXX TODO
162 @frontend.route('/playlist')
164 #TODO: if anything goes wrong, fall back to xmlfeed
165 playlist_id
= request
.args
.get('list')
167 return "bad list id", 400 # todo
168 page
= int(request
.args
.get('page', 1))
170 offset
= (page
-1)*100 # each call returns 100 items
171 result
= fetch_ajax(make_playlist_params(playlist_id
, offset
))
173 rows
= prepare_playlist(result
)
175 return render_template('playlist.html.j2',
176 title
="playlist", # XXX: can't get playlist metadata from this, get from xmlfeed!
180 @frontend.before_app_request
182 if not 'header_items' in g
:
184 g
.header_items
.append({
186 'url': url_for('dangerous.search'),
187 'parent': frontend
.name
,