]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/iprima.py
[extractor/FranceCulture] Fix extractor (#3874)
[yt-dlp.git] / yt_dlp / extractor / iprima.py
CommitLineData
7881a644 1import re
f406c787 2import time
7881a644 3
4from .common import InfoExtractor
1cc79574 5from ..utils import (
369e7e3f
S
6 determine_ext,
7 js_to_json,
e1b7c54d 8 urlencode_postdata,
9 ExtractorError,
10 parse_qs
82642235 11)
7881a644 12
13
14class IPrimaIE(InfoExtractor):
e1b7c54d 15 _VALID_URL = r'https?://(?!cnn)(?:[^/]+)\.iprima\.cz/(?:[^/]+/)*(?P<id>[^/?#&]+)'
da42ff06 16 _GEO_BYPASS = False
e1b7c54d 17 _NETRC_MACHINE = 'iprima'
18 _LOGIN_URL = 'https://auth.iprima.cz/oauth2/login'
19 _TOKEN_URL = 'https://auth.iprima.cz/oauth2/token'
20 access_token = None
7881a644 21
22 _TESTS = [{
30fa5c60 23 'url': 'https://prima.iprima.cz/particka/92-epizoda',
7881a644 24 'info_dict': {
30fa5c60 25 'id': 'p51388',
f406c787 26 'ext': 'mp4',
30fa5c60
S
27 'title': 'Partička (92)',
28 'description': 'md5:859d53beae4609e6dd7796413f1b6cac',
e1b7c54d 29 'upload_date': '20201103',
30 'timestamp': 1604437480,
7881a644 31 },
32 'params': {
f406c787 33 'skip_download': True, # m3u8 download
7881a644 34 },
973f2532 35 }, {
f406c787 36 'url': 'http://play.iprima.cz/particka/particka-92',
bc03e585 37 'only_matching': True,
da42ff06
S
38 }, {
39 # geo restricted
40 'url': 'http://play.iprima.cz/closer-nove-pripady/closer-nove-pripady-iv-1',
41 'only_matching': True,
a2637a2d 42 }, {
a2637a2d
S
43 'url': 'https://prima.iprima.cz/my-little-pony/mapa-znameni-2-2',
44 'only_matching': True,
45 }, {
a2637a2d
S
46 'url': 'https://prima.iprima.cz/porady/jak-se-stavi-sen/rodina-rathousova-praha',
47 'only_matching': True,
9235b509
S
48 }, {
49 'url': 'http://www.iprima.cz/filmy/desne-rande',
50 'only_matching': True,
90046d77 51 }, {
52 'url': 'https://zoom.iprima.cz/10-nejvetsich-tajemstvi-zahad/posvatna-mista-a-stavby',
53 'only_matching': True,
54 }, {
55 'url': 'https://krimi.iprima.cz/mraz-0/sebevrazdy',
56 'only_matching': True,
57 }, {
58 'url': 'https://cool.iprima.cz/derava-silnice-nevadi',
59 'only_matching': True,
60 }, {
61 'url': 'https://love.iprima.cz/laska-az-za-hrob/slib-dany-bratrovi',
62 'only_matching': True,
e1b7c54d 63 }]
64
52efa4b3 65 def _perform_login(self, username, password):
66 if self.access_token:
67 return
e1b7c54d 68
69 login_page = self._download_webpage(
70 self._LOGIN_URL, None, note='Downloading login page',
71 errnote='Downloading login page failed')
72
73 login_form = self._hidden_inputs(login_page)
74
75 login_form.update({
76 '_email': username,
77 '_password': password})
78
79 _, login_handle = self._download_webpage_handle(
80 self._LOGIN_URL, None, data=urlencode_postdata(login_form),
81 note='Logging in')
82
83 code = parse_qs(login_handle.geturl()).get('code')[0]
84 if not code:
85 raise ExtractorError('Login failed', expected=True)
86
87 token_request_data = {
88 'scope': 'openid+email+profile+phone+address+offline_access',
89 'client_id': 'prima_sso',
90 'grant_type': 'authorization_code',
91 'code': code,
d54c6003 92 'redirect_uri': 'https://auth.iprima.cz/sso/auth-check'}
e1b7c54d 93
94 token_data = self._download_json(
95 self._TOKEN_URL, None,
96 note='Downloading token', errnote='Downloading token failed',
97 data=urlencode_postdata(token_request_data))
98
99 self.access_token = token_data.get('access_token')
100 if self.access_token is None:
101 raise ExtractorError('Getting token failed', expected=True)
102
52efa4b3 103 def _real_initialize(self):
104 if not self.access_token:
105 self.raise_login_required('Login is required to access any iPrima content', method='password')
106
e1b7c54d 107 def _raise_access_error(self, error_code):
108 if error_code == 'PLAY_GEOIP_DENIED':
109 self.raise_geo_restricted(countries=['CZ'], metadata_available=True)
110 elif error_code is not None:
111 self.raise_no_formats('Access to stream infos forbidden', expected=True)
112
e1b7c54d 113 def _real_extract(self, url):
114 video_id = self._match_id(url)
115
116 webpage = self._download_webpage(url, video_id)
117
118 title = self._html_search_meta(
119 ['og:title', 'twitter:title'],
120 webpage, 'title', default=None)
121
122 video_id = self._search_regex((
123 r'productId\s*=\s*([\'"])(?P<id>p\d+)\1',
124 r'pproduct_id\s*=\s*([\'"])(?P<id>p\d+)\1'),
125 webpage, 'real id', group='id')
126
127 metadata = self._download_json(
128 f'https://api.play-backend.iprima.cz/api/v1//products/id-{video_id}/play',
129 video_id, note='Getting manifest URLs', errnote='Failed to get manifest URLs',
130 headers={'X-OTT-Access-Token': self.access_token},
131 expected_status=403)
132
133 self._raise_access_error(metadata.get('errorCode'))
134
135 stream_infos = metadata.get('streamInfos')
136 formats = []
137 if stream_infos is None:
138 self.raise_no_formats('Reading stream infos failed', expected=True)
139 else:
140 for manifest in stream_infos:
141 manifest_type = manifest.get('type')
142 manifest_url = manifest.get('url')
143 ext = determine_ext(manifest_url)
144 if manifest_type == 'HLS' or ext == 'm3u8':
145 formats += self._extract_m3u8_formats(
146 manifest_url, video_id, 'mp4', entry_protocol='m3u8_native',
147 m3u8_id='hls', fatal=False)
148 elif manifest_type == 'DASH' or ext == 'mpd':
149 formats += self._extract_mpd_formats(
150 manifest_url, video_id, mpd_id='dash', fatal=False)
151 self._sort_formats(formats)
152
153 final_result = self._search_json_ld(webpage, video_id) or {}
154 final_result.update({
155 'id': video_id,
156 'title': title,
157 'thumbnail': self._html_search_meta(
158 ['thumbnail', 'og:image', 'twitter:image'],
159 webpage, 'thumbnail', default=None),
160 'formats': formats,
161 'description': self._html_search_meta(
162 ['description', 'og:description', 'twitter:description'],
163 webpage, 'description', default=None)})
164
165 return final_result
166
167
168class IPrimaCNNIE(InfoExtractor):
169 _VALID_URL = r'https?://cnn\.iprima\.cz/(?:[^/]+/)*(?P<id>[^/?#&]+)'
170 _GEO_BYPASS = False
171
172 _TESTS = [{
173 'url': 'https://cnn.iprima.cz/porady/strunc/24072020-koronaviru-mam-plne-zuby-strasit-druhou-vlnou-je-absurdni-rika-senatorka-dernerova',
174 'info_dict': {
175 'id': 'p716177',
176 'ext': 'mp4',
177 'title': 'md5:277c6b1ed0577e51b40ddd35602ff43e',
178 },
179 'params': {
180 'skip_download': 'm3u8'
181 }
973f2532 182 }]
7881a644 183
184 def _real_extract(self, url):
369e7e3f 185 video_id = self._match_id(url)
7881a644 186
09322ccc
S
187 self._set_cookie('play.iprima.cz', 'ott_adult_confirmed', '1')
188
7881a644 189 webpage = self._download_webpage(url, video_id)
190
30fa5c60
S
191 title = self._og_search_title(
192 webpage, default=None) or self._search_regex(
193 r'<h1>([^<]+)', webpage, 'title')
194
a2637a2d
S
195 video_id = self._search_regex(
196 (r'<iframe[^>]+\bsrc=["\'](?:https?:)?//(?:api\.play-backend\.iprima\.cz/prehravac/embedded|prima\.iprima\.cz/[^/]+/[^/]+)\?.*?\bid=(p\d+)',
30fa5c60
S
197 r'data-product="([^"]+)">',
198 r'id=["\']player-(p\d+)"',
b7770046
U
199 r'playerId\s*:\s*["\']player-(p\d+)',
200 r'\bvideos\s*=\s*["\'](p\d+)'),
a2637a2d 201 webpage, 'real id')
82642235 202
82f66218
S
203 playerpage = self._download_webpage(
204 'http://play.iprima.cz/prehravac/init',
205 video_id, note='Downloading player', query={
206 '_infuse': 1,
207 '_ts': round(time.time()),
208 'productId': video_id,
209 }, headers={'Referer': url})
7881a644 210
369e7e3f 211 formats = []
7881a644 212
369e7e3f
S
213 def extract_formats(format_url, format_key=None, lang=None):
214 ext = determine_ext(format_url)
215 new_formats = []
216 if format_key == 'hls' or ext == 'm3u8':
217 new_formats = self._extract_m3u8_formats(
218 format_url, video_id, 'mp4', entry_protocol='m3u8_native',
219 m3u8_id='hls', fatal=False)
220 elif format_key == 'dash' or ext == 'mpd':
221 return
222 new_formats = self._extract_mpd_formats(
223 format_url, video_id, mpd_id='dash', fatal=False)
224 if lang:
225 for f in new_formats:
226 if not f.get('language'):
227 f['language'] = lang
228 formats.extend(new_formats)
229
230 options = self._parse_json(
231 self._search_regex(
0bbcc8a1 232 r'(?s)(?:TDIPlayerOptions|playerOptions)\s*=\s*({.+?});\s*\]\]',
369e7e3f
S
233 playerpage, 'player options', default='{}'),
234 video_id, transform_source=js_to_json, fatal=False)
235 if options:
236 for key, tracks in options.get('tracks', {}).items():
237 if not isinstance(tracks, list):
238 continue
239 for track in tracks:
240 src = track.get('src')
241 if src:
242 extract_formats(src, key.lower(), track.get('lang'))
243
244 if not formats:
245 for _, src in re.findall(r'src["\']\s*:\s*(["\'])(.+?)\1', playerpage):
246 extract_formats(src)
91264ce5 247
3c6b3bf2 248 if not formats and '>GEO_IP_NOT_ALLOWED<' in playerpage:
b7da73eb 249 self.raise_geo_restricted(countries=['CZ'], metadata_available=True)
3c6b3bf2 250
91264ce5 251 self._sort_formats(formats)
7881a644 252
253 return {
f406c787 254 'id': video_id,
30fa5c60
S
255 'title': title,
256 'thumbnail': self._og_search_thumbnail(webpage, default=None),
7881a644 257 'formats': formats,
30fa5c60 258 'description': self._og_search_description(webpage, default=None),
91264ce5 259 }