[tvplay] Add support for subtitles (Closes #10194)
[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_str,
9     compat_urlparse,
10 )
11 from ..utils import (
12     parse_iso8601,
13     qualities,
14     determine_ext,
15     update_url_query,
16     int_or_none,
17 )
18
19
20 class TVPlayIE(InfoExtractor):
21     IE_DESC = 'TV3Play and related services'
22     _VALID_URL = r'''(?x)https?://(?:www\.)?
23         (?:tvplay(?:\.skaties)?\.lv/parraides|
24            (?:tv3play|play\.tv3)\.lt/programos|
25            tv3play(?:\.tv3)?\.ee/sisu|
26            tv(?:3|6|8|10)play\.se/program|
27            (?:(?:tv3play|viasat4play|tv6play)\.no|tv3play\.dk)/programmer|
28            play\.novatv\.bg/programi
29         )/[^/]+/(?P<id>\d+)
30         '''
31     _TESTS = [
32         {
33             'url': 'http://www.tvplay.lv/parraides/vinas-melo-labak/418113?autostart=true',
34             'md5': 'a1612fe0849455423ad8718fe049be21',
35             'info_dict': {
36                 'id': '418113',
37                 'ext': 'mp4',
38                 'title': 'Kādi ir īri? - Viņas melo labāk',
39                 'description': 'Baiba apsmej īrus, kādi tie ir un ko viņi dara.',
40                 'duration': 25,
41                 'timestamp': 1406097056,
42                 'upload_date': '20140723',
43             },
44         },
45         {
46             'url': 'http://play.tv3.lt/programos/moterys-meluoja-geriau/409229?autostart=true',
47             'info_dict': {
48                 'id': '409229',
49                 'ext': 'flv',
50                 'title': 'Moterys meluoja geriau',
51                 'description': 'md5:9aec0fc68e2cbc992d2a140bd41fa89e',
52                 'duration': 1330,
53                 'timestamp': 1403769181,
54                 'upload_date': '20140626',
55             },
56             'params': {
57                 # rtmp download
58                 'skip_download': True,
59             },
60         },
61         {
62             'url': 'http://www.tv3play.ee/sisu/kodu-keset-linna/238551?autostart=true',
63             'info_dict': {
64                 'id': '238551',
65                 'ext': 'flv',
66                 'title': 'Kodu keset linna 398537',
67                 'description': 'md5:7df175e3c94db9e47c0d81ffa5d68701',
68                 'duration': 1257,
69                 'timestamp': 1292449761,
70                 'upload_date': '20101215',
71             },
72             'params': {
73                 # rtmp download
74                 'skip_download': True,
75             },
76         },
77         {
78             'url': 'http://www.tv3play.se/program/husraddarna/395385?autostart=true',
79             'info_dict': {
80                 'id': '395385',
81                 'ext': 'mp4',
82                 'title': 'Husräddarna S02E07',
83                 'description': 'md5:f210c6c89f42d4fc39faa551be813777',
84                 'duration': 2574,
85                 'timestamp': 1400596321,
86                 'upload_date': '20140520',
87             },
88             'params': {
89                 'skip_download': True,
90             },
91         },
92         {
93             'url': 'http://www.tv6play.se/program/den-sista-dokusapan/266636?autostart=true',
94             'info_dict': {
95                 'id': '266636',
96                 'ext': 'mp4',
97                 'title': 'Den sista dokusåpan S01E08',
98                 'description': 'md5:295be39c872520221b933830f660b110',
99                 'duration': 1492,
100                 'timestamp': 1330522854,
101                 'upload_date': '20120229',
102                 'age_limit': 18,
103             },
104             'params': {
105                 'skip_download': True,
106             },
107         },
108         {
109             'url': 'http://www.tv8play.se/program/antikjakten/282756?autostart=true',
110             'info_dict': {
111                 'id': '282756',
112                 'ext': 'mp4',
113                 'title': 'Antikjakten S01E10',
114                 'description': 'md5:1b201169beabd97e20c5ad0ad67b13b8',
115                 'duration': 2646,
116                 'timestamp': 1348575868,
117                 'upload_date': '20120925',
118             },
119             'params': {
120                 'skip_download': True,
121             },
122         },
123         {
124             'url': 'http://www.tv3play.no/programmer/anna-anka-soker-assistent/230898?autostart=true',
125             'info_dict': {
126                 'id': '230898',
127                 'ext': 'mp4',
128                 'title': 'Anna Anka søker assistent - Ep. 8',
129                 'description': 'md5:f80916bf5bbe1c5f760d127f8dd71474',
130                 'duration': 2656,
131                 'timestamp': 1277720005,
132                 'upload_date': '20100628',
133             },
134             'params': {
135                 'skip_download': True,
136             },
137         },
138         {
139             'url': 'http://www.viasat4play.no/programmer/budbringerne/21873?autostart=true',
140             'info_dict': {
141                 'id': '21873',
142                 'ext': 'mp4',
143                 'title': 'Budbringerne program 10',
144                 'description': 'md5:4db78dc4ec8a85bb04fd322a3ee5092d',
145                 'duration': 1297,
146                 'timestamp': 1254205102,
147                 'upload_date': '20090929',
148             },
149             'params': {
150                 'skip_download': True,
151             },
152         },
153         {
154             'url': 'http://www.tv6play.no/programmer/hotelinspektor-alex-polizzi/361883?autostart=true',
155             'info_dict': {
156                 'id': '361883',
157                 'ext': 'mp4',
158                 'title': 'Hotelinspektør Alex Polizzi - Ep. 10',
159                 'description': 'md5:3ecf808db9ec96c862c8ecb3a7fdaf81',
160                 'duration': 2594,
161                 'timestamp': 1393236292,
162                 'upload_date': '20140224',
163             },
164             'params': {
165                 'skip_download': True,
166             },
167         },
168         {
169             'url': 'http://play.novatv.bg/programi/zdravei-bulgariya/624952?autostart=true',
170             'info_dict': {
171                 'id': '624952',
172                 'ext': 'flv',
173                 'title': 'Здравей, България (12.06.2015 г.) ',
174                 'description': 'md5:99f3700451ac5bb71a260268b8daefd7',
175                 'duration': 8838,
176                 'timestamp': 1434100372,
177                 'upload_date': '20150612',
178             },
179             'params': {
180                 # rtmp download
181                 'skip_download': True,
182             },
183         },
184         {
185             'url': 'http://tvplay.skaties.lv/parraides/vinas-melo-labak/418113?autostart=true',
186             'only_matching': True,
187         },
188         {
189             'url': 'http://tv3play.tv3.ee/sisu/kodu-keset-linna/238551?autostart=true',
190             'only_matching': True,
191         }
192     ]
193
194     def _real_extract(self, url):
195         video_id = self._match_id(url)
196
197         video = self._download_json(
198             'http://playapi.mtgx.tv/v1/videos/%s' % video_id, video_id, 'Downloading video JSON')
199
200         title = video['title']
201
202         if video.get('is_geo_blocked'):
203             self.report_warning(
204                 'This content might not be available in your country due to copyright reasons')
205
206         streams = self._download_json(
207             'http://playapi.mtgx.tv/v1/videos/stream/%s' % video_id, video_id, 'Downloading streams JSON')
208
209         quality = qualities(['hls', 'medium', 'high'])
210         formats = []
211         for format_id, video_url in streams.get('streams', {}).items():
212             if not video_url or not isinstance(video_url, compat_str):
213                 continue
214             ext = determine_ext(video_url)
215             if ext == 'f4m':
216                 formats.extend(self._extract_f4m_formats(
217                     update_url_query(video_url, {
218                         'hdcore': '3.5.0',
219                         'plugin': 'aasp-3.5.0.151.81'
220                     }), video_id, f4m_id='hds', fatal=False))
221             elif ext == 'm3u8':
222                 formats.extend(self._extract_m3u8_formats(
223                     video_url, video_id, 'mp4', 'm3u8_native',
224                     m3u8_id='hls', fatal=False))
225             else:
226                 fmt = {
227                     'format_id': format_id,
228                     'quality': quality(format_id),
229                     'ext': ext,
230                 }
231                 if video_url.startswith('rtmp'):
232                     m = re.search(
233                         r'^(?P<url>rtmp://[^/]+/(?P<app>[^/]+))/(?P<playpath>.+)$', video_url)
234                     if not m:
235                         continue
236                     fmt.update({
237                         'ext': 'flv',
238                         'url': m.group('url'),
239                         'app': m.group('app'),
240                         'play_path': m.group('playpath'),
241                     })
242                 else:
243                     fmt.update({
244                         'url': video_url,
245                     })
246                 formats.append(fmt)
247         self._sort_formats(formats)
248
249         # TODO: webvtt in m3u8
250         subtitles = {}
251         sami_path = video.get('sami_path')
252         if sami_path:
253             lang = self._search_regex(
254                 r'_([a-z]{2})\.xml', sami_path, 'lang',
255                 default=compat_urlparse.urlparse(url).netloc.rsplit('.', 1)[-1])
256             subtitles[lang] = [{
257                 'url': sami_path,
258             }]
259
260         return {
261             'id': video_id,
262             'title': title,
263             'description': video.get('description'),
264             'duration': int_or_none(video.get('duration')),
265             'timestamp': parse_iso8601(video.get('created_at')),
266             'view_count': int_or_none(video.get('views', {}).get('total')),
267             'age_limit': int_or_none(video.get('age_limit', 0)),
268             'formats': formats,
269             'subtitles': subtitles,
270         }