[cleanup] Make more playlist entries lazy (#11763)
[yt-dlp.git] / yt_dlp / extractor / openrec.py
blobb4f1c7d8587eaa52a12454302d53fc33a9398ff0
1 from .common import InfoExtractor
2 from ..utils import (
3 ExtractorError,
4 get_first,
5 int_or_none,
6 traverse_obj,
7 try_get,
8 unified_strdate,
9 unified_timestamp,
13 class OpenRecBaseIE(InfoExtractor):
14 _M3U8_HEADERS = {'Referer': 'https://www.openrec.tv/'}
16 def _extract_pagestore(self, webpage, video_id):
17 return self._parse_json(
18 self._search_regex(r'(?m)window\.pageStore\s*=\s*(\{.+?\});$', webpage, 'window.pageStore'), video_id)
20 def _expand_media(self, video_id, media):
21 for name, m3u8_url in (media or {}).items():
22 if not m3u8_url:
23 continue
24 yield from self._extract_m3u8_formats(
25 m3u8_url, video_id, ext='mp4', m3u8_id=name, headers=self._M3U8_HEADERS)
27 def _extract_movie(self, webpage, video_id, name, is_live):
28 window_stores = self._extract_pagestore(webpage, video_id)
29 movie_stores = [
30 # extract all three important data (most of data are duplicated each other, but slightly different!)
31 traverse_obj(window_stores, ('v8', 'state', 'movie'), expected_type=dict),
32 traverse_obj(window_stores, ('v8', 'movie'), expected_type=dict),
33 traverse_obj(window_stores, 'movieStore', expected_type=dict),
35 if not any(movie_stores):
36 raise ExtractorError(f'Failed to extract {name} info')
38 formats = list(self._expand_media(video_id, get_first(movie_stores, 'media')))
39 if not formats:
40 # archived livestreams or subscriber-only videos
41 cookies = self._get_cookies('https://www.openrec.tv/')
42 detail = self._download_json(
43 f'https://apiv5.openrec.tv/api/v5/movies/{video_id}/detail', video_id,
44 headers={
45 'Origin': 'https://www.openrec.tv',
46 'Referer': 'https://www.openrec.tv/',
47 'access-token': try_get(cookies, lambda x: x.get('access_token').value),
48 'uuid': try_get(cookies, lambda x: x.get('uuid').value),
50 new_media = traverse_obj(detail, ('data', 'items', ..., 'media'), get_all=False)
51 formats = list(self._expand_media(video_id, new_media))
52 is_live = False
54 return {
55 'id': video_id,
56 'title': get_first(movie_stores, 'title'),
57 'description': get_first(movie_stores, 'introduction'),
58 'thumbnail': get_first(movie_stores, 'thumbnailUrl'),
59 'formats': formats,
60 'uploader': get_first(movie_stores, ('channel', 'user', 'name')),
61 'uploader_id': get_first(movie_stores, ('channel', 'user', 'id')),
62 'timestamp': int_or_none(get_first(movie_stores, ['publishedAt', 'time']), scale=1000) or unified_timestamp(get_first(movie_stores, 'publishedAt')),
63 'is_live': is_live,
64 'http_headers': self._M3U8_HEADERS,
68 class OpenRecIE(OpenRecBaseIE):
69 IE_NAME = 'openrec'
70 _VALID_URL = r'https?://(?:www\.)?openrec\.tv/live/(?P<id>[^/]+)'
71 _TESTS = [{
72 'url': 'https://www.openrec.tv/live/2p8v31qe4zy',
73 'only_matching': True,
74 }, {
75 'url': 'https://www.openrec.tv/live/wez93eqvjzl',
76 'only_matching': True,
79 def _real_extract(self, url):
80 video_id = self._match_id(url)
81 webpage = self._download_webpage(f'https://www.openrec.tv/live/{video_id}', video_id)
83 return self._extract_movie(webpage, video_id, 'live', True)
86 class OpenRecCaptureIE(OpenRecBaseIE):
87 IE_NAME = 'openrec:capture'
88 _VALID_URL = r'https?://(?:www\.)?openrec\.tv/capture/(?P<id>[^/]+)'
89 _TESTS = [{
90 'url': 'https://www.openrec.tv/capture/l9nk2x4gn14',
91 'only_matching': True,
92 }, {
93 'url': 'https://www.openrec.tv/capture/mldjr82p7qk',
94 'info_dict': {
95 'id': 'mldjr82p7qk',
96 'title': 'たいじの恥ずかしい英語力',
97 'uploader': 'たいちゃんねる',
98 'uploader_id': 'Yaritaiji',
99 'upload_date': '20210803',
103 def _real_extract(self, url):
104 video_id = self._match_id(url)
105 webpage = self._download_webpage(f'https://www.openrec.tv/capture/{video_id}', video_id)
107 window_stores = self._extract_pagestore(webpage, video_id)
108 movie_store = window_stores.get('movie')
110 capture_data = window_stores.get('capture')
111 if not capture_data:
112 raise ExtractorError('Cannot extract title')
114 formats = self._extract_m3u8_formats(
115 capture_data.get('source'), video_id, ext='mp4', headers=self._M3U8_HEADERS)
117 return {
118 'id': video_id,
119 'title': capture_data.get('title'),
120 'thumbnail': capture_data.get('thumbnailUrl'),
121 'formats': formats,
122 'timestamp': unified_timestamp(traverse_obj(movie_store, 'createdAt', expected_type=str)),
123 'uploader': traverse_obj(movie_store, ('channel', 'name'), expected_type=str),
124 'uploader_id': traverse_obj(movie_store, ('channel', 'id'), expected_type=str),
125 'upload_date': unified_strdate(capture_data.get('createdAt')),
126 'http_headers': self._M3U8_HEADERS,
130 class OpenRecMovieIE(OpenRecBaseIE):
131 IE_NAME = 'openrec:movie'
132 _VALID_URL = r'https?://(?:www\.)?openrec\.tv/movie/(?P<id>[^/]+)'
133 _TESTS = [{
134 'url': 'https://www.openrec.tv/movie/nqz5xl5km8v',
135 'info_dict': {
136 'id': 'nqz5xl5km8v',
137 'title': '限定コミュニティ(Discord)参加方法ご説明動画',
138 'description': 'md5:ebd563e5f5b060cda2f02bf26b14d87f',
139 'thumbnail': r're:https://.+',
140 'uploader': 'タイキとカズヒロ',
141 'uploader_id': 'taiki_to_kazuhiro',
142 'timestamp': 1638856800,
146 def _real_extract(self, url):
147 video_id = self._match_id(url)
148 webpage = self._download_webpage(f'https://www.openrec.tv/movie/{video_id}', video_id)
150 return self._extract_movie(webpage, video_id, 'movie', False)