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