Merge pull request #12861 from Tithen-Firion/cbsinteractive-fix
[youtube-dl] / youtube_dl / extractor / dailymail.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 from .common import InfoExtractor
5 from ..compat import compat_str
6 from ..utils import (
7     int_or_none,
8     determine_protocol,
9     try_get,
10     unescapeHTML,
11 )
12
13
14 class DailyMailIE(InfoExtractor):
15     _VALID_URL = r'https?://(?:www\.)?dailymail\.co\.uk/video/[^/]+/video-(?P<id>[0-9]+)'
16     _TEST = {
17         'url': 'http://www.dailymail.co.uk/video/tvshowbiz/video-1295863/The-Mountain-appears-sparkling-water-ad-Heavy-Bubbles.html',
18         'md5': 'f6129624562251f628296c3a9ffde124',
19         'info_dict': {
20             'id': '1295863',
21             'ext': 'mp4',
22             'title': 'The Mountain appears in sparkling water ad for \'Heavy Bubbles\'',
23             'description': 'md5:a93d74b6da172dd5dc4d973e0b766a84',
24         }
25     }
26
27     def _real_extract(self, url):
28         video_id = self._match_id(url)
29         webpage = self._download_webpage(url, video_id)
30         video_data = self._parse_json(self._search_regex(
31             r"data-opts='({.+?})'", webpage, 'video data'), video_id)
32         title = unescapeHTML(video_data['title'])
33
34         sources_url = (try_get(
35             video_data,
36             (lambda x: x['plugins']['sources']['url'],
37              lambda x: x['sources']['url']), compat_str) or
38             'http://www.dailymail.co.uk/api/player/%s/video-sources.json' % video_id)
39
40         video_sources = self._download_json(sources_url, video_id)
41
42         formats = []
43         for rendition in video_sources['renditions']:
44             rendition_url = rendition.get('url')
45             if not rendition_url:
46                 continue
47             tbr = int_or_none(rendition.get('encodingRate'), 1000)
48             container = rendition.get('videoContainer')
49             is_hls = container == 'M2TS'
50             protocol = 'm3u8_native' if is_hls else determine_protocol({'url': rendition_url})
51             formats.append({
52                 'format_id': ('hls' if is_hls else protocol) + ('-%d' % tbr if tbr else ''),
53                 'url': rendition_url,
54                 'width': int_or_none(rendition.get('frameWidth')),
55                 'height': int_or_none(rendition.get('frameHeight')),
56                 'tbr': tbr,
57                 'vcodec': rendition.get('videoCodec'),
58                 'container': container,
59                 'protocol': protocol,
60                 'ext': 'mp4' if is_hls else None,
61             })
62         self._sort_formats(formats)
63
64         return {
65             'id': video_id,
66             'title': title,
67             'description': unescapeHTML(video_data.get('descr')),
68             'thumbnail': video_data.get('poster') or video_data.get('thumbnail'),
69             'formats': formats,
70         }