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