[tvplay] Extract series metadata
[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                 'series': 'Viņas melo labāk',
41                 'season': '2.sezona',
42                 'season_number': 2,
43                 'duration': 25,
44                 'timestamp': 1406097056,
45                 'upload_date': '20140723',
46             },
47         },
48         {
49             'url': 'http://play.tv3.lt/programos/moterys-meluoja-geriau/409229?autostart=true',
50             'info_dict': {
51                 'id': '409229',
52                 'ext': 'flv',
53                 'title': 'Moterys meluoja geriau',
54                 'description': 'md5:9aec0fc68e2cbc992d2a140bd41fa89e',
55                 'series': 'Moterys meluoja geriau',
56                 'episode_number': 47,
57                 'season': '1 sezonas',
58                 'season_number': 1,
59                 'duration': 1330,
60                 'timestamp': 1403769181,
61                 'upload_date': '20140626',
62             },
63             'params': {
64                 # rtmp download
65                 'skip_download': True,
66             },
67         },
68         {
69             'url': 'http://www.tv3play.ee/sisu/kodu-keset-linna/238551?autostart=true',
70             'info_dict': {
71                 'id': '238551',
72                 'ext': 'flv',
73                 'title': 'Kodu keset linna 398537',
74                 'description': 'md5:7df175e3c94db9e47c0d81ffa5d68701',
75                 'duration': 1257,
76                 'timestamp': 1292449761,
77                 'upload_date': '20101215',
78             },
79             'params': {
80                 # rtmp download
81                 'skip_download': True,
82             },
83         },
84         {
85             'url': 'http://www.tv3play.se/program/husraddarna/395385?autostart=true',
86             'info_dict': {
87                 'id': '395385',
88                 'ext': 'mp4',
89                 'title': 'Husräddarna S02E07',
90                 'description': 'md5:f210c6c89f42d4fc39faa551be813777',
91                 'duration': 2574,
92                 'timestamp': 1400596321,
93                 'upload_date': '20140520',
94             },
95             'params': {
96                 'skip_download': True,
97             },
98         },
99         {
100             'url': 'http://www.tv6play.se/program/den-sista-dokusapan/266636?autostart=true',
101             'info_dict': {
102                 'id': '266636',
103                 'ext': 'mp4',
104                 'title': 'Den sista dokusåpan S01E08',
105                 'description': 'md5:295be39c872520221b933830f660b110',
106                 'duration': 1492,
107                 'timestamp': 1330522854,
108                 'upload_date': '20120229',
109                 'age_limit': 18,
110             },
111             'params': {
112                 'skip_download': True,
113             },
114         },
115         {
116             'url': 'http://www.tv8play.se/program/antikjakten/282756?autostart=true',
117             'info_dict': {
118                 'id': '282756',
119                 'ext': 'mp4',
120                 'title': 'Antikjakten S01E10',
121                 'description': 'md5:1b201169beabd97e20c5ad0ad67b13b8',
122                 'duration': 2646,
123                 'timestamp': 1348575868,
124                 'upload_date': '20120925',
125             },
126             'params': {
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': 'mp4',
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                 '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': 'mp4',
150                 'title': 'Budbringerne program 10',
151                 'description': 'md5:4db78dc4ec8a85bb04fd322a3ee5092d',
152                 'duration': 1297,
153                 'timestamp': 1254205102,
154                 'upload_date': '20090929',
155             },
156             'params': {
157                 'skip_download': True,
158             },
159         },
160         {
161             'url': 'http://www.tv6play.no/programmer/hotelinspektor-alex-polizzi/361883?autostart=true',
162             'info_dict': {
163                 'id': '361883',
164                 'ext': 'mp4',
165                 'title': 'Hotelinspektør Alex Polizzi - Ep. 10',
166                 'description': 'md5:3ecf808db9ec96c862c8ecb3a7fdaf81',
167                 'duration': 2594,
168                 'timestamp': 1393236292,
169                 'upload_date': '20140224',
170             },
171             'params': {
172                 'skip_download': True,
173             },
174         },
175         {
176             'url': 'http://play.novatv.bg/programi/zdravei-bulgariya/624952?autostart=true',
177             'info_dict': {
178                 'id': '624952',
179                 'ext': 'flv',
180                 'title': 'Здравей, България (12.06.2015 г.) ',
181                 'description': 'md5:99f3700451ac5bb71a260268b8daefd7',
182                 'duration': 8838,
183                 'timestamp': 1434100372,
184                 'upload_date': '20150612',
185             },
186             'params': {
187                 # rtmp download
188                 'skip_download': True,
189             },
190         },
191         {
192             'url': 'http://tvplay.skaties.lv/parraides/vinas-melo-labak/418113?autostart=true',
193             'only_matching': True,
194         },
195         {
196             'url': 'http://tv3play.tv3.ee/sisu/kodu-keset-linna/238551?autostart=true',
197             'only_matching': True,
198         }
199     ]
200
201     def _real_extract(self, url):
202         video_id = self._match_id(url)
203
204         video = self._download_json(
205             'http://playapi.mtgx.tv/v1/videos/%s' % video_id, video_id, 'Downloading video JSON')
206
207         title = video['title']
208
209         if video.get('is_geo_blocked'):
210             self.report_warning(
211                 'This content might not be available in your country due to copyright reasons')
212
213         streams = self._download_json(
214             'http://playapi.mtgx.tv/v1/videos/stream/%s' % video_id, video_id, 'Downloading streams JSON')
215
216         quality = qualities(['hls', 'medium', 'high'])
217         formats = []
218         for format_id, video_url in streams.get('streams', {}).items():
219             if not video_url or not isinstance(video_url, compat_str):
220                 continue
221             ext = determine_ext(video_url)
222             if ext == 'f4m':
223                 formats.extend(self._extract_f4m_formats(
224                     update_url_query(video_url, {
225                         'hdcore': '3.5.0',
226                         'plugin': 'aasp-3.5.0.151.81'
227                     }), video_id, f4m_id='hds', fatal=False))
228             elif ext == 'm3u8':
229                 formats.extend(self._extract_m3u8_formats(
230                     video_url, video_id, 'mp4', 'm3u8_native',
231                     m3u8_id='hls', fatal=False))
232             else:
233                 fmt = {
234                     'format_id': format_id,
235                     'quality': quality(format_id),
236                     'ext': ext,
237                 }
238                 if video_url.startswith('rtmp'):
239                     m = re.search(
240                         r'^(?P<url>rtmp://[^/]+/(?P<app>[^/]+))/(?P<playpath>.+)$', video_url)
241                     if not m:
242                         continue
243                     fmt.update({
244                         'ext': 'flv',
245                         'url': m.group('url'),
246                         'app': m.group('app'),
247                         'play_path': m.group('playpath'),
248                     })
249                 else:
250                     fmt.update({
251                         'url': video_url,
252                     })
253                 formats.append(fmt)
254         self._sort_formats(formats)
255
256         # TODO: webvtt in m3u8
257         subtitles = {}
258         sami_path = video.get('sami_path')
259         if sami_path:
260             lang = self._search_regex(
261                 r'_([a-z]{2})\.xml', sami_path, 'lang',
262                 default=compat_urlparse.urlparse(url).netloc.rsplit('.', 1)[-1])
263             subtitles[lang] = [{
264                 'url': sami_path,
265             }]
266
267         series = video.get('format_title')
268         episode_number = int_or_none(video.get('format_position', {}).get('episode'))
269         season = video.get('_embedded', {}).get('season', {}).get('title')
270         season_number = int_or_none(video.get('format_position', {}).get('season'))
271
272         return {
273             'id': video_id,
274             'title': title,
275             'description': video.get('description'),
276             'series': series,
277             'episode_number': episode_number,
278             'season': season,
279             'season_number': season_number,
280             'duration': int_or_none(video.get('duration')),
281             'timestamp': parse_iso8601(video.get('created_at')),
282             'view_count': int_or_none(video.get('views', {}).get('total')),
283             'age_limit': int_or_none(video.get('age_limit', 0)),
284             'formats': formats,
285             'subtitles': subtitles,
286         }