imggaming.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import json
  2. from .common import InfoExtractor
  3. from ..networking.exceptions import HTTPError
  4. from ..utils import (
  5. ExtractorError,
  6. int_or_none,
  7. str_or_none,
  8. try_get,
  9. )
  10. class ImgGamingBaseIE(InfoExtractor):
  11. _API_BASE = 'https://dce-frontoffice.imggaming.com/api/v2/'
  12. _API_KEY = '857a1e5d-e35e-4fdf-805b-a87b6f8364bf'
  13. _HEADERS = None
  14. _MANIFEST_HEADERS = {'Accept-Encoding': 'identity'}
  15. _REALM = None
  16. _VALID_URL_TEMPL = r'https?://(?P<domain>%s)/(?P<type>live|playlist|video)/(?P<id>\d+)(?:\?.*?\bplaylistId=(?P<playlist_id>\d+))?'
  17. def _initialize_pre_login(self):
  18. self._HEADERS = {
  19. 'Realm': 'dce.' + self._REALM,
  20. 'x-api-key': self._API_KEY,
  21. }
  22. def _perform_login(self, username, password):
  23. p_headers = self._HEADERS.copy()
  24. p_headers['Content-Type'] = 'application/json'
  25. self._HEADERS['Authorization'] = 'Bearer ' + self._download_json(
  26. self._API_BASE + 'login',
  27. None, 'Logging in', data=json.dumps({
  28. 'id': username,
  29. 'secret': password,
  30. }).encode(), headers=p_headers)['authorisationToken']
  31. def _real_initialize(self):
  32. if not self._HEADERS.get('Authorization'):
  33. self.raise_login_required(method='password')
  34. def _call_api(self, path, media_id):
  35. return self._download_json(
  36. self._API_BASE + path + media_id, media_id, headers=self._HEADERS)
  37. def _extract_dve_api_url(self, media_id, media_type):
  38. stream_path = 'stream'
  39. if media_type == 'video':
  40. stream_path += '/vod/'
  41. else:
  42. stream_path += '?eventId='
  43. try:
  44. return self._call_api(
  45. stream_path, media_id)['playerUrlCallback']
  46. except ExtractorError as e:
  47. if isinstance(e.cause, HTTPError) and e.cause.status == 403:
  48. raise ExtractorError(
  49. self._parse_json(e.cause.response.read().decode(), media_id)['messages'][0],
  50. expected=True)
  51. raise
  52. def _real_extract(self, url):
  53. domain, media_type, media_id, playlist_id = self._match_valid_url(url).groups()
  54. if playlist_id:
  55. if self._yes_playlist(playlist_id, media_id):
  56. media_type, media_id = 'playlist', playlist_id
  57. if media_type == 'playlist':
  58. playlist = self._call_api('vod/playlist/', media_id)
  59. entries = []
  60. for video in try_get(playlist, lambda x: x['videos']['vods']) or []:
  61. video_id = str_or_none(video.get('id'))
  62. if not video_id:
  63. continue
  64. entries.append(self.url_result(
  65. f'https://{domain}/video/{video_id}',
  66. self.ie_key(), video_id))
  67. return self.playlist_result(
  68. entries, media_id, playlist.get('title'),
  69. playlist.get('description'))
  70. dve_api_url = self._extract_dve_api_url(media_id, media_type)
  71. video_data = self._download_json(dve_api_url, media_id)
  72. is_live = media_type == 'live'
  73. if is_live:
  74. title = self._call_api('event/', media_id)['title']
  75. else:
  76. title = video_data['name']
  77. formats = []
  78. for proto in ('hls', 'dash'):
  79. media_url = video_data.get(proto + 'Url') or try_get(video_data, lambda x: x[proto]['url'])
  80. if not media_url:
  81. continue
  82. if proto == 'hls':
  83. m3u8_formats = self._extract_m3u8_formats(
  84. media_url, media_id, 'mp4', live=is_live,
  85. m3u8_id='hls', fatal=False, headers=self._MANIFEST_HEADERS)
  86. for f in m3u8_formats:
  87. f.setdefault('http_headers', {}).update(self._MANIFEST_HEADERS)
  88. formats.append(f)
  89. else:
  90. formats.extend(self._extract_mpd_formats(
  91. media_url, media_id, mpd_id='dash', fatal=False,
  92. headers=self._MANIFEST_HEADERS))
  93. subtitles = {}
  94. for subtitle in video_data.get('subtitles', []):
  95. subtitle_url = subtitle.get('url')
  96. if not subtitle_url:
  97. continue
  98. subtitles.setdefault(subtitle.get('lang', 'en_US'), []).append({
  99. 'url': subtitle_url,
  100. })
  101. return {
  102. 'id': media_id,
  103. 'title': title,
  104. 'formats': formats,
  105. 'thumbnail': video_data.get('thumbnailUrl'),
  106. 'description': video_data.get('description'),
  107. 'duration': int_or_none(video_data.get('duration')),
  108. 'tags': video_data.get('tags'),
  109. 'is_live': is_live,
  110. 'subtitles': subtitles,
  111. }