[viafree] Add extractor (Closes #10358)
[youtube-dl] / youtube_dl / extractor / tvplay.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .common import InfoExtractor
7 from ..compat import (
8     compat_HTTPError,
9     compat_str,
10     compat_urlparse,
11 )
12 from ..utils import (
13     determine_ext,
14     ExtractorError,
15     int_or_none,
16     parse_iso8601,
17     qualities,
18     update_url_query,
19 )
20
21
22 class TVPlayIE(InfoExtractor):
23     IE_NAME = 'mtg'
24     IE_DESC = 'MTG services'
25     _VALID_URL = r'''(?x)
26                     (?:
27                         mtg:|
28                         https?://
29                             (?:www\.)?
30                             (?:
31                                 tvplay(?:\.skaties)?\.lv/parraides|
32                                 (?:tv3play|play\.tv3)\.lt/programos|
33                                 tv3play(?:\.tv3)?\.ee/sisu|
34                                 (?:tv(?:3|6|8|10)play|viafree)\.se/program|
35                                 (?:(?:tv3play|viasat4play|tv6play|viafree)\.no|(?:tv3play|viafree)\.dk)/programmer|
36                                 play\.novatv\.bg/programi
37                             )
38                             /(?:[^/]+/)+
39                         )
40                         (?P<id>\d+)
41                     '''
42     _TESTS = [
43         {
44             'url': 'http://www.tvplay.lv/parraides/vinas-melo-labak/418113?autostart=true',
45             'md5': 'a1612fe0849455423ad8718fe049be21',
46             'info_dict': {
47                 'id': '418113',
48                 'ext': 'mp4',
49                 'title': 'Kādi ir īri? - Viņas melo labāk',
50                 'description': 'Baiba apsmej īrus, kādi tie ir un ko viņi dara.',
51                 'series': 'Viņas melo labāk',
52                 'season': '2.sezona',
53                 'season_number': 2,
54                 'duration': 25,
55                 'timestamp': 1406097056,
56                 'upload_date': '20140723',
57             },
58         },
59         {
60             'url': 'http://play.tv3.lt/programos/moterys-meluoja-geriau/409229?autostart=true',
61             'info_dict': {
62                 'id': '409229',
63                 'ext': 'flv',
64                 'title': 'Moterys meluoja geriau',
65                 'description': 'md5:9aec0fc68e2cbc992d2a140bd41fa89e',
66                 'series': 'Moterys meluoja geriau',
67                 'episode_number': 47,
68                 'season': '1 sezonas',
69                 'season_number': 1,
70                 'duration': 1330,
71                 'timestamp': 1403769181,
72                 'upload_date': '20140626',
73             },
74             'params': {
75                 # rtmp download
76                 'skip_download': True,
77             },
78         },
79         {
80             'url': 'http://www.tv3play.ee/sisu/kodu-keset-linna/238551?autostart=true',
81             'info_dict': {
82                 'id': '238551',
83                 'ext': 'flv',
84                 'title': 'Kodu keset linna 398537',
85                 'description': 'md5:7df175e3c94db9e47c0d81ffa5d68701',
86                 'duration': 1257,
87                 'timestamp': 1292449761,
88                 'upload_date': '20101215',
89             },
90             'params': {
91                 # rtmp download
92                 'skip_download': True,
93             },
94         },
95         {
96             'url': 'http://www.tv3play.se/program/husraddarna/395385?autostart=true',
97             'info_dict': {
98                 'id': '395385',
99                 'ext': 'mp4',
100                 'title': 'Husräddarna S02E07',
101                 'description': 'md5:f210c6c89f42d4fc39faa551be813777',
102                 'duration': 2574,
103                 'timestamp': 1400596321,
104                 'upload_date': '20140520',
105             },
106             'params': {
107                 'skip_download': True,
108             },
109         },
110         {
111             'url': 'http://www.tv6play.se/program/den-sista-dokusapan/266636?autostart=true',
112             'info_dict': {
113                 'id': '266636',
114                 'ext': 'mp4',
115                 'title': 'Den sista dokusåpan S01E08',
116                 'description': 'md5:295be39c872520221b933830f660b110',
117                 'duration': 1492,
118                 'timestamp': 1330522854,
119                 'upload_date': '20120229',
120                 'age_limit': 18,
121             },
122             'params': {
123                 'skip_download': True,
124             },
125         },
126         {
127             'url': 'http://www.tv8play.se/program/antikjakten/282756?autostart=true',
128             'info_dict': {
129                 'id': '282756',
130                 'ext': 'mp4',
131                 'title': 'Antikjakten S01E10',
132                 'description': 'md5:1b201169beabd97e20c5ad0ad67b13b8',
133                 'duration': 2646,
134                 'timestamp': 1348575868,
135                 'upload_date': '20120925',
136             },
137             'params': {
138                 'skip_download': True,
139             },
140         },
141         {
142             'url': 'http://www.tv3play.no/programmer/anna-anka-soker-assistent/230898?autostart=true',
143             'info_dict': {
144                 'id': '230898',
145                 'ext': 'mp4',
146                 'title': 'Anna Anka søker assistent - Ep. 8',
147                 'description': 'md5:f80916bf5bbe1c5f760d127f8dd71474',
148                 'duration': 2656,
149                 'timestamp': 1277720005,
150                 'upload_date': '20100628',
151             },
152             'params': {
153                 'skip_download': True,
154             },
155         },
156         {
157             'url': 'http://www.viasat4play.no/programmer/budbringerne/21873?autostart=true',
158             'info_dict': {
159                 'id': '21873',
160                 'ext': 'mp4',
161                 'title': 'Budbringerne program 10',
162                 'description': 'md5:4db78dc4ec8a85bb04fd322a3ee5092d',
163                 'duration': 1297,
164                 'timestamp': 1254205102,
165                 'upload_date': '20090929',
166             },
167             'params': {
168                 'skip_download': True,
169             },
170         },
171         {
172             'url': 'http://www.tv6play.no/programmer/hotelinspektor-alex-polizzi/361883?autostart=true',
173             'info_dict': {
174                 'id': '361883',
175                 'ext': 'mp4',
176                 'title': 'Hotelinspektør Alex Polizzi - Ep. 10',
177                 'description': 'md5:3ecf808db9ec96c862c8ecb3a7fdaf81',
178                 'duration': 2594,
179                 'timestamp': 1393236292,
180                 'upload_date': '20140224',
181             },
182             'params': {
183                 'skip_download': True,
184             },
185         },
186         {
187             'url': 'http://play.novatv.bg/programi/zdravei-bulgariya/624952?autostart=true',
188             'info_dict': {
189                 'id': '624952',
190                 'ext': 'flv',
191                 'title': 'Здравей, България (12.06.2015 г.) ',
192                 'description': 'md5:99f3700451ac5bb71a260268b8daefd7',
193                 'duration': 8838,
194                 'timestamp': 1434100372,
195                 'upload_date': '20150612',
196             },
197             'params': {
198                 # rtmp download
199                 'skip_download': True,
200             },
201         },
202         {
203             'url': 'http://tvplay.skaties.lv/parraides/vinas-melo-labak/418113?autostart=true',
204             'only_matching': True,
205         },
206         {
207             'url': 'http://tv3play.tv3.ee/sisu/kodu-keset-linna/238551?autostart=true',
208             'only_matching': True,
209         },
210         {
211             'url': 'http://www.viafree.se/program/underhallning/i-like-radio-live/sasong-1/676869',
212             'only_matching': True,
213         },
214         {
215             'url': 'mtg:418113',
216             'only_matching': True,
217         }
218     ]
219
220     def _real_extract(self, url):
221         video_id = self._match_id(url)
222
223         video = self._download_json(
224             'http://playapi.mtgx.tv/v3/videos/%s' % video_id, video_id, 'Downloading video JSON')
225
226         title = video['title']
227
228         try:
229             streams = self._download_json(
230                 'http://playapi.mtgx.tv/v3/videos/stream/%s' % video_id,
231                 video_id, 'Downloading streams JSON')
232         except ExtractorError as e:
233             if isinstance(e.cause, compat_HTTPError) and e.cause.code == 403:
234                 msg = self._parse_json(e.cause.read().decode('utf-8'), video_id)
235                 raise ExtractorError(msg['msg'], expected=True)
236             raise
237
238         quality = qualities(['hls', 'medium', 'high'])
239         formats = []
240         for format_id, video_url in streams.get('streams', {}).items():
241             if not video_url or not isinstance(video_url, compat_str):
242                 continue
243             ext = determine_ext(video_url)
244             if ext == 'f4m':
245                 formats.extend(self._extract_f4m_formats(
246                     update_url_query(video_url, {
247                         'hdcore': '3.5.0',
248                         'plugin': 'aasp-3.5.0.151.81'
249                     }), video_id, f4m_id='hds', fatal=False))
250             elif ext == 'm3u8':
251                 formats.extend(self._extract_m3u8_formats(
252                     video_url, video_id, 'mp4', 'm3u8_native',
253                     m3u8_id='hls', fatal=False))
254             else:
255                 fmt = {
256                     'format_id': format_id,
257                     'quality': quality(format_id),
258                     'ext': ext,
259                 }
260                 if video_url.startswith('rtmp'):
261                     m = re.search(
262                         r'^(?P<url>rtmp://[^/]+/(?P<app>[^/]+))/(?P<playpath>.+)$', video_url)
263                     if not m:
264                         continue
265                     fmt.update({
266                         'ext': 'flv',
267                         'url': m.group('url'),
268                         'app': m.group('app'),
269                         'play_path': m.group('playpath'),
270                     })
271                 else:
272                     fmt.update({
273                         'url': video_url,
274                     })
275                 formats.append(fmt)
276
277         if not formats and video.get('is_geo_blocked'):
278             self.raise_geo_restricted(
279                 'This content might not be available in your country due to copyright reasons')
280
281         self._sort_formats(formats)
282
283         # TODO: webvtt in m3u8
284         subtitles = {}
285         sami_path = video.get('sami_path')
286         if sami_path:
287             lang = self._search_regex(
288                 r'_([a-z]{2})\.xml', sami_path, 'lang',
289                 default=compat_urlparse.urlparse(url).netloc.rsplit('.', 1)[-1])
290             subtitles[lang] = [{
291                 'url': sami_path,
292             }]
293
294         series = video.get('format_title')
295         episode_number = int_or_none(video.get('format_position', {}).get('episode'))
296         season = video.get('_embedded', {}).get('season', {}).get('title')
297         season_number = int_or_none(video.get('format_position', {}).get('season'))
298
299         return {
300             'id': video_id,
301             'title': title,
302             'description': video.get('description'),
303             'series': series,
304             'episode_number': episode_number,
305             'season': season,
306             'season_number': season_number,
307             'duration': int_or_none(video.get('duration')),
308             'timestamp': parse_iso8601(video.get('created_at')),
309             'view_count': int_or_none(video.get('views', {}).get('total')),
310             'age_limit': int_or_none(video.get('age_limit', 0)),
311             'formats': formats,
312             'subtitles': subtitles,
313         }
314
315
316 class ViafreeIE(InfoExtractor):
317     _VALID_URL = r'''(?x)
318                     https?://
319                         (?:www\.)?
320                         viafree\.
321                         (?:
322                             (?:dk|no)/programmer|
323                             se/program
324                         )
325                         /(?:[^/]+/)+(?P<id>[^/?#&]+)
326                     '''
327     _TESTS = [{
328         'url': 'http://www.viafree.se/program/livsstil/husraddarna/sasong-2/avsnitt-2',
329         'info_dict': {
330             'id': '395375',
331             'ext': 'mp4',
332             'title': 'Husräddarna S02E02',
333             'description': 'md5:4db5c933e37db629b5a2f75dfb34829e',
334             'series': 'Husräddarna',
335             'season': 'Säsong 2',
336             'season_number': 2,
337             'duration': 2576,
338             'timestamp': 1400596321,
339             'upload_date': '20140520',
340         },
341         'params': {
342             'skip_download': True,
343         },
344         'add_ie': [TVPlayIE.ie_key()],
345     }, {
346         'url': 'http://www.viafree.no/programmer/underholdning/det-beste-vorspielet/sesong-2/episode-1',
347         'only_matching': True,
348     }, {
349         'url': 'http://www.viafree.dk/programmer/reality/paradise-hotel/saeson-7/episode-5',
350         'only_matching': True,
351     }]
352
353     @classmethod
354     def suitable(cls, url):
355         return False if TVPlayIE.suitable(url) else super(ViafreeIE, cls).suitable(url)
356
357     def _real_extract(self, url):
358         video_id = self._match_id(url)
359
360         webpage = self._download_webpage(url, video_id)
361
362         video_id = self._search_regex(
363             r'currentVideo["\']\s*:\s*.+?["\']id["\']\s*:\s*["\'](?P<id>\d{6,})',
364             webpage, 'video id')
365
366         return self.url_result('mtg:%s' % video_id, TVPlayIE.ie_key())