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