]> Raphaël G. Git Repositories - youtubedl/blob - youtube_dl/extractor/tvplay.py
Update README.md
[youtubedl] / 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_HTTPError,
9 compat_urlparse,
10 )
11 from ..utils import (
12 determine_ext,
13 ExtractorError,
14 int_or_none,
15 parse_iso8601,
16 qualities,
17 try_get,
18 update_url_query,
19 url_or_none,
20 )
21
22
23 class TVPlayIE(InfoExtractor):
24 IE_NAME = 'mtg'
25 IE_DESC = 'MTG services'
26 _VALID_URL = r'''(?x)
27 (?:
28 mtg:|
29 https?://
30 (?:www\.)?
31 (?:
32 tvplay(?:\.skaties)?\.lv(?:/parraides)?|
33 (?:tv3play|play\.tv3)\.lt(?:/programos)?|
34 tv3play(?:\.tv3)?\.ee/sisu|
35 (?:tv(?:3|6|8|10)play|viafree)\.se/program|
36 (?:(?:tv3play|viasat4play|tv6play|viafree)\.no|(?:tv3play|viafree)\.dk)/programmer|
37 play\.nova(?:tv)?\.bg/programi
38 )
39 /(?:[^/]+/)+
40 )
41 (?P<id>\d+)
42 '''
43 _TESTS = [
44 {
45 'url': 'http://www.tvplay.lv/parraides/vinas-melo-labak/418113?autostart=true',
46 'md5': 'a1612fe0849455423ad8718fe049be21',
47 'info_dict': {
48 'id': '418113',
49 'ext': 'mp4',
50 'title': 'Kādi ir īri? - Viņas melo labāk',
51 'description': 'Baiba apsmej īrus, kādi tie ir un ko viņi dara.',
52 'series': 'Viņas melo labāk',
53 'season': '2.sezona',
54 'season_number': 2,
55 'duration': 25,
56 'timestamp': 1406097056,
57 'upload_date': '20140723',
58 },
59 },
60 {
61 'url': 'http://play.tv3.lt/programos/moterys-meluoja-geriau/409229?autostart=true',
62 'info_dict': {
63 'id': '409229',
64 'ext': 'flv',
65 'title': 'Moterys meluoja geriau',
66 'description': 'md5:9aec0fc68e2cbc992d2a140bd41fa89e',
67 'series': 'Moterys meluoja geriau',
68 'episode_number': 47,
69 'season': '1 sezonas',
70 'season_number': 1,
71 'duration': 1330,
72 'timestamp': 1403769181,
73 'upload_date': '20140626',
74 },
75 'params': {
76 # rtmp download
77 'skip_download': True,
78 },
79 },
80 {
81 'url': 'http://www.tv3play.ee/sisu/kodu-keset-linna/238551?autostart=true',
82 'info_dict': {
83 'id': '238551',
84 'ext': 'flv',
85 'title': 'Kodu keset linna 398537',
86 'description': 'md5:7df175e3c94db9e47c0d81ffa5d68701',
87 'duration': 1257,
88 'timestamp': 1292449761,
89 'upload_date': '20101215',
90 },
91 'params': {
92 # rtmp download
93 'skip_download': True,
94 },
95 },
96 {
97 'url': 'http://www.tv3play.se/program/husraddarna/395385?autostart=true',
98 'info_dict': {
99 'id': '395385',
100 'ext': 'mp4',
101 'title': 'Husräddarna S02E07',
102 'description': 'md5:f210c6c89f42d4fc39faa551be813777',
103 'duration': 2574,
104 'timestamp': 1400596321,
105 'upload_date': '20140520',
106 },
107 'params': {
108 'skip_download': True,
109 },
110 },
111 {
112 'url': 'http://www.tv6play.se/program/den-sista-dokusapan/266636?autostart=true',
113 'info_dict': {
114 'id': '266636',
115 'ext': 'mp4',
116 'title': 'Den sista dokusåpan S01E08',
117 'description': 'md5:295be39c872520221b933830f660b110',
118 'duration': 1492,
119 'timestamp': 1330522854,
120 'upload_date': '20120229',
121 'age_limit': 18,
122 },
123 'params': {
124 'skip_download': True,
125 },
126 },
127 {
128 'url': 'http://www.tv8play.se/program/antikjakten/282756?autostart=true',
129 'info_dict': {
130 'id': '282756',
131 'ext': 'mp4',
132 'title': 'Antikjakten S01E10',
133 'description': 'md5:1b201169beabd97e20c5ad0ad67b13b8',
134 'duration': 2646,
135 'timestamp': 1348575868,
136 'upload_date': '20120925',
137 },
138 'params': {
139 'skip_download': True,
140 },
141 },
142 {
143 'url': 'http://www.tv3play.no/programmer/anna-anka-soker-assistent/230898?autostart=true',
144 'info_dict': {
145 'id': '230898',
146 'ext': 'mp4',
147 'title': 'Anna Anka søker assistent - Ep. 8',
148 'description': 'md5:f80916bf5bbe1c5f760d127f8dd71474',
149 'duration': 2656,
150 'timestamp': 1277720005,
151 'upload_date': '20100628',
152 },
153 'params': {
154 'skip_download': True,
155 },
156 },
157 {
158 'url': 'http://www.viasat4play.no/programmer/budbringerne/21873?autostart=true',
159 'info_dict': {
160 'id': '21873',
161 'ext': 'mp4',
162 'title': 'Budbringerne program 10',
163 'description': 'md5:4db78dc4ec8a85bb04fd322a3ee5092d',
164 'duration': 1297,
165 'timestamp': 1254205102,
166 'upload_date': '20090929',
167 },
168 'params': {
169 'skip_download': True,
170 },
171 },
172 {
173 'url': 'http://www.tv6play.no/programmer/hotelinspektor-alex-polizzi/361883?autostart=true',
174 'info_dict': {
175 'id': '361883',
176 'ext': 'mp4',
177 'title': 'Hotelinspektør Alex Polizzi - Ep. 10',
178 'description': 'md5:3ecf808db9ec96c862c8ecb3a7fdaf81',
179 'duration': 2594,
180 'timestamp': 1393236292,
181 'upload_date': '20140224',
182 },
183 'params': {
184 'skip_download': True,
185 },
186 },
187 {
188 'url': 'http://play.novatv.bg/programi/zdravei-bulgariya/624952?autostart=true',
189 'info_dict': {
190 'id': '624952',
191 'ext': 'flv',
192 'title': 'Здравей, България (12.06.2015 г.) ',
193 'description': 'md5:99f3700451ac5bb71a260268b8daefd7',
194 'duration': 8838,
195 'timestamp': 1434100372,
196 'upload_date': '20150612',
197 },
198 'params': {
199 # rtmp download
200 'skip_download': True,
201 },
202 },
203 {
204 'url': 'https://play.nova.bg/programi/zdravei-bulgariya/764300?autostart=true',
205 'only_matching': True,
206 },
207 {
208 'url': 'http://tvplay.skaties.lv/parraides/vinas-melo-labak/418113?autostart=true',
209 'only_matching': True,
210 },
211 {
212 'url': 'https://tvplay.skaties.lv/vinas-melo-labak/418113/?autostart=true',
213 'only_matching': True,
214 },
215 {
216 # views is null
217 'url': 'http://tvplay.skaties.lv/parraides/tv3-zinas/760183',
218 'only_matching': True,
219 },
220 {
221 'url': 'http://tv3play.tv3.ee/sisu/kodu-keset-linna/238551?autostart=true',
222 'only_matching': True,
223 },
224 {
225 'url': 'http://www.viafree.se/program/underhallning/i-like-radio-live/sasong-1/676869',
226 'only_matching': True,
227 },
228 {
229 'url': 'mtg:418113',
230 'only_matching': True,
231 }
232 ]
233
234 def _real_extract(self, url):
235 video_id = self._match_id(url)
236 geo_country = self._search_regex(
237 r'https?://[^/]+\.([a-z]{2})', url,
238 'geo country', default=None)
239 if geo_country:
240 self._initialize_geo_bypass({'countries': [geo_country.upper()]})
241 video = self._download_json(
242 'http://playapi.mtgx.tv/v3/videos/%s' % video_id, video_id, 'Downloading video JSON')
243
244 title = video['title']
245
246 try:
247 streams = self._download_json(
248 'http://playapi.mtgx.tv/v3/videos/stream/%s' % video_id,
249 video_id, 'Downloading streams JSON')
250 except ExtractorError as e:
251 if isinstance(e.cause, compat_HTTPError) and e.cause.code == 403:
252 msg = self._parse_json(e.cause.read().decode('utf-8'), video_id)
253 raise ExtractorError(msg['msg'], expected=True)
254 raise
255
256 quality = qualities(['hls', 'medium', 'high'])
257 formats = []
258 for format_id, video_url in streams.get('streams', {}).items():
259 video_url = url_or_none(video_url)
260 if not video_url:
261 continue
262 ext = determine_ext(video_url)
263 if ext == 'f4m':
264 formats.extend(self._extract_f4m_formats(
265 update_url_query(video_url, {
266 'hdcore': '3.5.0',
267 'plugin': 'aasp-3.5.0.151.81'
268 }), video_id, f4m_id='hds', fatal=False))
269 elif ext == 'm3u8':
270 formats.extend(self._extract_m3u8_formats(
271 video_url, video_id, 'mp4', 'm3u8_native',
272 m3u8_id='hls', fatal=False))
273 else:
274 fmt = {
275 'format_id': format_id,
276 'quality': quality(format_id),
277 'ext': ext,
278 }
279 if video_url.startswith('rtmp'):
280 m = re.search(
281 r'^(?P<url>rtmp://[^/]+/(?P<app>[^/]+))/(?P<playpath>.+)$', video_url)
282 if not m:
283 continue
284 fmt.update({
285 'ext': 'flv',
286 'url': m.group('url'),
287 'app': m.group('app'),
288 'play_path': m.group('playpath'),
289 'preference': -1,
290 })
291 else:
292 fmt.update({
293 'url': video_url,
294 })
295 formats.append(fmt)
296
297 if not formats and video.get('is_geo_blocked'):
298 self.raise_geo_restricted(
299 'This content might not be available in your country due to copyright reasons')
300
301 self._sort_formats(formats)
302
303 # TODO: webvtt in m3u8
304 subtitles = {}
305 sami_path = video.get('sami_path')
306 if sami_path:
307 lang = self._search_regex(
308 r'_([a-z]{2})\.xml', sami_path, 'lang',
309 default=compat_urlparse.urlparse(url).netloc.rsplit('.', 1)[-1])
310 subtitles[lang] = [{
311 'url': sami_path,
312 }]
313
314 series = video.get('format_title')
315 episode_number = int_or_none(video.get('format_position', {}).get('episode'))
316 season = video.get('_embedded', {}).get('season', {}).get('title')
317 season_number = int_or_none(video.get('format_position', {}).get('season'))
318
319 return {
320 'id': video_id,
321 'title': title,
322 'description': video.get('description'),
323 'series': series,
324 'episode_number': episode_number,
325 'season': season,
326 'season_number': season_number,
327 'duration': int_or_none(video.get('duration')),
328 'timestamp': parse_iso8601(video.get('created_at')),
329 'view_count': try_get(video, lambda x: x['views']['total'], int),
330 'age_limit': int_or_none(video.get('age_limit', 0)),
331 'formats': formats,
332 'subtitles': subtitles,
333 }
334
335
336 class ViafreeIE(InfoExtractor):
337 _VALID_URL = r'''(?x)
338 https?://
339 (?:www\.)?
340 viafree\.(?P<country>dk|no|se)
341 /(?P<id>program(?:mer)?/(?:[^/]+/)+[^/?#&]+)
342 '''
343 _TESTS = [{
344 'url': 'http://www.viafree.no/programmer/underholdning/det-beste-vorspielet/sesong-2/episode-1',
345 'info_dict': {
346 'id': '757786',
347 'ext': 'mp4',
348 'title': 'Det beste vorspielet - Sesong 2 - Episode 1',
349 'description': 'md5:b632cb848331404ccacd8cd03e83b4c3',
350 'series': 'Det beste vorspielet',
351 'season_number': 2,
352 'duration': 1116,
353 'timestamp': 1471200600,
354 'upload_date': '20160814',
355 },
356 'params': {
357 'skip_download': True,
358 },
359 }, {
360 # with relatedClips
361 'url': 'http://www.viafree.se/program/reality/sommaren-med-youtube-stjarnorna/sasong-1/avsnitt-1',
362 'only_matching': True,
363 }, {
364 # Different og:image URL schema
365 'url': 'http://www.viafree.se/program/reality/sommaren-med-youtube-stjarnorna/sasong-1/avsnitt-2',
366 'only_matching': True,
367 }, {
368 'url': 'http://www.viafree.se/program/livsstil/husraddarna/sasong-2/avsnitt-2',
369 'only_matching': True,
370 }, {
371 'url': 'http://www.viafree.dk/programmer/reality/paradise-hotel/saeson-7/episode-5',
372 'only_matching': True,
373 }]
374 _GEO_BYPASS = False
375
376 @classmethod
377 def suitable(cls, url):
378 return False if TVPlayIE.suitable(url) else super(ViafreeIE, cls).suitable(url)
379
380 def _real_extract(self, url):
381 country, path = re.match(self._VALID_URL, url).groups()
382 content = self._download_json(
383 'https://viafree-content.mtg-api.com/viafree-content/v1/%s/path/%s' % (country, path), path)
384 program = content['_embedded']['viafreeBlocks'][0]['_embedded']['program']
385 guid = program['guid']
386 meta = content['meta']
387 title = meta['title']
388
389 try:
390 stream_href = self._download_json(
391 program['_links']['streamLink']['href'], guid,
392 headers=self.geo_verification_headers())['embedded']['prioritizedStreams'][0]['links']['stream']['href']
393 except ExtractorError as e:
394 if isinstance(e.cause, compat_HTTPError) and e.cause.code == 403:
395 self.raise_geo_restricted(countries=[country])
396 raise
397
398 formats = self._extract_m3u8_formats(stream_href, guid, 'mp4')
399 self._sort_formats(formats)
400 episode = program.get('episode') or {}
401
402 return {
403 'id': guid,
404 'title': title,
405 'thumbnail': meta.get('image'),
406 'description': meta.get('description'),
407 'series': episode.get('seriesTitle'),
408 'episode_number': int_or_none(episode.get('episodeNumber')),
409 'season_number': int_or_none(episode.get('seasonNumber')),
410 'duration': int_or_none(try_get(program, lambda x: x['video']['duration']['milliseconds']), 1000),
411 'timestamp': parse_iso8601(try_get(program, lambda x: x['availability']['start'])),
412 'formats': formats,
413 }
414
415
416 class TVPlayHomeIE(InfoExtractor):
417 _VALID_URL = r'https?://tvplay\.(?:tv3\.lt|skaties\.lv|tv3\.ee)/[^/]+/[^/?#&]+-(?P<id>\d+)'
418 _TESTS = [{
419 'url': 'https://tvplay.tv3.lt/aferistai-n-7/aferistai-10047125/',
420 'info_dict': {
421 'id': '366367',
422 'ext': 'mp4',
423 'title': 'Aferistai',
424 'description': 'Aferistai. Kalėdinė pasaka.',
425 'series': 'Aferistai [N-7]',
426 'season': '1 sezonas',
427 'season_number': 1,
428 'duration': 464,
429 'timestamp': 1394209658,
430 'upload_date': '20140307',
431 'age_limit': 18,
432 },
433 'params': {
434 'skip_download': True,
435 },
436 'add_ie': [TVPlayIE.ie_key()],
437 }, {
438 'url': 'https://tvplay.skaties.lv/vinas-melo-labak/vinas-melo-labak-10280317/',
439 'only_matching': True,
440 }, {
441 'url': 'https://tvplay.tv3.ee/cool-d-ga-mehhikosse/cool-d-ga-mehhikosse-10044354/',
442 'only_matching': True,
443 }]
444
445 def _real_extract(self, url):
446 video_id = self._match_id(url)
447
448 webpage = self._download_webpage(url, video_id)
449
450 video_id = self._search_regex(
451 r'data-asset-id\s*=\s*["\'](\d{5,})\b', webpage, 'video id')
452
453 if len(video_id) < 8:
454 return self.url_result(
455 'mtg:%s' % video_id, ie=TVPlayIE.ie_key(), video_id=video_id)
456
457 m3u8_url = self._search_regex(
458 r'data-file\s*=\s*(["\'])(?P<url>(?:(?!\1).)+)\1', webpage,
459 'm3u8 url', group='url')
460
461 formats = self._extract_m3u8_formats(
462 m3u8_url, video_id, 'mp4', entry_protocol='m3u8_native',
463 m3u8_id='hls')
464 self._sort_formats(formats)
465
466 title = self._search_regex(
467 r'data-title\s*=\s*(["\'])(?P<value>(?:(?!\1).)+)\1', webpage,
468 'title', default=None, group='value') or self._html_search_meta(
469 'title', webpage, default=None) or self._og_search_title(
470 webpage)
471
472 description = self._html_search_meta(
473 'description', webpage,
474 default=None) or self._og_search_description(webpage)
475
476 thumbnail = self._search_regex(
477 r'data-image\s*=\s*(["\'])(?P<url>(?:(?!\1).)+)\1', webpage,
478 'thumbnail', default=None, group='url') or self._html_search_meta(
479 'thumbnail', webpage, default=None) or self._og_search_thumbnail(
480 webpage)
481
482 duration = int_or_none(self._search_regex(
483 r'data-duration\s*=\s*["\'](\d+)', webpage, 'duration',
484 fatal=False))
485
486 season = self._search_regex(
487 (r'data-series-title\s*=\s*(["\'])[^/]+/(?P<value>(?:(?!\1).)+)\1',
488 r'\bseason\s*:\s*(["\'])(?P<value>(?:(?!\1).)+)\1'), webpage,
489 'season', default=None, group='value')
490 season_number = int_or_none(self._search_regex(
491 r'(\d+)(?:[.\s]+sezona|\s+HOOAEG)', season or '', 'season number',
492 default=None))
493 episode = self._search_regex(
494 (r'\bepisode\s*:\s*(["\'])(?P<value>(?:(?!\1).)+)\1',
495 r'data-subtitle\s*=\s*(["\'])(?P<value>(?:(?!\1).)+)\1'), webpage,
496 'episode', default=None, group='value')
497 episode_number = int_or_none(self._search_regex(
498 r'(?:S[]rija|Osa)\s+(\d+)', episode or '', 'episode number',
499 default=None))
500
501 return {
502 'id': video_id,
503 'title': title,
504 'description': description,
505 'thumbnail': thumbnail,
506 'duration': duration,
507 'season': season,
508 'season_number': season_number,
509 'episode': episode,
510 'episode_number': episode_number,
511 'formats': formats,
512 }