polsatgo.py 3.1 KB

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