]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/rokfin.py
[extractor/youtube] Ignore incomplete data for comment threads by default (#7475)
[yt-dlp.git] / yt_dlp / extractor / rokfin.py
CommitLineData
334b1c48 1import itertools
c9b2b368 2import json
e037c405 3import re
4import urllib.parse
334b1c48 5from datetime import datetime
6
c9b2b368 7from .common import InfoExtractor, SearchInfoExtractor
334b1c48 8from ..utils import (
7eaf7f9a 9 ExtractorError,
e037c405 10 determine_ext,
334b1c48 11 float_or_none,
12 format_field,
13 int_or_none,
14 str_or_none,
15 traverse_obj,
e037c405 16 try_get,
17 unescapeHTML,
334b1c48 18 unified_timestamp,
19 url_or_none,
e037c405 20 urlencode_postdata,
334b1c48 21)
22
7eaf7f9a 23_API_BASE_URL = 'https://prod-api-v2.production.rokfin.com/api/v2/public/'
24
25
334b1c48 26class RokfinIE(InfoExtractor):
27 _VALID_URL = r'https?://(?:www\.)?rokfin\.com/(?P<id>(?P<type>post|stream)/\d+)'
e037c405 28 _NETRC_MACHINE = 'rokfin'
29 _AUTH_BASE = 'https://secure.rokfin.com/auth/realms/rokfin-web/protocol/openid-connect'
30 _access_mgmt_tokens = {} # OAuth 2.0: RFC 6749, Sec. 1.4-5
334b1c48 31 _TESTS = [{
32 'url': 'https://www.rokfin.com/post/57548/Mitt-Romneys-Crazy-Solution-To-Climate-Change',
33 'info_dict': {
34 'id': 'post/57548',
35 'ext': 'mp4',
36 'title': 'Mitt Romney\'s Crazy Solution To Climate Change',
37 'thumbnail': r're:https://img\.production\.rokfin\.com/.+',
38 'upload_date': '20211023',
39 'timestamp': 1634998029,
40 'channel': 'Jimmy Dore',
41 'channel_id': 65429,
42 'channel_url': 'https://rokfin.com/TheJimmyDoreShow',
43 'duration': 213.0,
44 'availability': 'public',
45 'live_status': 'not_live',
46 'dislike_count': int,
47 'like_count': int,
7a6c8a08 48 'duration': 213,
334b1c48 49 }
50 }, {
51 'url': 'https://rokfin.com/post/223/Julian-Assange-Arrested-Streaming-In-Real-Time',
52 'info_dict': {
53 'id': 'post/223',
54 'ext': 'mp4',
55 'title': 'Julian Assange Arrested: Streaming In Real Time',
56 'thumbnail': r're:https://img\.production\.rokfin\.com/.+',
57 'upload_date': '20190412',
58 'timestamp': 1555052644,
59 'channel': 'Ron Placone',
60 'channel_id': 10,
61 'channel_url': 'https://rokfin.com/RonPlacone',
62 'availability': 'public',
63 'live_status': 'not_live',
64 'dislike_count': int,
65 'like_count': int,
66 'tags': ['FreeThinkingMedia^', 'RealProgressives^'],
67 }
68 }, {
69 'url': 'https://www.rokfin.com/stream/10543/Its-A-Crazy-Mess-Regional-Director-Blows-Whistle-On-Pfizers-Vaccine-Trial-Data',
70 'info_dict': {
71 'id': 'stream/10543',
72 'ext': 'mp4',
73 'title': '"It\'s A Crazy Mess" Regional Director Blows Whistle On Pfizer\'s Vaccine Trial Data',
74 'thumbnail': r're:https://img\.production\.rokfin\.com/.+',
75 'description': 'md5:324ce2d3e3b62e659506409e458b9d8e',
7a6c8a08 76 'channel': 'TLAVagabond',
334b1c48 77 'channel_id': 53856,
78 'channel_url': 'https://rokfin.com/TLAVagabond',
79 'availability': 'public',
80 'is_live': False,
81 'was_live': True,
82 'live_status': 'was_live',
83 'timestamp': 1635874720,
84 'release_timestamp': 1635874720,
85 'release_date': '20211102',
86 'upload_date': '20211102',
87 'dislike_count': int,
88 'like_count': int,
89 'tags': ['FreeThinkingMedia^'],
7a6c8a08 90 'duration': None,
91 }
92 }, {
93 'url': 'https://rokfin.com/post/126703/Brave-New-World--Aldous-Huxley-DEEPDIVE--Chpts-13--Quite-Frankly--Jay-Dyer',
94 'info_dict': {
95 'id': 'post/126703',
96 'ext': 'mp4',
97 'title': 'Brave New World - Aldous Huxley DEEPDIVE! (Chpts 1-3) - Quite Frankly & Jay Dyer',
98 'thumbnail': r're:https://img\.production\.rokfin\.com/.+',
99 'channel': 'Jay Dyer',
100 'channel_id': 186881,
101 'channel_url': 'https://rokfin.com/jaydyer',
102 'availability': 'premium_only',
103 'live_status': 'not_live',
104 'dislike_count': int,
105 'like_count': int,
106 'timestamp': 1678213357,
107 'upload_date': '20230307',
108 'tags': ['FreeThinkingMedia^', 'OpenMind^'],
109 'description': 'md5:cb04e32e68326c9b2b251b297bacff35',
110 'duration': 3100,
111 }
112 }, {
113 'url': 'https://rokfin.com/stream/31332/The-Grayzone-live-on-Nordstream-blame-game',
114 'info_dict': {
115 'id': 'stream/31332',
116 'ext': 'mp4',
117 'title': 'The Grayzone live on Nordstream blame game',
118 'thumbnail': r're:https://image\.v\.rokfin\.com/.+',
119 'channel': 'Max Blumenthal',
120 'channel_id': 248902,
121 'channel_url': 'https://rokfin.com/MaxBlumenthal',
122 'availability': 'premium_only',
123 'live_status': 'was_live',
124 'dislike_count': int,
125 'like_count': int,
126 'timestamp': 1678475166,
127 'release_timestamp': 1678475166.0,
128 'release_date': '20230310',
129 'upload_date': '20230310',
130 'tags': ['FreeThinkingMedia^'],
334b1c48 131 }
132 }]
133
134 def _real_extract(self, url):
135 video_id, video_type = self._match_valid_url(url).group('id', 'type')
e037c405 136 metadata = self._download_json_using_access_token(f'{_API_BASE_URL}{video_id}', video_id)
334b1c48 137
138 scheduled = unified_timestamp(metadata.get('scheduledAt'))
139 live_status = ('was_live' if metadata.get('stoppedAt')
140 else 'is_upcoming' if scheduled
141 else 'is_live' if video_type == 'stream'
142 else 'not_live')
143
144 video_url = traverse_obj(metadata, 'url', ('content', 'contentUrl'), expected_type=url_or_none)
7a6c8a08 145 if video_url in (None, 'fake.m3u8'):
146 video_url = format_field(self._search_regex(
147 r'https?://[^/]+/([^/]+)/storyboard.vtt',
148 traverse_obj(metadata, 'timelineUrl', ('content', 'timelineUrl'), expected_type=url_or_none),
149 video_id, default=None), None, 'https://stream.v.rokfin.com/%s.m3u8')
150
334b1c48 151 formats, subtitles = [{'url': video_url}] if video_url else [], {}
152 if determine_ext(video_url) == 'm3u8':
153 formats, subtitles = self._extract_m3u8_formats_and_subtitles(
154 video_url, video_id, fatal=False, live=live_status == 'is_live')
155
156 if not formats:
9461cb58 157 if traverse_obj(metadata, 'premiumPlan', 'premium'):
334b1c48 158 self.raise_login_required('This video is only available to premium users', True, method='cookies')
159 elif scheduled:
160 self.raise_no_formats(
962ffcf8 161 f'Stream is offline; scheduled for {datetime.fromtimestamp(scheduled).strftime("%Y-%m-%d %H:%M:%S")}',
334b1c48 162 video_id=video_id, expected=True)
334b1c48 163
164 uploader = traverse_obj(metadata, ('createdBy', 'username'), ('creator', 'username'))
165 timestamp = (scheduled or float_or_none(metadata.get('postedAtMilli'), 1000)
166 or unified_timestamp(metadata.get('creationDateTime')))
167 return {
168 'id': video_id,
169 'formats': formats,
170 'subtitles': subtitles,
171 'title': str_or_none(traverse_obj(metadata, 'title', ('content', 'contentTitle'))),
172 'duration': float_or_none(traverse_obj(metadata, ('content', 'duration'))),
173 'thumbnail': url_or_none(traverse_obj(metadata, 'thumbnail', ('content', 'thumbnailUrl1'))),
174 'description': str_or_none(traverse_obj(metadata, 'description', ('content', 'contentDescription'))),
175 'like_count': int_or_none(metadata.get('likeCount')),
176 'dislike_count': int_or_none(metadata.get('dislikeCount')),
177 'channel': str_or_none(traverse_obj(metadata, ('createdBy', 'name'), ('creator', 'name'))),
178 'channel_id': traverse_obj(metadata, ('createdBy', 'id'), ('creator', 'id')),
179 'channel_url': url_or_none(f'https://rokfin.com/{uploader}') if uploader else None,
180 'timestamp': timestamp,
181 'release_timestamp': timestamp if live_status != 'not_live' else None,
182 'tags': traverse_obj(metadata, ('tags', ..., 'title'), expected_type=str_or_none),
183 'live_status': live_status,
184 'availability': self._availability(
9461cb58 185 needs_premium=bool(traverse_obj(metadata, 'premiumPlan', 'premium')),
334b1c48 186 is_private=False, needs_subscription=False, needs_auth=False, is_unlisted=False),
187 # 'comment_count': metadata.get('numComments'), # Data provided by website is wrong
188 '__post_extractor': self.extract_comments(video_id) if video_type == 'post' else None,
189 }
190
191 def _get_comments(self, video_id):
192 pages_total = None
193 for page_n in itertools.count():
194 raw_comments = self._download_json(
7eaf7f9a 195 f'{_API_BASE_URL}comment?postId={video_id[5:]}&page={page_n}&size=50',
a70635b8 196 video_id, note=f'Downloading viewer comments page {page_n + 1}{format_field(pages_total, None, " of %s")}',
334b1c48 197 fatal=False) or {}
198
199 for comment in raw_comments.get('content') or []:
200 yield {
201 'text': str_or_none(comment.get('comment')),
202 'author': str_or_none(comment.get('name')),
203 'id': comment.get('commentId'),
204 'author_id': comment.get('userId'),
205 'parent': 'root',
206 'like_count': int_or_none(comment.get('numLikes')),
207 'dislike_count': int_or_none(comment.get('numDislikes')),
208 'timestamp': unified_timestamp(comment.get('postedAt'))
209 }
210
7eaf7f9a 211 pages_total = int_or_none(raw_comments.get('totalPages')) or None
212 is_last = raw_comments.get('last')
213 if not raw_comments.get('content') or is_last or (page_n > pages_total if pages_total else is_last is not False):
334b1c48 214 return
7eaf7f9a 215
e037c405 216 def _perform_login(self, username, password):
217 # https://openid.net/specs/openid-connect-core-1_0.html#CodeFlowAuth (Sec. 3.1)
218 login_page = self._download_webpage(
219 f'{self._AUTH_BASE}/auth?client_id=web&redirect_uri=https%3A%2F%2Frokfin.com%2Ffeed&response_mode=fragment&response_type=code&scope=openid',
220 None, note='loading login page', errnote='error loading login page')
221 authentication_point_url = unescapeHTML(self._search_regex(
222 r'<form\s+[^>]+action\s*=\s*"(https://secure\.rokfin\.com/auth/realms/rokfin-web/login-actions/authenticate\?[^"]+)"',
223 login_page, name='Authentication URL'))
224
225 resp_body = self._download_webpage(
226 authentication_point_url, None, note='logging in', fatal=False, expected_status=404,
227 data=urlencode_postdata({'username': username, 'password': password, 'rememberMe': 'off', 'credentialId': ''}))
228 if not self._authentication_active():
229 if re.search(r'(?i)(invalid\s+username\s+or\s+password)', resp_body or ''):
230 raise ExtractorError('invalid username/password', expected=True)
231 raise ExtractorError('Login failed')
232
233 urlh = self._request_webpage(
234 f'{self._AUTH_BASE}/auth', None,
235 note='granting user authorization', errnote='user authorization rejected by Rokfin',
236 query={
237 'client_id': 'web',
238 'prompt': 'none',
239 'redirect_uri': 'https://rokfin.com/silent-check-sso.html',
240 'response_mode': 'fragment',
241 'response_type': 'code',
242 'scope': 'openid',
243 })
244 self._access_mgmt_tokens = self._download_json(
245 f'{self._AUTH_BASE}/token', None,
246 note='getting access credentials', errnote='error getting access credentials',
247 data=urlencode_postdata({
248 'code': urllib.parse.parse_qs(urllib.parse.urldefrag(urlh.geturl()).fragment).get('code')[0],
249 'client_id': 'web',
250 'grant_type': 'authorization_code',
251 'redirect_uri': 'https://rokfin.com/silent-check-sso.html'
252 }))
253
254 def _authentication_active(self):
255 return not (
256 {'KEYCLOAK_IDENTITY', 'KEYCLOAK_IDENTITY_LEGACY', 'KEYCLOAK_SESSION', 'KEYCLOAK_SESSION_LEGACY'}
257 - set(self._get_cookies(self._AUTH_BASE)))
258
259 def _get_auth_token(self):
260 return try_get(self._access_mgmt_tokens, lambda x: ' '.join([x['token_type'], x['access_token']]))
261
262 def _download_json_using_access_token(self, url_or_request, video_id, headers={}, query={}):
263 assert 'authorization' not in headers
264 headers = headers.copy()
265 auth_token = self._get_auth_token()
266 refresh_token = self._access_mgmt_tokens.get('refresh_token')
267 if auth_token:
268 headers['authorization'] = auth_token
269
270 json_string, urlh = self._download_webpage_handle(
271 url_or_request, video_id, headers=headers, query=query, expected_status=401)
272 if not auth_token or urlh.code != 401 or refresh_token is None:
273 return self._parse_json(json_string, video_id)
274
275 self._access_mgmt_tokens = self._download_json(
276 f'{self._AUTH_BASE}/token', video_id,
277 note='User authorization expired or canceled by Rokfin. Re-authorizing ...', errnote='Failed to re-authorize',
278 data=urlencode_postdata({
279 'grant_type': 'refresh_token',
280 'refresh_token': refresh_token,
281 'client_id': 'web'
282 }))
283 headers['authorization'] = self._get_auth_token()
284 if headers['authorization'] is None:
285 raise ExtractorError('User authorization lost', expected=True)
286
287 return self._download_json(url_or_request, video_id, headers=headers, query=query)
288
7eaf7f9a 289
290class RokfinPlaylistBaseIE(InfoExtractor):
291 _TYPES = {
292 'video': 'post',
293 'audio': 'post',
294 'stream': 'stream',
295 'dead_stream': 'stream',
296 'stack': 'stack',
297 }
298
299 def _get_video_data(self, metadata):
300 for content in metadata.get('content') or []:
301 media_type = self._TYPES.get(content.get('mediaType'))
302 video_id = content.get('id') if media_type == 'post' else content.get('mediaId')
303 if not media_type or not video_id:
304 continue
305
306 yield self.url_result(f'https://rokfin.com/{media_type}/{video_id}', video_id=f'{media_type}/{video_id}',
307 video_title=str_or_none(traverse_obj(content, ('content', 'contentTitle'))))
308
309
310class RokfinStackIE(RokfinPlaylistBaseIE):
311 IE_NAME = 'rokfin:stack'
e037c405 312 IE_DESC = 'Rokfin Stacks'
7eaf7f9a 313 _VALID_URL = r'https?://(?:www\.)?rokfin\.com/stack/(?P<id>[^/]+)'
314 _TESTS = [{
315 'url': 'https://www.rokfin.com/stack/271/Tulsi-Gabbard-Portsmouth-Townhall-FULL--Feb-9-2020',
316 'playlist_count': 8,
317 'info_dict': {
318 'id': '271',
319 },
320 }]
321
322 def _real_extract(self, url):
323 list_id = self._match_id(url)
324 return self.playlist_result(self._get_video_data(
325 self._download_json(f'{_API_BASE_URL}stack/{list_id}', list_id)), list_id)
326
327
328class RokfinChannelIE(RokfinPlaylistBaseIE):
329 IE_NAME = 'rokfin:channel'
e037c405 330 IE_DESC = 'Rokfin Channels'
7eaf7f9a 331 _VALID_URL = r'https?://(?:www\.)?rokfin\.com/(?!((feed/?)|(discover/?)|(channels/?))$)(?P<id>[^/]+)/?$'
332 _TESTS = [{
333 'url': 'https://rokfin.com/TheConvoCouch',
334 'playlist_mincount': 100,
335 'info_dict': {
336 'id': '12071-new',
337 'title': 'TheConvoCouch - New',
338 'description': 'md5:bb622b1bca100209b91cd685f7847f06',
339 },
340 }]
341
342 _TABS = {
343 'new': 'posts',
344 'top': 'top',
345 'videos': 'video',
346 'podcasts': 'audio',
347 'streams': 'stream',
348 'stacks': 'stack',
349 }
350
351 def _real_initialize(self):
352 self._validate_extractor_args()
353
354 def _validate_extractor_args(self):
355 requested_tabs = self._configuration_arg('tab', None)
356 if requested_tabs is not None and (len(requested_tabs) > 1 or requested_tabs[0] not in self._TABS):
357 raise ExtractorError(f'Invalid extractor-arg "tab". Must be one of {", ".join(self._TABS)}', expected=True)
358
359 def _entries(self, channel_id, channel_name, tab):
360 pages_total = None
361 for page_n in itertools.count(0):
362 if tab in ('posts', 'top'):
363 data_url = f'{_API_BASE_URL}user/{channel_name}/{tab}?page={page_n}&size=50'
364 else:
365 data_url = f'{_API_BASE_URL}post/search/{tab}?page={page_n}&size=50&creator={channel_id}'
366 metadata = self._download_json(
367 data_url, channel_name,
a70635b8 368 note=f'Downloading video metadata page {page_n + 1}{format_field(pages_total, None, " of %s")}')
7eaf7f9a 369
370 yield from self._get_video_data(metadata)
371 pages_total = int_or_none(metadata.get('totalPages')) or None
372 is_last = metadata.get('last')
373 if is_last or (page_n > pages_total if pages_total else is_last is not False):
374 return
375
376 def _real_extract(self, url):
377 channel_name = self._match_id(url)
378 channel_info = self._download_json(f'{_API_BASE_URL}user/{channel_name}', channel_name)
379 channel_id = channel_info['id']
380 tab = self._configuration_arg('tab', default=['new'])[0]
381
382 return self.playlist_result(
383 self._entries(channel_id, channel_name, self._TABS[tab]),
384 f'{channel_id}-{tab}', f'{channel_name} - {tab.title()}', str_or_none(channel_info.get('description')))
c9b2b368 385
386
387class RokfinSearchIE(SearchInfoExtractor):
388 IE_NAME = 'rokfin:search'
389 IE_DESC = 'Rokfin Search'
390 _SEARCH_KEY = 'rkfnsearch'
391 _TYPES = {
392 'video': (('id', 'raw'), 'post'),
393 'audio': (('id', 'raw'), 'post'),
394 'stream': (('content_id', 'raw'), 'stream'),
395 'dead_stream': (('content_id', 'raw'), 'stream'),
396 'stack': (('content_id', 'raw'), 'stack'),
397 }
398 _TESTS = [{
399 'url': 'rkfnsearch5:"zelenko"',
400 'playlist_count': 5,
401 'info_dict': {
402 'id': '"zelenko"',
403 'title': '"zelenko"',
404 }
405 }]
406 _db_url = None
407 _db_access_key = None
408
409 def _real_initialize(self):
9809740b 410 self._db_url, self._db_access_key = self.cache.load(self.ie_key(), 'auth', default=(None, None))
c9b2b368 411 if not self._db_url:
412 self._get_db_access_credentials()
413
414 def _search_results(self, query):
415 total_pages = None
416 for page_number in itertools.count(1):
417 search_results = self._run_search_query(
418 query, data={'query': query, 'page': {'size': 100, 'current': page_number}},
a70635b8 419 note=f'Downloading page {page_number}{format_field(total_pages, None, " of ~%s")}')
c9b2b368 420 total_pages = traverse_obj(search_results, ('meta', 'page', 'total_pages'), expected_type=int_or_none)
421
422 for result in search_results.get('results') or []:
423 video_id_key, video_type = self._TYPES.get(traverse_obj(result, ('content_type', 'raw')), (None, None))
424 video_id = traverse_obj(result, video_id_key, expected_type=int_or_none)
425 if video_id and video_type:
426 yield self.url_result(url=f'https://rokfin.com/{video_type}/{video_id}')
427 if not search_results.get('results'):
428 return
429
430 def _run_search_query(self, video_id, data, **kwargs):
431 data = json.dumps(data).encode()
432 for attempt in range(2):
433 search_results = self._download_json(
434 self._db_url, video_id, data=data, fatal=(attempt == 1),
435 headers={'authorization': self._db_access_key}, **kwargs)
436 if search_results:
437 return search_results
438 self.write_debug('Updating access credentials')
439 self._get_db_access_credentials(video_id)
440
441 def _get_db_access_credentials(self, video_id=None):
442 auth_data = {'SEARCH_KEY': None, 'ENDPOINT_BASE': None}
443 notfound_err_page = self._download_webpage(
444 'https://rokfin.com/discover', video_id, expected_status=404, note='Downloading home page')
445 for js_file_path in re.findall(r'<script\b[^>]*\ssrc\s*=\s*"(/static/js/[^">]+)"', notfound_err_page):
446 js_content = self._download_webpage(
447 f'https://rokfin.com{js_file_path}', video_id, note='Downloading JavaScript file', fatal=False)
448 auth_data.update(re.findall(
449 rf'REACT_APP_({"|".join(auth_data.keys())})\s*:\s*"([^"]+)"', js_content or ''))
450 if not all(auth_data.values()):
451 continue
452
453 self._db_url = url_or_none(f'{auth_data["ENDPOINT_BASE"]}/api/as/v1/engines/rokfin-search/search.json')
454 self._db_access_key = f'Bearer {auth_data["SEARCH_KEY"]}'
9809740b 455 self.cache.store(self.ie_key(), 'auth', (self._db_url, self._db_access_key))
c9b2b368 456 return
457 raise ExtractorError('Unable to extract access credentials')