2019-12-04 17:20:53 +01:00
|
|
|
# coding: utf-8
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
|
|
|
import json
|
|
|
|
|
|
|
|
from .common import InfoExtractor
|
|
|
|
from ..compat import compat_HTTPError
|
|
|
|
from ..utils import (
|
|
|
|
ExtractorError,
|
|
|
|
int_or_none,
|
2019-12-04 20:56:23 +01:00
|
|
|
str_or_none,
|
2019-12-04 17:20:53 +01:00
|
|
|
try_get,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class ImgGamingBaseIE(InfoExtractor):
|
|
|
|
_API_BASE = 'https://dce-frontoffice.imggaming.com/api/v2/'
|
|
|
|
_API_KEY = '857a1e5d-e35e-4fdf-805b-a87b6f8364bf'
|
|
|
|
_HEADERS = None
|
|
|
|
_MANIFEST_HEADERS = {'Accept-Encoding': 'identity'}
|
|
|
|
_REALM = None
|
2019-12-07 19:23:19 +01:00
|
|
|
_VALID_URL_TEMPL = r'https?://(?P<domain>%s)/(?P<type>live|playlist|video)/(?P<id>\d+)(?:\?.*?\bplaylistId=(?P<playlist_id>\d+))?'
|
2019-12-04 17:20:53 +01:00
|
|
|
|
2022-03-18 21:53:33 +01:00
|
|
|
def _initialize_pre_login(self):
|
2019-12-04 17:20:53 +01:00
|
|
|
self._HEADERS = {
|
|
|
|
'Realm': 'dce.' + self._REALM,
|
|
|
|
'x-api-key': self._API_KEY,
|
|
|
|
}
|
|
|
|
|
2022-03-18 21:53:33 +01:00
|
|
|
def _perform_login(self, username, password):
|
2019-12-04 17:20:53 +01:00
|
|
|
p_headers = self._HEADERS.copy()
|
|
|
|
p_headers['Content-Type'] = 'application/json'
|
|
|
|
self._HEADERS['Authorization'] = 'Bearer ' + self._download_json(
|
2019-12-06 11:04:12 +01:00
|
|
|
self._API_BASE + 'login',
|
2019-12-04 17:20:53 +01:00
|
|
|
None, 'Logging in', data=json.dumps({
|
2022-03-18 21:53:33 +01:00
|
|
|
'id': username,
|
2019-12-04 17:20:53 +01:00
|
|
|
'secret': password,
|
|
|
|
}).encode(), headers=p_headers)['authorisationToken']
|
|
|
|
|
2022-03-18 21:53:33 +01:00
|
|
|
def _real_initialize(self):
|
|
|
|
if not self._HEADERS.get('Authorization'):
|
|
|
|
self.raise_login_required(method='password')
|
|
|
|
|
2019-12-04 20:56:23 +01:00
|
|
|
def _call_api(self, path, media_id):
|
|
|
|
return self._download_json(
|
|
|
|
self._API_BASE + path + media_id, media_id, headers=self._HEADERS)
|
|
|
|
|
2019-12-04 17:20:53 +01:00
|
|
|
def _extract_dve_api_url(self, media_id, media_type):
|
2019-12-04 20:56:23 +01:00
|
|
|
stream_path = 'stream'
|
2019-12-04 17:20:53 +01:00
|
|
|
if media_type == 'video':
|
2019-12-04 20:56:23 +01:00
|
|
|
stream_path += '/vod/'
|
2019-12-04 17:20:53 +01:00
|
|
|
else:
|
2019-12-04 20:56:23 +01:00
|
|
|
stream_path += '?eventId='
|
2019-12-04 17:20:53 +01:00
|
|
|
try:
|
2019-12-04 20:56:23 +01:00
|
|
|
return self._call_api(
|
|
|
|
stream_path, media_id)['playerUrlCallback']
|
2019-12-04 17:20:53 +01:00
|
|
|
except ExtractorError as e:
|
|
|
|
if isinstance(e.cause, compat_HTTPError) and e.cause.code == 403:
|
|
|
|
raise ExtractorError(
|
|
|
|
self._parse_json(e.cause.read().decode(), media_id)['messages'][0],
|
|
|
|
expected=True)
|
|
|
|
raise
|
|
|
|
|
|
|
|
def _real_extract(self, url):
|
2021-08-19 03:41:24 +02:00
|
|
|
domain, media_type, media_id, playlist_id = self._match_valid_url(url).groups()
|
2019-12-04 20:56:23 +01:00
|
|
|
|
|
|
|
if playlist_id:
|
2022-01-23 18:54:24 +01:00
|
|
|
if self._yes_playlist(playlist_id, media_id):
|
2019-12-04 20:56:23 +01:00
|
|
|
media_type, media_id = 'playlist', playlist_id
|
|
|
|
|
|
|
|
if media_type == 'playlist':
|
|
|
|
playlist = self._call_api('vod/playlist/', media_id)
|
|
|
|
entries = []
|
|
|
|
for video in try_get(playlist, lambda x: x['videos']['vods']) or []:
|
|
|
|
video_id = str_or_none(video.get('id'))
|
|
|
|
if not video_id:
|
|
|
|
continue
|
|
|
|
entries.append(self.url_result(
|
2019-12-06 11:04:12 +01:00
|
|
|
'https://%s/video/%s' % (domain, video_id),
|
2019-12-04 20:56:23 +01:00
|
|
|
self.ie_key(), video_id))
|
|
|
|
return self.playlist_result(
|
|
|
|
entries, media_id, playlist.get('title'),
|
|
|
|
playlist.get('description'))
|
|
|
|
|
2019-12-04 17:20:53 +01:00
|
|
|
dve_api_url = self._extract_dve_api_url(media_id, media_type)
|
|
|
|
video_data = self._download_json(dve_api_url, media_id)
|
|
|
|
is_live = media_type == 'live'
|
|
|
|
if is_live:
|
2021-12-15 17:00:46 +01:00
|
|
|
title = self._call_api('event/', media_id)['title']
|
2019-12-04 17:20:53 +01:00
|
|
|
else:
|
|
|
|
title = video_data['name']
|
|
|
|
|
|
|
|
formats = []
|
|
|
|
for proto in ('hls', 'dash'):
|
|
|
|
media_url = video_data.get(proto + 'Url') or try_get(video_data, lambda x: x[proto]['url'])
|
|
|
|
if not media_url:
|
|
|
|
continue
|
|
|
|
if proto == 'hls':
|
|
|
|
m3u8_formats = self._extract_m3u8_formats(
|
2022-03-02 18:29:01 +01:00
|
|
|
media_url, media_id, 'mp4', live=is_live,
|
2019-12-04 17:20:53 +01:00
|
|
|
m3u8_id='hls', fatal=False, headers=self._MANIFEST_HEADERS)
|
|
|
|
for f in m3u8_formats:
|
|
|
|
f.setdefault('http_headers', {}).update(self._MANIFEST_HEADERS)
|
|
|
|
formats.append(f)
|
|
|
|
else:
|
|
|
|
formats.extend(self._extract_mpd_formats(
|
|
|
|
media_url, media_id, mpd_id='dash', fatal=False,
|
|
|
|
headers=self._MANIFEST_HEADERS))
|
|
|
|
self._sort_formats(formats)
|
|
|
|
|
2019-12-04 20:56:23 +01:00
|
|
|
subtitles = {}
|
|
|
|
for subtitle in video_data.get('subtitles', []):
|
|
|
|
subtitle_url = subtitle.get('url')
|
|
|
|
if not subtitle_url:
|
|
|
|
continue
|
|
|
|
subtitles.setdefault(subtitle.get('lang', 'en_US'), []).append({
|
|
|
|
'url': subtitle_url,
|
|
|
|
})
|
|
|
|
|
2019-12-04 17:20:53 +01:00
|
|
|
return {
|
|
|
|
'id': media_id,
|
|
|
|
'title': title,
|
|
|
|
'formats': formats,
|
|
|
|
'thumbnail': video_data.get('thumbnailUrl'),
|
|
|
|
'description': video_data.get('description'),
|
|
|
|
'duration': int_or_none(video_data.get('duration')),
|
|
|
|
'tags': video_data.get('tags'),
|
|
|
|
'is_live': is_live,
|
2019-12-04 20:56:23 +01:00
|
|
|
'subtitles': subtitles,
|
2019-12-04 17:20:53 +01:00
|
|
|
}
|