[medialaan] Fix and improve extraction (closes #11912)
[youtube-dl] / 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\.)?
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://vitaya.be/de-jurk/precies-je-hebt-geen-borsten',
87         'only_matching': True,
88     }, {
89         # clip
90         'url': 'http://vtmkzoom.be/k3-dansstudio/een-nieuw-seizoen-van-k3-dansstudio',
91         'only_matching': True,
92     }]
93
94     def _real_initialize(self):
95         self._logged_in = False
96
97     def _login(self):
98         username, password = self._get_login_info()
99         if username is None:
100             self.raise_login_required()
101
102         auth_data = {
103             'APIKey': self._APIKEY,
104             'sdk': 'js_6.1',
105             'format': 'json',
106             'loginID': username,
107             'password': password,
108         }
109
110         auth_info = self._download_json(
111             'https://accounts.eu1.gigya.com/accounts.login', None,
112             note='Logging in', errnote='Unable to log in',
113             data=urlencode_postdata(auth_data))
114
115         error_message = auth_info.get('errorDetails') or auth_info.get('errorMessage')
116         if error_message:
117             raise ExtractorError(
118                 'Unable to login: %s' % error_message, expected=True)
119
120         self._uid = auth_info['UID']
121         self._uid_signature = auth_info['UIDSignature']
122         self._signature_timestamp = auth_info['signatureTimestamp']
123
124         self._logged_in = True
125
126     def _real_extract(self, url):
127         mobj = re.match(self._VALID_URL, url)
128         video_id, site_id = mobj.group('id', 'site_id')
129
130         webpage = self._download_webpage(url, video_id)
131
132         config = self._parse_json(
133             self._search_regex(
134                 r'videoJSConfig\s*=\s*JSON\.parse\(\'({.+?})\'\);',
135                 webpage, 'config', default='{}'), video_id,
136             transform_source=lambda s: s.replace(
137                 '\\\\', '\\').replace(r'\"', '"').replace(r"\'", "'"))
138
139         vod_id = config.get('vodId') or self._search_regex(
140             (r'\\"vodId\\"\s*:\s*\\"(.+?)\\"',
141              r'<[^>]+id=["\']vod-(\d+)'),
142             webpage, 'video_id', default=None)
143
144         # clip, no authentication required
145         if not vod_id:
146             player = self._parse_json(
147                 self._search_regex(
148                     r'vmmaplayer\(({.+?})\);', webpage, 'vmma player',
149                     default=''),
150                 video_id, transform_source=lambda s: '[%s]' % s, fatal=False)
151             if player:
152                 video = player[-1]
153                 info = {
154                     'id': video_id,
155                     'url': video['videoUrl'],
156                     'title': video['title'],
157                     'thumbnail': video.get('imageUrl'),
158                     'timestamp': int_or_none(video.get('createdDate')),
159                     'duration': int_or_none(video.get('duration')),
160                 }
161             else:
162                 info = self._parse_html5_media_entries(
163                     url, webpage, video_id, m3u8_id='hls')[0]
164                 info.update({
165                     'id': video_id,
166                     'title': self._html_search_meta('description', webpage),
167                     'duration': parse_duration(self._html_search_meta('duration', webpage)),
168                 })
169         # vod, authentication required
170         else:
171             if not self._logged_in:
172                 self._login()
173
174             settings = self._parse_json(
175                 self._search_regex(
176                     r'jQuery\.extend\(Drupal\.settings\s*,\s*({.+?})\);',
177                     webpage, 'drupal settings', default='{}'),
178                 video_id)
179
180             def get(container, item):
181                 return try_get(
182                     settings, lambda x: x[container][item],
183                     compat_str) or self._search_regex(
184                     r'"%s"\s*:\s*"([^"]+)' % item, webpage, item,
185                     default=None)
186
187             app_id = get('vod', 'app_id') or self._SITE_TO_APP_ID.get(site_id, 'vtm_watch')
188             sso = get('vod', 'gigyaDatabase') or 'vtm-sso'
189
190             data = self._download_json(
191                 'http://vod.medialaan.io/api/1.0/item/%s/video' % vod_id,
192                 video_id, query={
193                     'app_id': app_id,
194                     'user_network': sso,
195                     'UID': self._uid,
196                     'UIDSignature': self._uid_signature,
197                     'signatureTimestamp': self._signature_timestamp,
198                 })
199
200             formats = self._extract_m3u8_formats(
201                 data['response']['uri'], video_id, entry_protocol='m3u8_native',
202                 ext='mp4', m3u8_id='hls')
203
204             self._sort_formats(formats)
205
206             info = {
207                 'id': vod_id,
208                 'formats': formats,
209             }
210
211             api_key = get('vod', 'apiKey')
212             channel = get('medialaanGigya', 'channel')
213
214             if api_key:
215                 videos = self._download_json(
216                     'http://vod.medialaan.io/vod/v2/videos', video_id, fatal=False,
217                     query={
218                         'channels': channel,
219                         'ids': vod_id,
220                         'limit': 1,
221                         'apikey': api_key,
222                     })
223                 if videos:
224                     video = try_get(
225                         videos, lambda x: x['response']['videos'][0], dict)
226                     if video:
227                         def get(container, item, expected_type=None):
228                             return try_get(
229                                 video, lambda x: x[container][item], expected_type)
230
231                         def get_string(container, item):
232                             return get(container, item, compat_str)
233
234                         info.update({
235                             'series': get_string('program', 'title'),
236                             'season': get_string('season', 'title'),
237                             'season_number': int_or_none(get('season', 'number')),
238                             'season_id': get_string('season', 'id'),
239                             'episode': get_string('episode', 'title'),
240                             'episode_number': int_or_none(get('episode', 'number')),
241                             'episode_id': get_string('episode', 'id'),
242                             'duration': int_or_none(
243                                 video.get('duration')) or int_or_none(
244                                 video.get('durationMillis'), scale=1000),
245                             'title': get_string('episode', 'title'),
246                             'description': get_string('episode', 'text'),
247                             'timestamp': unified_timestamp(get_string(
248                                 'publication', 'begin')),
249                         })
250
251             if not info.get('title'):
252                 info['title'] = try_get(
253                     config, lambda x: x['videoConfig']['title'],
254                     compat_str) or self._html_search_regex(
255                     r'\\"title\\"\s*:\s*\\"(.+?)\\"', webpage, 'title',
256                     default=None) or self._og_search_title(webpage)
257
258         if not info.get('description'):
259             info['description'] = self._html_search_regex(
260                 r'<div[^>]+class="field-item\s+even">\s*<p>(.+?)</p>',
261                 webpage, 'description', default=None)
262
263         return info