Merge branch 'fstirlitz-filmon'
[youtube-dl] / youtube_dl / extractor / disney.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .common import InfoExtractor
7 from ..utils import (
8     int_or_none,
9     unified_strdate,
10     compat_str,
11     determine_ext,
12 )
13
14
15 class DisneyIE(InfoExtractor):
16     _VALID_URL = r'''(?x)
17         https?://(?P<domain>(?:[^/]+\.)?(?:disney\.[a-z]{2,3}(?:\.[a-z]{2})?|disney(?:(?:me|latino)\.com|turkiye\.com\.tr)|starwars\.com))/(?:embed/|(?:[^/]+/)+[\w-]+-)(?P<id>[a-z0-9]{24})'''
18     _TESTS = [{
19         'url': 'http://video.disney.com/watch/moana-trailer-545ed1857afee5a0ec239977',
20         'info_dict': {
21             'id': '545ed1857afee5a0ec239977',
22             'ext': 'mp4',
23             'title': 'Moana - Trailer',
24             'description': 'A fun adventure for the entire Family!  Bring home Moana on Digital HD Feb 21 & Blu-ray March 7',
25             'upload_date': '20170112',
26         },
27         'params': {
28             # m3u8 download
29             'skip_download': True,
30         }
31     }, {
32         'url': 'http://videos.disneylatino.com/ver/spider-man-de-regreso-a-casa-primer-adelanto-543a33a1850bdcfcca13bae2',
33         'only_matching': True,
34     }, {
35         'url': 'http://video.en.disneyme.com/watch/future-worm/robo-carp-2001-544b66002aa7353cdd3f5114',
36         'only_matching': True,
37     }, {
38         'url': 'http://video.disneyturkiye.com.tr/izle/7c-7-cuceler/kimin-sesi-zaten-5456f3d015f6b36c8afdd0e2',
39         'only_matching': True,
40     }, {
41         'url': 'http://disneyjunior.disney.com/embed/546a4798ddba3d1612e4005d',
42         'only_matching': True,
43     }, {
44         'url': 'http://www.starwars.com/embed/54690d1e6c42e5f09a0fb097',
45         'only_matching': True,
46     }]
47
48     def _real_extract(self, url):
49         domain, video_id = re.match(self._VALID_URL, url).groups()
50         webpage = self._download_webpage(
51             'http://%s/embed/%s' % (domain, video_id), video_id)
52         video_data = self._parse_json(self._search_regex(
53             r'Disney\.EmbedVideo=({.+});', webpage, 'embed data'), video_id)['video']
54
55         for external in video_data.get('externals', []):
56             if external.get('source') == 'vevo':
57                 return self.url_result('vevo:' + external['data_id'], 'Vevo')
58
59         title = video_data['title']
60
61         formats = []
62         for flavor in video_data.get('flavors', []):
63             flavor_format = flavor.get('format')
64             flavor_url = flavor.get('url')
65             if not flavor_url or not re.match(r'https?://', flavor_url):
66                 continue
67             tbr = int_or_none(flavor.get('bitrate'))
68             if tbr == 99999:
69                 formats.extend(self._extract_m3u8_formats(
70                     flavor_url, video_id, 'mp4', m3u8_id=flavor_format, fatal=False))
71                 continue
72             format_id = []
73             if flavor_format:
74                 format_id.append(flavor_format)
75             if tbr:
76                 format_id.append(compat_str(tbr))
77             ext = determine_ext(flavor_url)
78             if flavor_format == 'applehttp' or ext == 'm3u8':
79                 ext = 'mp4'
80             width = int_or_none(flavor.get('width'))
81             height = int_or_none(flavor.get('height'))
82             formats.append({
83                 'format_id': '-'.join(format_id),
84                 'url': flavor_url,
85                 'width': width,
86                 'height': height,
87                 'tbr': tbr,
88                 'ext': ext,
89                 'vcodec': 'none' if (width == 0 and height == 0) else None,
90             })
91         self._sort_formats(formats)
92
93         subtitles = {}
94         for caption in video_data.get('captions', []):
95             caption_url = caption.get('url')
96             caption_format = caption.get('format')
97             if not caption_url or caption_format.startswith('unknown'):
98                 continue
99             subtitles.setdefault(caption.get('language', 'en'), []).append({
100                 'url': caption_url,
101                 'ext': {
102                     'webvtt': 'vtt',
103                 }.get(caption_format, caption_format),
104             })
105
106         return {
107             'id': video_id,
108             'title': title,
109             'description': video_data.get('description') or video_data.get('short_desc'),
110             'thumbnail': video_data.get('thumb') or video_data.get('thumb_secure'),
111             'duration': int_or_none(video_data.get('duration_sec')),
112             'upload_date': unified_strdate(video_data.get('publish_date')),
113             'formats': formats,
114             'subtitles': subtitles,
115         }