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