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