]>
Commit | Line | Data |
---|---|---|
7881a644 | 1 | import re |
f406c787 | 2 | import time |
7881a644 | 3 | |
4 | from .common import InfoExtractor | |
1cc79574 | 5 | from ..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 | ||
14 | class 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) | |
e1b7c54d | 151 | |
2530b68d | 152 | final_result = self._search_json_ld(webpage, video_id, default={}) |
e1b7c54d | 153 | final_result.update({ |
154 | 'id': video_id, | |
155 | 'title': title, | |
156 | 'thumbnail': self._html_search_meta( | |
157 | ['thumbnail', 'og:image', 'twitter:image'], | |
158 | webpage, 'thumbnail', default=None), | |
159 | 'formats': formats, | |
160 | 'description': self._html_search_meta( | |
161 | ['description', 'og:description', 'twitter:description'], | |
162 | webpage, 'description', default=None)}) | |
163 | ||
164 | return final_result | |
165 | ||
166 | ||
167 | class IPrimaCNNIE(InfoExtractor): | |
168 | _VALID_URL = r'https?://cnn\.iprima\.cz/(?:[^/]+/)*(?P<id>[^/?#&]+)' | |
169 | _GEO_BYPASS = False | |
170 | ||
171 | _TESTS = [{ | |
172 | 'url': 'https://cnn.iprima.cz/porady/strunc/24072020-koronaviru-mam-plne-zuby-strasit-druhou-vlnou-je-absurdni-rika-senatorka-dernerova', | |
173 | 'info_dict': { | |
174 | 'id': 'p716177', | |
175 | 'ext': 'mp4', | |
176 | 'title': 'md5:277c6b1ed0577e51b40ddd35602ff43e', | |
177 | }, | |
178 | 'params': { | |
179 | 'skip_download': 'm3u8' | |
180 | } | |
973f2532 | 181 | }] |
7881a644 | 182 | |
183 | def _real_extract(self, url): | |
369e7e3f | 184 | video_id = self._match_id(url) |
7881a644 | 185 | |
09322ccc S |
186 | self._set_cookie('play.iprima.cz', 'ott_adult_confirmed', '1') |
187 | ||
7881a644 | 188 | webpage = self._download_webpage(url, video_id) |
189 | ||
30fa5c60 S |
190 | title = self._og_search_title( |
191 | webpage, default=None) or self._search_regex( | |
192 | r'<h1>([^<]+)', webpage, 'title') | |
193 | ||
a2637a2d S |
194 | video_id = self._search_regex( |
195 | (r'<iframe[^>]+\bsrc=["\'](?:https?:)?//(?:api\.play-backend\.iprima\.cz/prehravac/embedded|prima\.iprima\.cz/[^/]+/[^/]+)\?.*?\bid=(p\d+)', | |
30fa5c60 S |
196 | r'data-product="([^"]+)">', |
197 | r'id=["\']player-(p\d+)"', | |
b7770046 U |
198 | r'playerId\s*:\s*["\']player-(p\d+)', |
199 | r'\bvideos\s*=\s*["\'](p\d+)'), | |
a2637a2d | 200 | webpage, 'real id') |
82642235 | 201 | |
82f66218 S |
202 | playerpage = self._download_webpage( |
203 | 'http://play.iprima.cz/prehravac/init', | |
204 | video_id, note='Downloading player', query={ | |
205 | '_infuse': 1, | |
206 | '_ts': round(time.time()), | |
207 | 'productId': video_id, | |
208 | }, headers={'Referer': url}) | |
7881a644 | 209 | |
369e7e3f | 210 | formats = [] |
7881a644 | 211 | |
369e7e3f S |
212 | def extract_formats(format_url, format_key=None, lang=None): |
213 | ext = determine_ext(format_url) | |
214 | new_formats = [] | |
215 | if format_key == 'hls' or ext == 'm3u8': | |
216 | new_formats = self._extract_m3u8_formats( | |
217 | format_url, video_id, 'mp4', entry_protocol='m3u8_native', | |
218 | m3u8_id='hls', fatal=False) | |
219 | elif format_key == 'dash' or ext == 'mpd': | |
220 | return | |
221 | new_formats = self._extract_mpd_formats( | |
222 | format_url, video_id, mpd_id='dash', fatal=False) | |
223 | if lang: | |
224 | for f in new_formats: | |
225 | if not f.get('language'): | |
226 | f['language'] = lang | |
227 | formats.extend(new_formats) | |
228 | ||
229 | options = self._parse_json( | |
230 | self._search_regex( | |
0bbcc8a1 | 231 | r'(?s)(?:TDIPlayerOptions|playerOptions)\s*=\s*({.+?});\s*\]\]', |
369e7e3f S |
232 | playerpage, 'player options', default='{}'), |
233 | video_id, transform_source=js_to_json, fatal=False) | |
234 | if options: | |
235 | for key, tracks in options.get('tracks', {}).items(): | |
236 | if not isinstance(tracks, list): | |
237 | continue | |
238 | for track in tracks: | |
239 | src = track.get('src') | |
240 | if src: | |
241 | extract_formats(src, key.lower(), track.get('lang')) | |
242 | ||
243 | if not formats: | |
244 | for _, src in re.findall(r'src["\']\s*:\s*(["\'])(.+?)\1', playerpage): | |
245 | extract_formats(src) | |
91264ce5 | 246 | |
3c6b3bf2 | 247 | if not formats and '>GEO_IP_NOT_ALLOWED<' in playerpage: |
b7da73eb | 248 | self.raise_geo_restricted(countries=['CZ'], metadata_available=True) |
3c6b3bf2 | 249 | |
7881a644 | 250 | return { |
f406c787 | 251 | 'id': video_id, |
30fa5c60 S |
252 | 'title': title, |
253 | 'thumbnail': self._og_search_thumbnail(webpage, default=None), | |
7881a644 | 254 | 'formats': formats, |
30fa5c60 | 255 | 'description': self._og_search_description(webpage, default=None), |
91264ce5 | 256 | } |