]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/medialaan.py
New upstream version 2017.11.06
[youtubedl] / youtube_dl / extractor / medialaan.py
1 from __future__ import unicode_literals
2
3 import re
4
5 from .gigya import GigyaBaseIE
6
7 from ..compat import compat_str
8 from ..utils import (
9 int_or_none,
10 parse_duration,
11 try_get,
12 unified_timestamp,
13 )
14
15
16 class MedialaanIE(GigyaBaseIE):
17 _VALID_URL = r'''(?x)
18 https?://
19 (?:www\.|nieuws\.)?
20 (?:
21 (?P<site_id>vtm|q2|vtmkzoom)\.be/
22 (?:
23 video(?:/[^/]+/id/|/?\?.*?\baid=)|
24 (?:[^/]+/)*
25 )
26 )
27 (?P<id>[^/?#&]+)
28 '''
29 _NETRC_MACHINE = 'medialaan'
30 _APIKEY = '3_HZ0FtkMW_gOyKlqQzW5_0FHRC7Nd5XpXJZcDdXY4pk5eES2ZWmejRW5egwVm4ug-'
31 _SITE_TO_APP_ID = {
32 'vtm': 'vtm_watch',
33 'q2': 'q2',
34 'vtmkzoom': 'vtmkzoom',
35 }
36 _TESTS = [{
37 # vod
38 'url': 'http://vtm.be/video/volledige-afleveringen/id/vtm_20170219_VM0678361_vtmwatch',
39 'info_dict': {
40 'id': 'vtm_20170219_VM0678361_vtmwatch',
41 'ext': 'mp4',
42 'title': 'Allemaal Chris afl. 6',
43 'description': 'md5:4be86427521e7b07e0adb0c9c554ddb2',
44 'timestamp': 1487533280,
45 'upload_date': '20170219',
46 'duration': 2562,
47 'series': 'Allemaal Chris',
48 'season': 'Allemaal Chris',
49 'season_number': 1,
50 'season_id': '256936078124527',
51 'episode': 'Allemaal Chris afl. 6',
52 'episode_number': 6,
53 'episode_id': '256936078591527',
54 },
55 'params': {
56 'skip_download': True,
57 },
58 'skip': 'Requires account credentials',
59 }, {
60 # clip
61 'url': 'http://vtm.be/video?aid=168332',
62 'info_dict': {
63 'id': '168332',
64 'ext': 'mp4',
65 'title': '"Veronique liegt!"',
66 'description': 'md5:1385e2b743923afe54ba4adc38476155',
67 'timestamp': 1489002029,
68 'upload_date': '20170308',
69 'duration': 96,
70 },
71 }, {
72 # vod
73 'url': 'http://vtm.be/video/volledige-afleveringen/id/257107153551000',
74 'only_matching': True,
75 }, {
76 # vod
77 'url': 'http://vtm.be/video?aid=163157',
78 'only_matching': True,
79 }, {
80 # vod
81 'url': 'http://www.q2.be/video/volledige-afleveringen/id/2be_20170301_VM0684442_q2',
82 'only_matching': True,
83 }, {
84 # clip
85 'url': 'http://vtmkzoom.be/k3-dansstudio/een-nieuw-seizoen-van-k3-dansstudio',
86 'only_matching': True,
87 }, {
88 # http/s redirect
89 'url': 'https://vtmkzoom.be/video?aid=45724',
90 'info_dict': {
91 'id': '257136373657000',
92 'ext': 'mp4',
93 'title': 'K3 Dansstudio Ushuaia afl.6',
94 },
95 'params': {
96 'skip_download': True,
97 },
98 'skip': 'Requires account credentials',
99 }, {
100 # nieuws.vtm.be
101 'url': 'https://nieuws.vtm.be/stadion/stadion/genk-nog-moeilijk-programma',
102 'only_matching': True,
103 }]
104
105 def _real_initialize(self):
106 self._logged_in = False
107
108 def _login(self):
109 username, password = self._get_login_info()
110 if username is None:
111 self.raise_login_required()
112
113 auth_data = {
114 'APIKey': self._APIKEY,
115 'sdk': 'js_6.1',
116 'format': 'json',
117 'loginID': username,
118 'password': password,
119 }
120
121 auth_info = self._gigya_login(auth_data)
122
123 self._uid = auth_info['UID']
124 self._uid_signature = auth_info['UIDSignature']
125 self._signature_timestamp = auth_info['signatureTimestamp']
126
127 self._logged_in = True
128
129 def _real_extract(self, url):
130 mobj = re.match(self._VALID_URL, url)
131 video_id, site_id = mobj.group('id', 'site_id')
132
133 webpage = self._download_webpage(url, video_id)
134
135 config = self._parse_json(
136 self._search_regex(
137 r'videoJSConfig\s*=\s*JSON\.parse\(\'({.+?})\'\);',
138 webpage, 'config', default='{}'), video_id,
139 transform_source=lambda s: s.replace(
140 '\\\\', '\\').replace(r'\"', '"').replace(r"\'", "'"))
141
142 vod_id = config.get('vodId') or self._search_regex(
143 (r'\\"vodId\\"\s*:\s*\\"(.+?)\\"',
144 r'<[^>]+id=["\']vod-(\d+)'),
145 webpage, 'video_id', default=None)
146
147 # clip, no authentication required
148 if not vod_id:
149 player = self._parse_json(
150 self._search_regex(
151 r'vmmaplayer\(({.+?})\);', webpage, 'vmma player',
152 default=''),
153 video_id, transform_source=lambda s: '[%s]' % s, fatal=False)
154 if player:
155 video = player[-1]
156 if video['videoUrl'] in ('http', 'https'):
157 return self.url_result(video['url'], MedialaanIE.ie_key())
158 info = {
159 'id': video_id,
160 'url': video['videoUrl'],
161 'title': video['title'],
162 'thumbnail': video.get('imageUrl'),
163 'timestamp': int_or_none(video.get('createdDate')),
164 'duration': int_or_none(video.get('duration')),
165 }
166 else:
167 info = self._parse_html5_media_entries(
168 url, webpage, video_id, m3u8_id='hls')[0]
169 info.update({
170 'id': video_id,
171 'title': self._html_search_meta('description', webpage),
172 'duration': parse_duration(self._html_search_meta('duration', webpage)),
173 })
174 # vod, authentication required
175 else:
176 if not self._logged_in:
177 self._login()
178
179 settings = self._parse_json(
180 self._search_regex(
181 r'jQuery\.extend\(Drupal\.settings\s*,\s*({.+?})\);',
182 webpage, 'drupal settings', default='{}'),
183 video_id)
184
185 def get(container, item):
186 return try_get(
187 settings, lambda x: x[container][item],
188 compat_str) or self._search_regex(
189 r'"%s"\s*:\s*"([^"]+)' % item, webpage, item,
190 default=None)
191
192 app_id = get('vod', 'app_id') or self._SITE_TO_APP_ID.get(site_id, 'vtm_watch')
193 sso = get('vod', 'gigyaDatabase') or 'vtm-sso'
194
195 data = self._download_json(
196 'http://vod.medialaan.io/api/1.0/item/%s/video' % vod_id,
197 video_id, query={
198 'app_id': app_id,
199 'user_network': sso,
200 'UID': self._uid,
201 'UIDSignature': self._uid_signature,
202 'signatureTimestamp': self._signature_timestamp,
203 })
204
205 formats = self._extract_m3u8_formats(
206 data['response']['uri'], video_id, entry_protocol='m3u8_native',
207 ext='mp4', m3u8_id='hls')
208
209 self._sort_formats(formats)
210
211 info = {
212 'id': vod_id,
213 'formats': formats,
214 }
215
216 api_key = get('vod', 'apiKey')
217 channel = get('medialaanGigya', 'channel')
218
219 if api_key:
220 videos = self._download_json(
221 'http://vod.medialaan.io/vod/v2/videos', video_id, fatal=False,
222 query={
223 'channels': channel,
224 'ids': vod_id,
225 'limit': 1,
226 'apikey': api_key,
227 })
228 if videos:
229 video = try_get(
230 videos, lambda x: x['response']['videos'][0], dict)
231 if video:
232 def get(container, item, expected_type=None):
233 return try_get(
234 video, lambda x: x[container][item], expected_type)
235
236 def get_string(container, item):
237 return get(container, item, compat_str)
238
239 info.update({
240 'series': get_string('program', 'title'),
241 'season': get_string('season', 'title'),
242 'season_number': int_or_none(get('season', 'number')),
243 'season_id': get_string('season', 'id'),
244 'episode': get_string('episode', 'title'),
245 'episode_number': int_or_none(get('episode', 'number')),
246 'episode_id': get_string('episode', 'id'),
247 'duration': int_or_none(
248 video.get('duration')) or int_or_none(
249 video.get('durationMillis'), scale=1000),
250 'title': get_string('episode', 'title'),
251 'description': get_string('episode', 'text'),
252 'timestamp': unified_timestamp(get_string(
253 'publication', 'begin')),
254 })
255
256 if not info.get('title'):
257 info['title'] = try_get(
258 config, lambda x: x['videoConfig']['title'],
259 compat_str) or self._html_search_regex(
260 r'\\"title\\"\s*:\s*\\"(.+?)\\"', webpage, 'title',
261 default=None) or self._og_search_title(webpage)
262
263 if not info.get('description'):
264 info['description'] = self._html_search_regex(
265 r'<div[^>]+class="field-item\s+even">\s*<p>(.+?)</p>',
266 webpage, 'description', default=None)
267
268 return info