]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/trovo.py
[cleanup, utils] Don't use kwargs for `format_field`
[yt-dlp.git] / yt_dlp / extractor / trovo.py
CommitLineData
974208e1 1import itertools
a820dc72 2import json
6ef5ad9e 3import random
4import string
a820dc72
RA
5
6from .common import InfoExtractor
7from ..utils import (
8 ExtractorError,
e0ddbd02 9 format_field,
a820dc72
RA
10 int_or_none,
11 str_or_none,
12 try_get,
13)
14
15
16class TrovoBaseIE(InfoExtractor):
17 _VALID_URL_BASE = r'https?://(?:www\.)?trovo\.live/'
36147a63 18 _HEADERS = {'Origin': 'https://trovo.live'}
a820dc72 19
6ef5ad9e 20 def _call_api(self, video_id, data):
21 if 'persistedQuery' in data.get('extensions', {}):
22 url = 'https://gql.trovo.live'
23 else:
24 url = 'https://api-web.trovo.live/graphql'
25
26 resp = self._download_json(
27 url, video_id, data=json.dumps([data]).encode(), headers={'Accept': 'application/json'},
28 query={
29 'qid': ''.join(random.choices(string.ascii_uppercase + string.digits, k=10)),
30 })[0]
31 if 'errors' in resp:
32 raise ExtractorError(f'Trovo said: {resp["errors"][0]["message"]}')
33 return resp['data'][data['operationName']]
0cbed930 34
a820dc72
RA
35 def _extract_streamer_info(self, data):
36 streamer_info = data.get('streamerInfo') or {}
37 username = streamer_info.get('userName')
38 return {
39 'uploader': streamer_info.get('nickName'),
40 'uploader_id': str_or_none(streamer_info.get('uid')),
a70635b8 41 'uploader_url': format_field(username, None, 'https://trovo.live/%s'),
a820dc72
RA
42 }
43
44
45class TrovoIE(TrovoBaseIE):
46 _VALID_URL = TrovoBaseIE._VALID_URL_BASE + r'(?!(?:clip|video)/)(?P<id>[^/?&#]+)'
47
48 def _real_extract(self, url):
49 username = self._match_id(url)
6ef5ad9e 50 live_info = self._call_api(username, data={
51 'operationName': 'live_LiveReaderService_GetLiveInfo',
52 'variables': {
53 'params': {
54 'userName': username,
55 },
56 },
57 })
a820dc72
RA
58 if live_info.get('isLive') == 0:
59 raise ExtractorError('%s is offline' % username, expected=True)
60 program_info = live_info['programInfo']
61 program_id = program_info['id']
39ca3b5c 62 title = program_info['title']
a820dc72
RA
63
64 formats = []
65 for stream_info in (program_info.get('streamInfo') or []):
66 play_url = stream_info.get('playUrl')
67 if not play_url:
68 continue
69 format_id = stream_info.get('desc')
70 formats.append({
71 'format_id': format_id,
72 'height': int_or_none(format_id[:-1]) if format_id else None,
73 'url': play_url,
36147a63 74 'http_headers': self._HEADERS,
a820dc72
RA
75 })
76 self._sort_formats(formats)
77
78 info = {
79 'id': program_id,
80 'title': title,
81 'formats': formats,
82 'thumbnail': program_info.get('coverUrl'),
83 'is_live': True,
84 }
85 info.update(self._extract_streamer_info(live_info))
86 return info
87
88
89class TrovoVodIE(TrovoBaseIE):
90 _VALID_URL = TrovoBaseIE._VALID_URL_BASE + r'(?:clip|video)/(?P<id>[^/?&#]+)'
91 _TESTS = [{
6ef5ad9e 92 'url': 'https://trovo.live/clip/lc-5285890818705062210?ltab=videos',
93 'params': {'getcomments': True},
a820dc72 94 'info_dict': {
6ef5ad9e 95 'id': 'lc-5285890818705062210',
a820dc72 96 'ext': 'mp4',
6ef5ad9e 97 'title': 'fatal moaning for a super good🤣🤣',
98 'uploader': 'OneTappedYou',
99 'timestamp': 1621628019,
100 'upload_date': '20210521',
101 'uploader_id': '100719456',
102 'duration': 31,
a820dc72
RA
103 'view_count': int,
104 'like_count': int,
105 'comment_count': int,
6ef5ad9e 106 'comments': 'mincount:1',
107 'categories': ['Call of Duty: Mobile'],
108 'uploader_url': 'https://trovo.live/OneTappedYou',
109 'thumbnail': r're:^https?://.*\.jpg',
a820dc72
RA
110 },
111 }, {
6ef5ad9e 112 'url': 'https://trovo.live/video/ltv-100095501_100095501_1609596043',
a820dc72
RA
113 'only_matching': True,
114 }]
115
116 def _real_extract(self, url):
117 vid = self._match_id(url)
6ef5ad9e 118
119 # NOTE: It is also possible to extract this info from the Nuxt data on the website,
120 # however that seems unreliable - sometimes it randomly doesn't return the data,
121 # at least when using a non-residential IP.
122 resp = self._call_api(vid, data={
123 'operationName': 'batchGetVodDetailInfo',
124 'variables': {
125 'params': {
126 'vids': [vid],
127 },
128 },
129 'extensions': {
130 'persistedQuery': {
131 'version': 1,
132 'sha256Hash': 'ceae0355d66476e21a1dd8e8af9f68de95b4019da2cda8b177c9a2255dad31d0',
133 },
134 },
135 })
136 vod_detail_info = resp['VodDetailInfos'][vid]
a820dc72
RA
137 vod_info = vod_detail_info['vodInfo']
138 title = vod_info['title']
139
6ef5ad9e 140 if try_get(vod_info, lambda x: x['playbackRights']['playbackRights'] != 'Normal'):
141 playback_rights_setting = vod_info['playbackRights']['playbackRightsSetting']
142 if playback_rights_setting == 'SubscriberOnly':
143 raise ExtractorError('This video is only available for subscribers', expected=True)
144 else:
145 raise ExtractorError(f'This video is not available ({playback_rights_setting})', expected=True)
146
a820dc72
RA
147 language = vod_info.get('languageName')
148 formats = []
149 for play_info in (vod_info.get('playInfos') or []):
150 play_url = play_info.get('playUrl')
151 if not play_url:
152 continue
153 format_id = play_info.get('desc')
154 formats.append({
155 'ext': 'mp4',
156 'filesize': int_or_none(play_info.get('fileSize')),
157 'format_id': format_id,
158 'height': int_or_none(format_id[:-1]) if format_id else None,
159 'language': language,
160 'protocol': 'm3u8_native',
161 'tbr': int_or_none(play_info.get('bitrate')),
162 'url': play_url,
36147a63 163 'http_headers': self._HEADERS,
a820dc72
RA
164 })
165 self._sort_formats(formats)
166
167 category = vod_info.get('categoryName')
168 get_count = lambda x: int_or_none(vod_info.get(x + 'Num'))
169
a820dc72
RA
170 info = {
171 'id': vid,
172 'title': title,
173 'formats': formats,
174 'thumbnail': vod_info.get('coverUrl'),
175 'timestamp': int_or_none(vod_info.get('publishTs')),
176 'duration': int_or_none(vod_info.get('duration')),
177 'view_count': get_count('watch'),
178 'like_count': get_count('like'),
179 'comment_count': get_count('comment'),
a820dc72 180 'categories': [category] if category else None,
6ef5ad9e 181 '__post_extractor': self.extract_comments(vid),
a820dc72
RA
182 }
183 info.update(self._extract_streamer_info(vod_detail_info))
184 return info
974208e1 185
6ef5ad9e 186 def _get_comments(self, vid):
187 for page in itertools.count(1):
188 comments_json = self._call_api(vid, data={
189 'operationName': 'getCommentList',
190 'variables': {
191 'params': {
192 'appInfo': {
193 'postID': vid,
194 },
195 'preview': {},
196 'pageSize': 99,
197 'page': page,
198 },
199 },
200 'extensions': {
201 'persistedQuery': {
202 'version': 1,
203 'sha256Hash': 'be8e5f9522ddac7f7c604c0d284fd22481813263580849926c4c66fb767eed25',
204 },
205 },
206 })
207 for comment in comments_json['commentList']:
208 content = comment.get('content')
209 if not content:
210 continue
211 author = comment.get('author') or {}
212 parent = comment.get('parentID')
213 yield {
214 'author': author.get('nickName'),
215 'author_id': str_or_none(author.get('uid')),
216 'id': str_or_none(comment.get('commentID')),
217 'text': content,
218 'timestamp': int_or_none(comment.get('createdAt')),
219 'parent': 'root' if parent == 0 else str_or_none(parent),
220 }
221
222 if comments_json['lastPage']:
223 break
224
974208e1 225
3262f8ab 226class TrovoChannelBaseIE(TrovoBaseIE):
974208e1
AG
227 def _get_vod_json(self, page, uid):
228 raise NotImplementedError('This method must be implemented by subclasses')
229
230 def _entries(self, uid):
231 for page in itertools.count(1):
232 vod_json = self._get_vod_json(page, uid)
233 vods = vod_json.get('vodInfos', [])
234 for vod in vods:
235 yield self.url_result(
236 'https://trovo.live/%s/%s' % (self._TYPE, vod.get('vid')),
237 ie=TrovoVodIE.ie_key())
238 has_more = vod_json['hasMore']
239 if not has_more:
240 break
241
242 def _real_extract(self, url):
243 id = self._match_id(url)
6ef5ad9e 244 live_info = self._call_api(id, data={
245 'operationName': 'live_LiveReaderService_GetLiveInfo',
246 'variables': {
247 'params': {
248 'userName': id,
249 },
250 },
251 })
252 uid = str(live_info['streamerInfo']['uid'])
974208e1
AG
253 return self.playlist_result(self._entries(uid), playlist_id=uid)
254
255
256class TrovoChannelVodIE(TrovoChannelBaseIE):
257 _VALID_URL = r'trovovod:(?P<id>[^\s]+)'
96565c7e 258 IE_DESC = 'All VODs of a trovo.live channel; "trovovod:" prefix'
974208e1
AG
259
260 _TESTS = [{
261 'url': 'trovovod:OneTappedYou',
262 'playlist_mincount': 24,
263 'info_dict': {
264 'id': '100719456',
265 },
266 }]
267
974208e1
AG
268 _TYPE = 'video'
269
270 def _get_vod_json(self, page, uid):
6ef5ad9e 271 return self._call_api(uid, data={
272 'operationName': 'getChannelLtvVideoInfos',
273 'variables': {
274 'params': {
275 'channelID': int(uid),
276 'pageSize': 99,
277 'currPage': page,
278 },
279 },
280 'extensions': {
281 'persistedQuery': {
282 'version': 1,
283 'sha256Hash': '78fe32792005eab7e922cafcdad9c56bed8bbc5f5df3c7cd24fcb84a744f5f78',
284 },
285 },
286 })
974208e1
AG
287
288
289class TrovoChannelClipIE(TrovoChannelBaseIE):
290 _VALID_URL = r'trovoclip:(?P<id>[^\s]+)'
96565c7e 291 IE_DESC = 'All Clips of a trovo.live channel; "trovoclip:" prefix'
974208e1
AG
292
293 _TESTS = [{
294 'url': 'trovoclip:OneTappedYou',
295 'playlist_mincount': 29,
296 'info_dict': {
297 'id': '100719456',
298 },
299 }]
300
974208e1
AG
301 _TYPE = 'clip'
302
303 def _get_vod_json(self, page, uid):
6ef5ad9e 304 return self._call_api(uid, data={
305 'operationName': 'getChannelClipVideoInfos',
306 'variables': {
307 'params': {
308 'channelID': int(uid),
309 'pageSize': 99,
310 'currPage': page,
311 },
312 },
313 'extensions': {
314 'persistedQuery': {
315 'version': 1,
316 'sha256Hash': 'e7924bfe20059b5c75fc8ff9e7929f43635681a7bdf3befa01072ed22c8eff31',
317 },
318 },
319 })