[cbs] improve extraction(closes #6321)
[youtube-dl] / youtube_dl / extractor / cbs.py
1 from __future__ import unicode_literals
2
3 from .theplatform import ThePlatformIE
4 from ..utils import (
5     xpath_text,
6     xpath_element,
7     int_or_none,
8     ExtractorError,
9     find_xpath_attr,
10 )
11
12
13 class CBSIE(ThePlatformIE):
14     _VALID_URL = r'https?://(?:www\.)?(?:cbs\.com/shows/[^/]+/(?:video|artist)|colbertlateshow\.com/(?:video|podcasts))/[^/]+/(?P<id>[^/]+)'
15
16     _TESTS = [{
17         'url': 'http://www.cbs.com/shows/garth-brooks/video/_u7W953k6la293J7EPTd9oHkSPs6Xn6_/connect-chat-feat-garth-brooks/',
18         'info_dict': {
19             'id': '_u7W953k6la293J7EPTd9oHkSPs6Xn6_',
20             'display_id': 'connect-chat-feat-garth-brooks',
21             'ext': 'mp4',
22             'title': 'Connect Chat feat. Garth Brooks',
23             'description': 'Connect with country music singer Garth Brooks, as he chats with fans on Wednesday November 27, 2013. Be sure to tune in to Garth Brooks: Live from Las Vegas, Friday November 29, at 9/8c on CBS!',
24             'duration': 1495,
25         },
26         'params': {
27             # rtmp download
28             'skip_download': True,
29         },
30         '_skip': 'Blocked outside the US',
31     }, {
32         'url': 'http://www.cbs.com/shows/liveonletterman/artist/221752/st-vincent/',
33         'info_dict': {
34             'id': 'WWF_5KqY3PK1',
35             'display_id': 'st-vincent',
36             'ext': 'flv',
37             'title': 'Live on Letterman - St. Vincent',
38             'description': 'Live On Letterman: St. Vincent in concert from New York\'s Ed Sullivan Theater on Tuesday, July 16, 2014.',
39             'duration': 3221,
40         },
41         'params': {
42             # rtmp download
43             'skip_download': True,
44         },
45         '_skip': 'Blocked outside the US',
46     }, {
47         'url': 'http://colbertlateshow.com/video/8GmB0oY0McANFvp2aEffk9jZZZ2YyXxy/the-colbeard/',
48         'only_matching': True,
49     }, {
50         'url': 'http://www.colbertlateshow.com/podcasts/dYSwjqPs_X1tvbV_P2FcPWRa_qT6akTC/in-the-bad-room-with-stephen/',
51         'only_matching': True,
52     }]
53     TP_RELEASE_URL_TEMPLATE = 'http://link.theplatform.com/s/dJ5BDC/%s?manifest=m3u&mbr=true'
54
55     def _parse_smil_subtitles(self, smil, namespace=None, subtitles_lang='en'):
56         closed_caption_e = find_xpath_attr(smil, self._xpath_ns('.//param', namespace), 'name', 'ClosedCaptionURL')
57         return {
58             'en': [{
59                 'ext': 'ttml',
60                 'url': closed_caption_e.attrib['value'],
61             }]
62         } if closed_caption_e is not None and closed_caption_e.attrib.get('value') else []
63
64     def _real_extract(self, url):
65         display_id = self._match_id(url)
66         webpage = self._download_webpage(url, display_id)
67         content_id = self._search_regex(
68             [r"video\.settings\.content_id\s*=\s*'([^']+)';", r"cbsplayer\.contentId\s*=\s*'([^']+)';"],
69             webpage, 'content id')
70         items_data = self._download_xml(
71             'http://can.cbs.com/thunder/player/videoPlayerService.php',
72             content_id, query={'partner': 'cbs', 'contentId': content_id})
73         video_data = xpath_element(items_data, './/item')
74         title = xpath_text(video_data, 'videoTitle', 'title', True)
75
76         subtitles = {}
77         formats = []
78         for item in items_data.findall('.//item'):
79             pid = xpath_text(item, 'pid')
80             if not pid:
81                 continue
82             try:
83                 tp_formats, tp_subtitles = self._extract_theplatform_smil(
84                     self.TP_RELEASE_URL_TEMPLATE % pid, content_id, 'Downloading %s SMIL data' % pid)
85             except ExtractorError:
86                 continue
87             formats.extend(tp_formats)
88             subtitles = self._merge_subtitles(subtitles, tp_subtitles)
89         self._sort_formats(formats)
90
91         info = self.get_metadata('dJ5BDC/media/guid/2198311517/%s' % content_id, content_id)
92         info.update({
93             'id': content_id,
94             'display_id': display_id,
95             'title': title,
96             'series': xpath_text(video_data, 'seriesTitle'),
97             'season_number': int_or_none(xpath_text(video_data, 'seasonNumber')),
98             'episode_number': int_or_none(xpath_text(video_data, 'episodeNumber')),
99             'duration': int_or_none(xpath_text(video_data, 'videoLength'), 1000),
100             'thumbnail': xpath_text(video_data, 'previewImageURL'),
101             'formats': formats,
102             'subtitles': subtitles,
103         })
104         return info