Merge branch 'shahid' of https://github.com/remitamine/youtube-dl into remitamine...
[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                 'age_limit': 18,
108             },
109             'params': {
110                 # rtmp download
111                 'skip_download': True,
112             },
113         },
114         {
115             'url': 'http://www.tv8play.se/program/antikjakten/282756?autostart=true',
116             'info_dict': {
117                 'id': '282756',
118                 'ext': 'flv',
119                 'title': 'Antikjakten S01E10',
120                 'description': 'md5:1b201169beabd97e20c5ad0ad67b13b8',
121                 'duration': 2646,
122                 'timestamp': 1348575868,
123                 'upload_date': '20120925',
124             },
125             'params': {
126                 # rtmp download
127                 'skip_download': True,
128             },
129         },
130         {
131             'url': 'http://www.tv3play.no/programmer/anna-anka-soker-assistent/230898?autostart=true',
132             'info_dict': {
133                 'id': '230898',
134                 'ext': 'flv',
135                 'title': 'Anna Anka søker assistent - Ep. 8',
136                 'description': 'md5:f80916bf5bbe1c5f760d127f8dd71474',
137                 'duration': 2656,
138                 'timestamp': 1277720005,
139                 'upload_date': '20100628',
140             },
141             'params': {
142                 # rtmp download
143                 'skip_download': True,
144             },
145         },
146         {
147             'url': 'http://www.viasat4play.no/programmer/budbringerne/21873?autostart=true',
148             'info_dict': {
149                 'id': '21873',
150                 'ext': 'flv',
151                 'title': 'Budbringerne program 10',
152                 'description': 'md5:4db78dc4ec8a85bb04fd322a3ee5092d',
153                 'duration': 1297,
154                 'timestamp': 1254205102,
155                 'upload_date': '20090929',
156             },
157             'params': {
158                 # rtmp download
159                 'skip_download': True,
160             },
161         },
162         {
163             'url': 'http://www.tv6play.no/programmer/hotelinspektor-alex-polizzi/361883?autostart=true',
164             'info_dict': {
165                 'id': '361883',
166                 'ext': 'flv',
167                 'title': 'Hotelinspektør Alex Polizzi - Ep. 10',
168                 'description': 'md5:3ecf808db9ec96c862c8ecb3a7fdaf81',
169                 'duration': 2594,
170                 'timestamp': 1393236292,
171                 'upload_date': '20140224',
172             },
173             'params': {
174                 # rtmp download
175                 'skip_download': True,
176             },
177         },
178         {
179             'url': 'http://play.novatv.bg/programi/zdravei-bulgariya/624952?autostart=true',
180             'info_dict': {
181                 'id': '624952',
182                 'ext': 'flv',
183                 'title': 'Здравей, България (12.06.2015 г.) ',
184                 'description': 'md5:99f3700451ac5bb71a260268b8daefd7',
185                 'duration': 8838,
186                 'timestamp': 1434100372,
187                 'upload_date': '20150612',
188             },
189             'params': {
190                 # rtmp download
191                 'skip_download': True,
192             },
193         },
194     ]
195
196     def _real_extract(self, url):
197         video_id = self._match_id(url)
198
199         video = self._download_json(
200             'http://playapi.mtgx.tv/v1/videos/%s' % video_id, video_id, 'Downloading video JSON')
201
202         if video['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['streams'].items():
212             if not video_url or not isinstance(video_url, compat_str):
213                 continue
214             fmt = {
215                 'format_id': format_id,
216                 'preference': quality(format_id),
217             }
218             if video_url.startswith('rtmp'):
219                 m = re.search(r'^(?P<url>rtmp://[^/]+/(?P<app>[^/]+))/(?P<playpath>.+)$', video_url)
220                 if not m:
221                     continue
222                 fmt.update({
223                     'ext': 'flv',
224                     'url': m.group('url'),
225                     'app': m.group('app'),
226                     'play_path': m.group('playpath'),
227                 })
228             elif video_url.endswith('.f4m'):
229                 formats.extend(self._extract_f4m_formats(
230                     video_url + '?hdcore=3.5.0&plugin=aasp-3.5.0.151.81', video_id))
231                 continue
232             else:
233                 fmt.update({
234                     'url': video_url,
235                 })
236             formats.append(fmt)
237
238         self._sort_formats(formats)
239
240         return {
241             'id': video_id,
242             'title': video['title'],
243             'description': video['description'],
244             'duration': video['duration'],
245             'timestamp': parse_iso8601(video['created_at']),
246             'view_count': video['views']['total'],
247             'age_limit': video.get('age_limit', 0),
248             'formats': formats,
249         }