[cbs] add base extractor
[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 .cbs import CBSBaseIE
6 from ..utils import (
7     parse_duration,
8     find_xpath_attr,
9 )
10
11
12 class CBSNewsIE(CBSBaseIE):
13     IE_DESC = 'CBS News'
14     _VALID_URL = r'https?://(?: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 _real_extract(self, url):
53         video_id = self._match_id(url)
54
55         webpage = self._download_webpage(url, video_id)
56
57         video_info = self._parse_json(self._html_search_regex(
58             r'(?:<ul class="media-list items" id="media-related-items"><li data-video-info|<div id="cbsNewsVideoPlayer" data-video-player-options)=\'({.+?})\'',
59             webpage, 'video JSON info'), video_id)
60
61         item = video_info['item'] if 'item' in video_info else video_info
62         title = item.get('articleTitle') or item.get('hed')
63         duration = item.get('duration')
64         thumbnail = item.get('mediaImage') or item.get('thumbnail')
65
66         subtitles = {}
67         formats = []
68         for format_id in ['RtmpMobileLow', 'RtmpMobileHigh', 'Hls', 'RtmpDesktop']:
69             pid = item.get('media' + format_id)
70             if not pid:
71                 continue
72             release_url = 'http://link.theplatform.com/s/dJ5BDC/%s?mbr=true' % pid
73             tp_formats, tp_subtitles = self._extract_theplatform_smil(release_url, video_id, 'Downloading %s SMIL data' % pid)
74             formats.extend(tp_formats)
75             subtitles = self._merge_subtitles(subtitles, tp_subtitles)
76         self._sort_formats(formats)
77
78         return {
79             'id': video_id,
80             'title': title,
81             'thumbnail': thumbnail,
82             'duration': duration,
83             'formats': formats,
84             'subtitles': subtitles,
85         }
86
87
88 class CBSNewsLiveVideoIE(InfoExtractor):
89     IE_DESC = 'CBS News Live Videos'
90     _VALID_URL = r'https?://(?:www\.)?cbsnews\.com/live/video/(?P<id>[\da-z_-]+)'
91
92     _TEST = {
93         'url': 'http://www.cbsnews.com/live/video/clinton-sanders-prepare-to-face-off-in-nh/',
94         'info_dict': {
95             'id': 'clinton-sanders-prepare-to-face-off-in-nh',
96             'ext': 'flv',
97             'title': 'Clinton, Sanders Prepare To Face Off In NH',
98             'duration': 334,
99         },
100     }
101
102     def _real_extract(self, url):
103         video_id = self._match_id(url)
104
105         webpage = self._download_webpage(url, video_id)
106
107         video_info = self._parse_json(self._html_search_regex(
108             r'data-story-obj=\'({.+?})\'', webpage, 'video JSON info'), video_id)['story']
109
110         hdcore_sign = 'hdcore=3.3.1'
111         f4m_formats = self._extract_f4m_formats(video_info['url'] + '&' + hdcore_sign, video_id)
112         if f4m_formats:
113             for entry in f4m_formats:
114                 # URLs without the extra param induce an 404 error
115                 entry.update({'extra_param_to_segment_url': hdcore_sign})
116         self._sort_formats(f4m_formats)
117
118         return {
119             'id': video_id,
120             'title': video_info['headline'],
121             'thumbnail': video_info.get('thumbnail_url_hd') or video_info.get('thumbnail_url_sd'),
122             'duration': parse_duration(video_info.get('segmentDur')),
123             'formats': f4m_formats,
124         }