]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/polsatgo.py
[misc] Add `hatch`, `ruff`, `pre-commit` and improve dev docs (#7409)
[yt-dlp.git] / yt_dlp / extractor / polsatgo.py
1 import json
2 import uuid
3
4 from .common import InfoExtractor
5 from ..utils import (
6 ExtractorError,
7 int_or_none,
8 try_get,
9 url_or_none,
10 )
11
12
13 class PolsatGoIE(InfoExtractor):
14 _VALID_URL = r'https?://(?:www\.)?polsat(?:box)?go\.pl/.+/(?P<id>[0-9a-fA-F]+)(?:[/#?]|$)'
15 _TESTS = [{
16 'url': 'https://polsatgo.pl/wideo/seriale/swiat-wedlug-kiepskich/5024045/sezon-1/5028300/swiat-wedlug-kiepskich-odcinek-88/4121',
17 'info_dict': {
18 'id': '4121',
19 'ext': 'mp4',
20 'title': 'Świat według Kiepskich - Odcinek 88',
21 'age_limit': 12,
22 },
23 }]
24
25 def _extract_formats(self, sources, video_id):
26 for source in sources or []:
27 if not source.get('id'):
28 continue
29 url = url_or_none(self._call_api(
30 'drm', video_id, 'getPseudoLicense',
31 {'mediaId': video_id, 'sourceId': source['id']}).get('url'))
32 if not url:
33 continue
34 yield {
35 'url': url,
36 'height': int_or_none(try_get(source, lambda x: x['quality'][:-1]))
37 }
38
39 def _real_extract(self, url):
40 video_id = self._match_id(url)
41 media = self._call_api('navigation', video_id, 'prePlayData', {'mediaId': video_id})['mediaItem']
42
43 formats = list(self._extract_formats(
44 try_get(media, lambda x: x['playback']['mediaSources']), video_id))
45
46 return {
47 'id': video_id,
48 'title': media['displayInfo']['title'],
49 'formats': formats,
50 'age_limit': int_or_none(media['displayInfo']['ageGroup'])
51 }
52
53 def _call_api(self, endpoint, media_id, method, params):
54 rand_uuid = str(uuid.uuid4())
55 res = self._download_json(
56 f'https://b2c-mobile.redefine.pl/rpc/{endpoint}/', media_id,
57 note=f'Downloading {method} JSON metadata',
58 data=json.dumps({
59 'method': method,
60 'id': '2137',
61 'jsonrpc': '2.0',
62 'params': {
63 **params,
64 'userAgentData': {
65 'deviceType': 'mobile',
66 'application': 'native',
67 'os': 'android',
68 'build': 10003,
69 'widevine': False,
70 'portal': 'pg',
71 'player': 'cpplayer',
72 },
73 'deviceId': {
74 'type': 'other',
75 'value': rand_uuid,
76 },
77 'clientId': rand_uuid,
78 'cpid': 1,
79 },
80 }).encode('utf-8'),
81 headers={'Content-type': 'application/json'})
82 if not res.get('result'):
83 if res['error']['code'] == 13404:
84 raise ExtractorError('This video is either unavailable in your region or is DRM protected', expected=True)
85 raise ExtractorError(f'Solorz said: {res["error"]["message"]} - {res["error"]["data"]["userMessage"]}')
86 return res['result']