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