caracoltv.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import base64
  2. import json
  3. import uuid
  4. from .common import InfoExtractor
  5. from ..utils import (
  6. int_or_none,
  7. js_to_json,
  8. traverse_obj,
  9. urljoin,
  10. )
  11. class CaracolTvPlayIE(InfoExtractor):
  12. _VALID_URL = r'https?://play\.caracoltv\.com/videoDetails/(?P<id>[^/?#]+)'
  13. _NETRC_MACHINE = 'caracoltv-play'
  14. _TESTS = [{
  15. 'url': 'https://play.caracoltv.com/videoDetails/OTo4NGFmNjUwOWQ2ZmM0NTg2YWRiOWU0MGNhOWViOWJkYQ==',
  16. 'info_dict': {
  17. 'id': 'OTo4NGFmNjUwOWQ2ZmM0NTg2YWRiOWU0MGNhOWViOWJkYQ==',
  18. 'title': 'La teoría del promedio',
  19. 'description': 'md5:1cdd6d2c13f19ef0d9649ab81a023ac3',
  20. },
  21. 'playlist_count': 6,
  22. }, {
  23. 'url': 'https://play.caracoltv.com/videoDetails/OTo3OWM4ZTliYzQxMmM0MTMxYTk4Mjk2YjdjNGQ4NGRkOQ==/ella?season=0',
  24. 'info_dict': {
  25. 'id': 'OTo3OWM4ZTliYzQxMmM0MTMxYTk4Mjk2YjdjNGQ4NGRkOQ==',
  26. 'title': 'Ella',
  27. 'description': 'md5:a639b1feb5ddcc0cff92a489b4e544b8',
  28. },
  29. 'playlist_count': 10,
  30. }, {
  31. 'url': 'https://play.caracoltv.com/videoDetails/OTpiYTY1YTVmOTI5MzI0ZWJhOGZiY2Y3MmRlOWZlYmJkOA==/la-vuelta-al-mundo-en-80-risas-2022?season=0',
  32. 'info_dict': {
  33. 'id': 'OTpiYTY1YTVmOTI5MzI0ZWJhOGZiY2Y3MmRlOWZlYmJkOA==',
  34. 'title': 'La vuelta al mundo en 80 risas 2022',
  35. 'description': 'md5:e97aac36106e5c37ebf947b3350106a4',
  36. },
  37. 'playlist_count': 17,
  38. }, {
  39. 'url': 'https://play.caracoltv.com/videoDetails/MzoxX3BwbjRmNjB1',
  40. 'only_matching': True,
  41. }]
  42. _USER_TOKEN = None
  43. def _extract_app_token(self, webpage):
  44. config_js_path = self._search_regex(
  45. r'<script[^>]+src\s*=\s*"([^"]+coreConfig.js[^"]+)', webpage, 'config js url', fatal=False)
  46. mediation_config = {} if not config_js_path else self._search_json(
  47. r'mediation\s*:', self._download_webpage(
  48. urljoin('https://play.caracoltv.com/', config_js_path), None, fatal=False, note='Extracting JS config'),
  49. 'mediation_config', None, transform_source=js_to_json, fatal=False)
  50. key = traverse_obj(
  51. mediation_config, ('live', 'key')) or '795cd9c089a1fc48094524a5eba85a3fca1331817c802f601735907c8bbb4f50'
  52. secret = traverse_obj(
  53. mediation_config, ('live', 'secret')) or '64dec00a6989ba83d087621465b5e5d38bdac22033b0613b659c442c78976fa0'
  54. return base64.b64encode(f'{key}:{secret}'.encode()).decode()
  55. def _perform_login(self, email, password):
  56. webpage = self._download_webpage('https://play.caracoltv.com/', None, fatal=False)
  57. app_token = self._extract_app_token(webpage)
  58. bearer_token = self._download_json(
  59. 'https://eu-gateway.inmobly.com/applications/oauth', None, data=b'', note='Retrieving bearer token',
  60. headers={'Authorization': f'Basic {app_token}'})['token']
  61. self._USER_TOKEN = self._download_json(
  62. 'https://eu-gateway.inmobly.com/user/login', None, note='Performing login', headers={
  63. 'Content-Type': 'application/json',
  64. 'Authorization': f'Bearer {bearer_token}',
  65. }, data=json.dumps({
  66. 'device_data': {
  67. 'device_id': str(uuid.uuid4()),
  68. 'device_token': '',
  69. 'device_type': 'web',
  70. },
  71. 'login_data': {
  72. 'enabled': True,
  73. 'email': email,
  74. 'password': password,
  75. },
  76. }).encode())['user_token']
  77. def _extract_video(self, video_data, series_id=None, season_id=None, season_number=None):
  78. formats, subtitles = self._extract_m3u8_formats_and_subtitles(video_data['stream_url'], series_id, 'mp4')
  79. return {
  80. 'id': video_data['id'],
  81. 'title': video_data.get('name'),
  82. 'description': video_data.get('description'),
  83. 'formats': formats,
  84. 'subtitles': subtitles,
  85. 'thumbnails': traverse_obj(
  86. video_data, ('extra_thumbs', ..., {'url': 'thumb_url', 'height': 'height', 'width': 'width'})),
  87. 'series_id': series_id,
  88. 'season_id': season_id,
  89. 'season_number': int_or_none(season_number),
  90. 'episode_number': int_or_none(video_data.get('item_order')),
  91. 'is_live': video_data.get('entry_type') == 3,
  92. }
  93. def _extract_series_seasons(self, seasons, series_id):
  94. for season in seasons:
  95. api_response = self._download_json(
  96. 'https://eu-gateway.inmobly.com/feed', series_id, query={'season_id': season['id']},
  97. headers={'Authorization': f'Bearer {self._USER_TOKEN}'})
  98. season_number = season.get('order')
  99. for episode in api_response['items']:
  100. yield self._extract_video(episode, series_id, season['id'], season_number)
  101. def _real_extract(self, url):
  102. series_id = self._match_id(url)
  103. if self._USER_TOKEN is None:
  104. self._perform_login('guest@inmobly.com', 'Test@gus1')
  105. api_response = self._download_json(
  106. 'https://eu-gateway.inmobly.com/feed', series_id, query={'include_ids': series_id},
  107. headers={'Authorization': f'Bearer {self._USER_TOKEN}'})['items'][0]
  108. if not api_response.get('seasons'):
  109. return self._extract_video(api_response)
  110. return self.playlist_result(
  111. self._extract_series_seasons(api_response['seasons'], series_id),
  112. series_id, **traverse_obj(api_response, {
  113. 'title': 'name',
  114. 'description': 'description',
  115. }))