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