Merge branch 'daum-fix-clip' of https://github.com/ping/youtube-dl into ping-daum...
[youtube-dl] / youtube_dl / extractor / cbsnews.py
1 # encoding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5 import json
6
7 from .theplatform import ThePlatformIE
8
9
10 class CBSNewsIE(ThePlatformIE):
11     IE_DESC = 'CBS News'
12     _VALID_URL = r'http://(?:www\.)?cbsnews\.com/(?:[^/]+/)+(?P<id>[\da-z_-]+)'
13
14     _TESTS = [
15         {
16             'url': 'http://www.cbsnews.com/news/tesla-and-spacex-elon-musks-industrial-empire/',
17             'info_dict': {
18                 'id': 'tesla-and-spacex-elon-musks-industrial-empire',
19                 'ext': 'flv',
20                 'title': 'Tesla and SpaceX: Elon Musk\'s industrial empire',
21                 'thumbnail': 'http://beta.img.cbsnews.com/i/2014/03/30/60147937-2f53-4565-ad64-1bdd6eb64679/60-0330-pelley-640x360.jpg',
22                 'duration': 791,
23             },
24             'params': {
25                 # rtmp download
26                 'skip_download': True,
27             },
28         },
29         {
30             'url': 'http://www.cbsnews.com/videos/fort-hood-shooting-army-downplays-mental-illness-as-cause-of-attack/',
31             'info_dict': {
32                 'id': 'fort-hood-shooting-army-downplays-mental-illness-as-cause-of-attack',
33                 'ext': 'mp4',
34                 'title': 'Fort Hood shooting: Army downplays mental illness as cause of attack',
35                 'thumbnail': 're:^https?://.*\.jpg$',
36                 'duration': 205,
37                 'subtitles': {
38                     'en': [{
39                         'ext': 'ttml',
40                     }],
41                 },
42             },
43             'params': {
44                 # m3u8 download
45                 'skip_download': True,
46             },
47         },
48     ]
49
50     def _real_extract(self, url):
51         mobj = re.match(self._VALID_URL, url)
52         video_id = mobj.group('id')
53
54         webpage = self._download_webpage(url, video_id)
55
56         video_info = json.loads(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'))
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         if 'mpxRefId' in video_info:
67             subtitles['en'] = [{
68                 'ext': 'ttml',
69                 'url': 'http://www.cbsnews.com/videos/captions/%s.adb_xml' % video_info['mpxRefId'],
70             }]
71
72         formats = []
73         for format_id in ['RtmpMobileLow', 'RtmpMobileHigh', 'Hls', 'RtmpDesktop']:
74             pid = item.get('media' + format_id)
75             if not pid:
76                 continue
77             release_url = 'http://link.theplatform.com/s/dJ5BDC/%s?format=SMIL&mbr=true' % pid
78             tp_formats, tp_subtitles = self._extract_theplatform_smil(release_url, video_id, 'Downloading %s SMIL data' % pid)
79             formats.extend(tp_formats)
80             subtitles = self._merge_subtitles(subtitles, tp_subtitles)
81         self._sort_formats(formats)
82
83         return {
84             'id': video_id,
85             'title': title,
86             'thumbnail': thumbnail,
87             'duration': duration,
88             'formats': formats,
89             'subtitles': subtitles,
90         }