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