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