Merge branch 'charlierose' of https://github.com/TRox1972/youtube-dl into TRox1972...
[youtube-dl] / youtube_dl / extractor / cbs.py
1 from __future__ import unicode_literals
2
3 from .theplatform import ThePlatformFeedIE
4 from ..utils import (
5     int_or_none,
6     find_xpath_attr,
7     ExtractorError,
8 )
9
10
11 class CBSBaseIE(ThePlatformFeedIE):
12     def _parse_smil_subtitles(self, smil, namespace=None, subtitles_lang='en'):
13         closed_caption_e = find_xpath_attr(smil, self._xpath_ns('.//param', namespace), 'name', 'ClosedCaptionURL')
14         return {
15             'en': [{
16                 'ext': 'ttml',
17                 'url': closed_caption_e.attrib['value'],
18             }]
19         } if closed_caption_e is not None and closed_caption_e.attrib.get('value') else []
20
21
22 class CBSIE(CBSBaseIE):
23     _VALID_URL = r'(?:cbs:|https?://(?:www\.)?(?:cbs\.com/shows/[^/]+/video|colbertlateshow\.com/(?:video|podcasts))/)(?P<id>[\w-]+)'
24
25     _TESTS = [{
26         'url': 'http://www.cbs.com/shows/garth-brooks/video/_u7W953k6la293J7EPTd9oHkSPs6Xn6_/connect-chat-feat-garth-brooks/',
27         'info_dict': {
28             'id': '_u7W953k6la293J7EPTd9oHkSPs6Xn6_',
29             'ext': 'mp4',
30             'title': 'Connect Chat feat. Garth Brooks',
31             '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!',
32             'duration': 1495,
33             'timestamp': 1385585425,
34             'upload_date': '20131127',
35             'uploader': 'CBSI-NEW',
36         },
37         'params': {
38             # m3u8 download
39             'skip_download': True,
40         },
41         '_skip': 'Blocked outside the US',
42     }, {
43         'url': 'http://colbertlateshow.com/video/8GmB0oY0McANFvp2aEffk9jZZZ2YyXxy/the-colbeard/',
44         'only_matching': True,
45     }, {
46         'url': 'http://www.colbertlateshow.com/podcasts/dYSwjqPs_X1tvbV_P2FcPWRa_qT6akTC/in-the-bad-room-with-stephen/',
47         'only_matching': True,
48     }]
49
50     def _extract_video_info(self, guid):
51         path = 'dJ5BDC/media/guid/2198311517/' + guid
52         smil_url = 'http://link.theplatform.com/s/%s?mbr=true' % path
53         formats, subtitles = self._extract_theplatform_smil(smil_url + '&manifest=m3u', guid)
54         for r in ('HLS&formats=M3U', 'RTMP', 'WIFI', '3G'):
55             try:
56                 tp_formats, _ = self._extract_theplatform_smil(smil_url + '&assetTypes=' + r, guid, 'Downloading %s SMIL data' % r.split('&')[0])
57                 formats.extend(tp_formats)
58             except ExtractorError:
59                 continue
60         self._sort_formats(formats)
61         metadata = self._download_theplatform_metadata(path, guid)
62         info = self._parse_theplatform_metadata(metadata)
63         info.update({
64             'id': guid,
65             'formats': formats,
66             'subtitles': subtitles,
67             'series': metadata.get('cbs$SeriesTitle'),
68             'season_number': int_or_none(metadata.get('cbs$SeasonNumber')),
69             'episode': metadata.get('cbs$EpisodeTitle'),
70             'episode_number': int_or_none(metadata.get('cbs$EpisodeNumber')),
71         })
72         return info
73
74     def _real_extract(self, url):
75         content_id = self._match_id(url)
76         return self._extract_video_info(content_id)