Merge pull request #8513 from remitamine/dash-sort
[youtube-dl] / youtube_dl / extractor / cbsnews.py
1 # encoding: utf-8
2 from __future__ import unicode_literals
3
4 from .common import InfoExtractor
5 from .theplatform import ThePlatformIE
6 from ..utils import (
7     parse_duration,
8     find_xpath_attr,
9 )
10
11
12 class CBSNewsIE(ThePlatformIE):
13     IE_DESC = 'CBS News'
14     _VALID_URL = r'http://(?:www\.)?cbsnews\.com/(?:news|videos)/(?P<id>[\da-z_-]+)'
15
16     _TESTS = [
17         {
18             'url': 'http://www.cbsnews.com/news/tesla-and-spacex-elon-musks-industrial-empire/',
19             'info_dict': {
20                 'id': 'tesla-and-spacex-elon-musks-industrial-empire',
21                 'ext': 'flv',
22                 'title': 'Tesla and SpaceX: Elon Musk\'s industrial empire',
23                 'thumbnail': 'http://beta.img.cbsnews.com/i/2014/03/30/60147937-2f53-4565-ad64-1bdd6eb64679/60-0330-pelley-640x360.jpg',
24                 'duration': 791,
25             },
26             'params': {
27                 # rtmp download
28                 'skip_download': True,
29             },
30         },
31         {
32             'url': 'http://www.cbsnews.com/videos/fort-hood-shooting-army-downplays-mental-illness-as-cause-of-attack/',
33             'info_dict': {
34                 'id': 'fort-hood-shooting-army-downplays-mental-illness-as-cause-of-attack',
35                 'ext': 'mp4',
36                 'title': 'Fort Hood shooting: Army downplays mental illness as cause of attack',
37                 'thumbnail': 're:^https?://.*\.jpg$',
38                 'duration': 205,
39                 'subtitles': {
40                     'en': [{
41                         'ext': 'ttml',
42                     }],
43                 },
44             },
45             'params': {
46                 # m3u8 download
47                 'skip_download': True,
48             },
49         },
50     ]
51
52     def _parse_smil_subtitles(self, smil, namespace=None, subtitles_lang='en'):
53         closed_caption_e = find_xpath_attr(smil, self._xpath_ns('.//param', namespace), 'name', 'ClosedCaptionURL')
54         return {
55             'en': [{
56                 'ext': 'ttml',
57                 'url': closed_caption_e.attrib['value'],
58             }]
59         } if closed_caption_e is not None and closed_caption_e.attrib.get('value') else []
60
61     def _real_extract(self, url):
62         video_id = self._match_id(url)
63
64         webpage = self._download_webpage(url, video_id)
65
66         video_info = self._parse_json(self._html_search_regex(
67             r'(?:<ul class="media-list items" id="media-related-items"><li data-video-info|<div id="cbsNewsVideoPlayer" data-video-player-options)=\'({.+?})\'',
68             webpage, 'video JSON info'), video_id)
69
70         item = video_info['item'] if 'item' in video_info else video_info
71         title = item.get('articleTitle') or item.get('hed')
72         duration = item.get('duration')
73         thumbnail = item.get('mediaImage') or item.get('thumbnail')
74
75         subtitles = {}
76         formats = []
77         for format_id in ['RtmpMobileLow', 'RtmpMobileHigh', 'Hls', 'RtmpDesktop']:
78             pid = item.get('media' + format_id)
79             if not pid:
80                 continue
81             release_url = 'http://link.theplatform.com/s/dJ5BDC/%s?format=SMIL&mbr=true' % pid
82             tp_formats, tp_subtitles = self._extract_theplatform_smil(release_url, video_id, 'Downloading %s SMIL data' % pid)
83             formats.extend(tp_formats)
84             subtitles = self._merge_subtitles(subtitles, tp_subtitles)
85         self._sort_formats(formats)
86
87         return {
88             'id': video_id,
89             'title': title,
90             'thumbnail': thumbnail,
91             'duration': duration,
92             'formats': formats,
93             'subtitles': subtitles,
94         }
95
96
97 class CBSNewsLiveVideoIE(InfoExtractor):
98     IE_DESC = 'CBS News Live Videos'
99     _VALID_URL = r'http://(?:www\.)?cbsnews\.com/live/video/(?P<id>[\da-z_-]+)'
100
101     _TEST = {
102         'url': 'http://www.cbsnews.com/live/video/clinton-sanders-prepare-to-face-off-in-nh/',
103         'info_dict': {
104             'id': 'clinton-sanders-prepare-to-face-off-in-nh',
105             'ext': 'flv',
106             'title': 'Clinton, Sanders Prepare To Face Off In NH',
107             'duration': 334,
108         },
109     }
110
111     def _real_extract(self, url):
112         video_id = self._match_id(url)
113
114         webpage = self._download_webpage(url, video_id)
115
116         video_info = self._parse_json(self._html_search_regex(
117             r'data-story-obj=\'({.+?})\'', webpage, 'video JSON info'), video_id)['story']
118
119         hdcore_sign = 'hdcore=3.3.1'
120         f4m_formats = self._extract_f4m_formats(video_info['url'] + '&' + hdcore_sign, video_id)
121         if f4m_formats:
122             for entry in f4m_formats:
123                 # URLs without the extra param induce an 404 error
124                 entry.update({'extra_param_to_segment_url': hdcore_sign})
125
126         return {
127             'id': video_id,
128             'title': video_info['headline'],
129             'thumbnail': video_info.get('thumbnail_url_hd') or video_info.get('thumbnail_url_sd'),
130             'duration': parse_duration(video_info.get('segmentDur')),
131             'formats': f4m_formats,
132         }