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