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