9 from .common
import InfoExtractor
10 from ..compat
import compat_ord
, compat_urllib_parse_unquote
26 class CDAIE(InfoExtractor
):
27 _VALID_URL
= r
'https?://(?:(?:www\.)?cda\.pl/video|ebd\.cda\.pl/[0-9]+x[0-9]+)/(?P<id>[0-9a-z]+)'
28 _NETRC_MACHINE
= 'cdapl'
30 _BASE_URL
= 'http://www.cda.pl/'
31 _BASE_API_URL
= 'https://api.cda.pl'
33 'Accept': 'application/vnd.cda.public+json',
34 'User-Agent': 'pl.cda 1.0 (version 1.2.88 build 15306; Android 9; Xiaomi Redmi 3S)',
36 # hardcoded in the app
37 _LOGIN_REQUEST_AUTH
= 'Basic YzU3YzBlZDUtYTIzOC00MWQwLWI2NjQtNmZmMWMxY2Y2YzVlOklBTm95QlhRRVR6U09MV1hnV3MwMW0xT2VyNWJNZzV4clRNTXhpNGZJUGVGZ0lWUlo5UGVYTDhtUGZaR1U1U3Q'
38 _BEARER_CACHE
= 'cda-bearer'
41 'url': 'http://www.cda.pl/video/5749950c',
42 'md5': '6f844bf51b15f31fae165365707ae970',
47 'title': 'Oto dlaczego przed zakrętem należy zwolnić.',
48 'description': 'md5:269ccd135d550da90d1662651fcb9772',
49 'thumbnail': r
're:^https?://.*\.jpg$',
50 'average_rating': float,
53 'upload_date': '20160221',
54 'timestamp': 1456078244,
57 'url': 'http://www.cda.pl/video/57413289',
58 'md5': 'a88828770a8310fc00be6c95faf7f4d5',
62 'title': 'Lądowanie na lotnisku na Maderze',
63 'description': 'md5:60d76b71186dcce4e0ba6d4bbdb13e1a',
64 'thumbnail': r
're:^https?://.*\.jpg$',
65 'uploader': 'crash404',
67 'average_rating': float,
73 'url': 'http://www.cda.pl/video/1273454c4',
77 'title': 'Bronson (2008) napisy HD 1080p',
78 'description': 'md5:1b6cb18508daf2dc4e0fa4db77fec24c',
80 'uploader': 'boniek61',
81 'thumbnail': r
're:^https?://.*\.jpg$',
85 'average_rating': float,
88 'url': 'http://ebd.cda.pl/0x0/5749950c',
89 'only_matching': True,
92 def _download_age_confirm_page(self
, url
, video_id
, *args
, **kwargs
):
93 form_data
= random_birthday('rok', 'miesiac', 'dzien')
94 form_data
.update({'return': url, 'module': 'video', 'module_id': video_id}
)
95 data
, content_type
= multipart_encode(form_data
)
96 return self
._download
_webpage
(
97 urljoin(url
, '/a/validatebirth'), video_id
, *args
,
100 'Content-Type': content_type
,
103 def _perform_login(self
, username
, password
):
104 cached_bearer
= self
.cache
.load(self
._BEARER
_CACHE
, username
) or {}
105 if cached_bearer
.get('valid_until', 0) > datetime
.datetime
.now().timestamp() + 5:
106 self
._API
_HEADERS
['Authorization'] = f
'Bearer {cached_bearer["token"]}'
109 password_hash
= base64
.urlsafe_b64encode(hmac
.new(
110 b
's01m1Oer5IANoyBXQETzSOLWXgWs01m1Oer5bMg5xrTMMxRZ9Pi4fIPeFgIVRZ9PeXL8mPfXQETZGUAN5StRZ9P',
111 ''.join(f
'{bytes((bt & 255, )).hex():0>2}'
112 for bt
in hashlib
.md5(password
.encode()).digest()).encode(),
113 hashlib
.sha256
).digest()).decode().replace('=', '')
115 token_res
= self
._download
_json
(
116 f
'{self._BASE_API_URL}/oauth/token', None, 'Logging in', data
=b
'',
117 headers
={**self._API_HEADERS, 'Authorization': self._LOGIN_REQUEST_AUTH}
,
119 'grant_type': 'password',
121 'password': password_hash
,
123 self
.cache
.store(self
._BEARER
_CACHE
, username
, {
124 'token': token_res
['access_token'],
125 'valid_until': token_res
['expires_in'] + datetime
.datetime
.now().timestamp(),
127 self
._API
_HEADERS
['Authorization'] = f
'Bearer {token_res["access_token"]}'
129 def _real_extract(self
, url
):
130 video_id
= self
._match
_id
(url
)
132 if 'Authorization' in self
._API
_HEADERS
:
133 return self
._api
_extract
(video_id
)
135 return self
._web
_extract
(video_id
, url
)
137 def _api_extract(self
, video_id
):
138 meta
= self
._download
_json
(
139 f
'{self._BASE_API_URL}/video/{video_id}', video_id
, headers
=self
._API
_HEADERS
)['video']
141 if meta
.get('premium') and not meta
.get('premium_free'):
142 self
.report_drm(video_id
)
144 uploader
= traverse_obj(meta
, 'author', 'login')
147 'url': quality
['file'],
148 'format': quality
.get('title'),
149 'resolution': quality
.get('name'),
150 'height': try_call(lambda: int(quality
['name'][:-1])),
151 'filesize': quality
.get('length'),
152 } for quality
in meta
['qualities'] if quality
.get('file')]
154 self
._sort
_formats
(formats
)
158 'title': meta
.get('title'),
159 'description': meta
.get('description'),
160 'uploader': None if uploader
== 'anonim' else uploader
,
161 'average_rating': float_or_none(meta
.get('rating')),
162 'thumbnail': meta
.get('thumb'),
164 'duration': meta
.get('duration'),
165 'age_limit': 18 if meta
.get('for_adults') else 0,
166 'view_count': meta
.get('views'),
169 def _web_extract(self
, video_id
, url
):
170 self
._set
_cookie
('cda.pl', 'cda.player', 'html5')
171 webpage
= self
._download
_webpage
(
172 self
._BASE
_URL
+ '/video/' + video_id
, video_id
)
174 if 'Ten film jest dostępny dla użytkowników premium' in webpage
:
175 raise ExtractorError('This video is only available for premium users.', expected
=True)
177 if re
.search(r
'niedostępn[ey] w(?: |\s+)Twoim kraju\s*<', webpage
):
178 self
.raise_geo_restricted()
180 need_confirm_age
= False
181 if self
._html
_search
_regex
(r
'(<form[^>]+action="[^"]*/a/validatebirth[^"]*")',
182 webpage
, 'birthday validate form', default
=None):
183 webpage
= self
._download
_age
_confirm
_page
(
184 url
, video_id
, note
='Confirming age')
185 need_confirm_age
= True
189 uploader
= self
._search
_regex
(r
'''(?x)
190 <(span|meta)[^>]+itemprop=(["\'])author\
2[^
>]*>
191 (?
:<\
1[^
>]*>[^
<]*</\
1>|
(?
!</\
1>)(?
:.|
\n))*?
192 <(span|meta
)[^
>]+itemprop
=(["\'])name\4[^>]*>(?P<uploader>[^<]+)</\3>
193 ''', webpage, 'uploader', default=None, group='uploader')
194 view_count = self._search_regex(
195 r'Odsłony:(?:\s| )*([0-9]+)', webpage,
196 'view_count', default=None)
197 average_rating = self._search_regex(
198 (r'<(?:span|meta)[^>]+itemprop=(["\'])ratingValue\
1[^
>]*>(?P
<rating_value
>[0-9.]+)',
199 r'<span
[^
>]+\bclass
=["\']rating["\'][^
>]*>(?P
<rating_value
>[0-9.]+)'), webpage, 'rating
', fatal=False,
200 group='rating_value
')
204 'title
': self._og_search_title(webpage),
205 'description
': self._og_search_description(webpage),
206 'uploader
': uploader,
207 'view_count
': int_or_none(view_count),
208 'average_rating
': float_or_none(average_rating),
209 'thumbnail
': self._og_search_thumbnail(webpage),
212 'age_limit
': 18 if need_confirm_age else 0,
215 info = self._search_json_ld(webpage, video_id, default={})
217 # Source: https://www.cda.pl/js/player.js?t=1606154898
219 for p in ('_XDDD
', '_CDA
', '_ADC
', '_CXD
', '_QWE
', '_Q5
', '_IKSDE
'):
221 a = compat_urllib_parse_unquote(a)
225 b.append(chr(33 + (f + 14) % 94) if 33 <= f <= 126 else chr(f))
227 a = a.replace('.cda
.mp4
', '')
228 for p in ('.2cda
.pl
', '.3cda
.pl
'):
229 a = a.replace(p, '.cda
.pl
')
231 a = a.replace('/upstream
', '.mp4
/upstream
')
232 return 'https
://' + a
233 return 'https
://' + a + '.mp4
'
235 def extract_format(page, version):
236 json_str = self._html_search_regex(
237 r'player_data
=(\\?
["\'])(?P<player_data>.+?)\1', page,
238 '%s player_json' % version, fatal=False, group='player_data')
241 player_data = self._parse_json(
242 json_str, '%s player_data' % version, fatal=False)
245 video = player_data.get('video')
246 if not video or 'file' not in video:
247 self.report_warning('Unable to extract %s version information' % version)
249 if video['file'].startswith('uggc'):
250 video['file'] = codecs.decode(video['file'], 'rot_13')
251 if video['file'].endswith('adc.mp4'):
252 video['file'] = video['file'].replace('adc.mp4', '.mp4')
253 elif not video['file'].startswith('http'):
254 video['file'] = decrypt_file(video['file'])
255 video_quality = video.get('quality')
256 qualities = video.get('qualities', {})
257 video_quality = next((k for k, v in qualities.items() if v == video_quality), video_quality)
258 info_dict['formats'].append({
259 'url': video['file'],
260 'format_id': video_quality,
261 'height': int_or_none(video_quality[:-1]),
263 for quality, cda_quality in qualities.items():
264 if quality == video_quality:
266 data = {'jsonrpc': '2.0', 'method': 'videoGetLink', 'id': 2,
267 'params': [video_id, cda_quality, video.get('ts'), video.get('hash2'), {}]}
268 data = json.dumps(data).encode('utf-8')
269 video_url = self._download_json(
270 f'https://www.cda.pl/video/{video_id}', video_id, headers={
271 'Content-Type': 'application/json',
272 'X-Requested-With': 'XMLHttpRequest'
273 }, data=data, note=f'Fetching {quality} url',
274 errnote=f'Failed to fetch {quality} url', fatal=False)
275 if try_get(video_url, lambda x: x['result']['status']) == 'ok':
276 video_url = try_get(video_url, lambda x: x['result']['resp'])
277 info_dict['formats'].append({
279 'format_id': quality,
280 'height': int_or_none(quality[:-1])
283 if not info_dict['duration']:
284 info_dict['duration'] = parse_duration(video.get('duration'))
286 extract_format(webpage, 'default')
288 for href, resolution in re.findall(
289 r'<a[^>]+data-quality="[^
"]+"[^
>]+href
="([^"]+)"[^>]+class="quality
-btn
"[^>]*>([0-9]+p)',
292 handler = self._download_age_confirm_page
294 handler = self._download_webpage
297 urljoin(self._BASE_URL, href), video_id,
298 'Downloading %s version information' % resolution, fatal=False)
300 # Manually report warning because empty page is returned when
301 # invalid version is requested.
302 self.report_warning('Unable to download %s version information' % resolution)
305 extract_format(webpage, resolution)
307 self._sort_formats(formats)
309 return merge_dicts(info_dict, info)