]> git.gir.st - subscriptionfeed.git/blob - app/dangerous/__init__.py
search results: fix channel without subscriber count, add spelling info
[subscriptionfeed.git] / 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
4 import requests
5 from flask import Blueprint, render_template, request, flash, g, url_for
6
7 from ..common.common import *
8 from ..common.innertube import *
9 from .lib import *
10 from .protobuf import make_sp, make_channel_params, make_playlist_params
11
12 frontend = Blueprint('dangerous', __name__,
13 template_folder='templates',
14 static_folder='static',
15 static_url_path='/static/ys')
16
17 @frontend.route('/search')
18 def search():
19 #token = getattr(current_user, 'token', 'guest')
20 q = request.args.get('q')
21 page = int(request.args.get('page', 1))
22
23 sp = make_sp(**{
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, page, 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 #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")
53 else:
54 return "not found", 404
55
56 result = fetch_ajax(make_channel_params(channel_id, subpage, page, sort_by))
57
58 title, descr, thumb, rows = prepare_channel_items(result, channel_id)
59 # TODO: add is_pinned/is_hidden
60
61 return render_template('channel.html.j2',
62 title=title,
63 subpage=subpage,
64 rows=rows,
65 channel_id=channel_id,
66 channel_img=thumb,
67 channel_desc=descr,
68 page=page)
69
70 ############
71
72 # TODO: this belongs in common.innertube
73 def prepare_channel_items(result, channel_id):
74 response = listfind(result,'response')
75
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
81
82 if 'continuationContents' in response.keys():
83 contents = response['continuationContents']
84 try: # TODO: cleanup
85 items = parse_channel_items(contents['gridContinuation']['items'], channel_id, title)
86 except:
87 try:
88 items = parse_channel_items(contents['sectionListContinuation']['contents'], channel_id, title)
89 except:
90 from flask import current_app
91 current_app.logger.error(result)
92 items = []
93 else: # if absent, we reach end of list
94 items = []
95
96 return title, descr, thumb, items
97
98 def parse_channel_items(items, channel_id, author):
99 result = []
100 for item in items:
101 key = next(iter(item.keys()), None)
102 content = item[key]
103 if key == "gridVideoRenderer" or key == "videoRenderer":
104 result.append({'type': 'VIDEO', 'content': {
105 'video_id': content['videoId'],
106 'title': content['title']['simpleText'],
107 'author': author,
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')),
112 }})
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
119 'author': author,
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)
124 }})
125 elif key == "itemSectionRenderer":
126 result.extend(parse_channel_items(content['contents'], channel_id, author))
127 else:
128 raise Exception(item) # XXX TODO
129
130 return result
131
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)))
136
137 def parse_playlist(item):
138 key = next(iter(item.keys()), None)
139 content = item[key]
140 if key == "playlistVideoRenderer":
141 if not content.get('isPlayable', False):
142 return None # private or deleted video
143
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'),
156 }}
157 else:
158 raise Exception(item) # XXX TODO
159
160 ############
161
162 @frontend.route('/playlist')
163 def playlist():
164 #TODO: if anything goes wrong, fall back to xmlfeed
165 playlist_id = request.args.get('list')
166 if not playlist_id:
167 return "bad list id", 400 # todo
168 page = int(request.args.get('page', 1))
169
170 offset = (page-1)*100 # each call returns 100 items
171 result = fetch_ajax(make_playlist_params(playlist_id, offset))
172
173 rows = prepare_playlist(result)
174
175 return render_template('playlist.html.j2',
176 title="playlist", # XXX: can't get playlist metadata from this, get from xmlfeed!
177 rows=rows,
178 page=page)
179
180 @frontend.before_app_request
181 def inject_button():
182 if not 'header_items' in g:
183 g.header_items = []
184 g.header_items.append({
185 'name': 'search',
186 'url': url_for('dangerous.search'),
187 'parent': frontend.name,
188 'priority': 15,
189 })
Imprint / Impressum