]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/polsatgo.py
[extractor] Fix bug in 617f658b7ec1193749848c1b7343acab125dbc46
[yt-dlp.git] / yt_dlp / extractor / polsatgo.py
1 from uuid import uuid4
2 import json
3
4 from .common import InfoExtractor
5 from ..utils import (
6 int_or_none,
7 try_get,
8 url_or_none,
9 ExtractorError,
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 self._sort_formats(formats)
46
47 return {
48 'id': video_id,
49 'title': media['displayInfo']['title'],
50 'formats': formats,
51 'age_limit': int_or_none(media['displayInfo']['ageGroup'])
52 }
53
54 def _call_api(self, endpoint, media_id, method, params):
55 rand_uuid = str(uuid4())
56 res = self._download_json(
57 f'https://b2c-mobile.redefine.pl/rpc/{endpoint}/', media_id,
58 note=f'Downloading {method} JSON metadata',
59 data=json.dumps({
60 'method': method,
61 'id': '2137',
62 'jsonrpc': '2.0',
63 'params': {
64 **params,
65 'userAgentData': {
66 'deviceType': 'mobile',
67 'application': 'native',
68 'os': 'android',
69 'build': 10003,
70 'widevine': False,
71 'portal': 'pg',
72 'player': 'cpplayer',
73 },
74 'deviceId': {
75 'type': 'other',
76 'value': rand_uuid,
77 },
78 'clientId': rand_uuid,
79 'cpid': 1,
80 },
81 }).encode('utf-8'),
82 headers={'Content-type': 'application/json'})
83 if not res.get('result'):
84 if res['error']['code'] == 13404:
85 raise ExtractorError('This video is either unavailable in your region or is DRM protected', expected=True)
86 raise ExtractorError(f'Solorz said: {res["error"]["message"]} - {res["error"]["data"]["userMessage"]}')
87 return res['result']