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