[cbs,cbsnews,cbssports] reduce requests while extracting all formats
[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': 'SNJBOYzXiWBOvaLsdzwH8fmtP1SCd91Y',
34                 'ext': 'mp4',
35                 'title': 'Fort Hood shooting: Army downplays mental illness as cause of attack',
36                 'description': 'md5:4a6983e480542d8b333a947bfc64ddc7',
37                 'upload_date': '19700101',
38                 'uploader': 'CBSI-NEW',
39                 'thumbnail': 're:^https?://.*\.jpg$',
40                 'duration': 205,
41                 'subtitles': {
42                     'en': [{
43                         'ext': 'ttml',
44                     }],
45                 },
46             },
47             'params': {
48                 # m3u8 download
49                 'skip_download': True,
50             },
51         },
52     ]
53
54     def _real_extract(self, url):
55         video_id = self._match_id(url)
56
57         webpage = self._download_webpage(url, video_id)
58
59         video_info = self._parse_json(self._html_search_regex(
60             r'(?:<ul class="media-list items" id="media-related-items"><li data-video-info|<div id="cbsNewsVideoPlayer" data-video-player-options)=\'({.+?})\'',
61             webpage, 'video JSON info'), video_id)
62
63         item = video_info['item'] if 'item' in video_info else video_info
64         guid = item['mpxRefId']
65         return self._extract_video_info('byGuid=%s' % guid, guid)
66
67
68 class CBSNewsLiveVideoIE(InfoExtractor):
69     IE_DESC = 'CBS News Live Videos'
70     _VALID_URL = r'https?://(?:www\.)?cbsnews\.com/live/video/(?P<id>[\da-z_-]+)'
71
72     _TEST = {
73         'url': 'http://www.cbsnews.com/live/video/clinton-sanders-prepare-to-face-off-in-nh/',
74         'info_dict': {
75             'id': 'clinton-sanders-prepare-to-face-off-in-nh',
76             'ext': 'flv',
77             'title': 'Clinton, Sanders Prepare To Face Off In NH',
78             'duration': 334,
79         },
80     }
81
82     def _real_extract(self, url):
83         video_id = self._match_id(url)
84
85         webpage = self._download_webpage(url, video_id)
86
87         video_info = self._parse_json(self._html_search_regex(
88             r'data-story-obj=\'({.+?})\'', webpage, 'video JSON info'), video_id)['story']
89
90         hdcore_sign = 'hdcore=3.3.1'
91         f4m_formats = self._extract_f4m_formats(video_info['url'] + '&' + hdcore_sign, video_id)
92         if f4m_formats:
93             for entry in f4m_formats:
94                 # URLs without the extra param induce an 404 error
95                 entry.update({'extra_param_to_segment_url': hdcore_sign})
96         self._sort_formats(f4m_formats)
97
98         return {
99             'id': video_id,
100             'title': video_info['headline'],
101             'thumbnail': video_info.get('thumbnail_url_hd') or video_info.get('thumbnail_url_sd'),
102             'duration': parse_duration(video_info.get('segmentDur')),
103             'formats': f4m_formats,
104         }