f35f686efac091587cb718f32f1e6ef0f16e6165
[youtube-dl] / youtube_dl / extractor / cbsinteractive.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .theplatform import ThePlatformIE
7 from ..compat import compat_urllib_parse
8 from ..utils import int_or_none
9
10
11 class CBSInteractiveIE(ThePlatformIE):
12     _VALID_URL = r'https?://(?:www\.)?(?P<site>cnet|zdnet)\.com/(?:videos|video/share)/(?P<id>[^/?]+)'
13     _TESTS = [{
14         'url': 'http://www.cnet.com/videos/hands-on-with-microsofts-windows-8-1-update/',
15         'md5': '041233212a0d06b179c87cbcca1577b8',
16         'info_dict': {
17             'id': '56f4ea68-bd21-4852-b08c-4de5b8354c60',
18             'ext': 'mp4',
19             'title': 'Hands-on with Microsoft Windows 8.1 Update',
20             'description': 'The new update to the Windows 8 OS brings improved performance for mouse and keyboard users.',
21             'uploader_id': '6085384d-619e-11e3-b231-14feb5ca9861',
22             'uploader': 'Sarah Mitroff',
23             'duration': 70,
24             'timestamp': 1396479627,
25             'upload_date': '20140402',
26         },
27         'params': {
28             'format': 'mp4',
29         },
30     }, {
31         'url': 'http://www.cnet.com/videos/whiny-pothole-tweets-at-local-government-when-hit-by-cars-tomorrow-daily-187/',
32         'md5': 'f2b16d73e08d69591dd9e25564695c0c',
33         'info_dict': {
34             'id': '56527b93-d25d-44e3-b738-f989ce2e49ba',
35             'ext': 'mp4',
36             'title': 'Whiny potholes tweet at local government when hit by cars (Tomorrow Daily 187)',
37             'description': 'Khail and Ashley wonder what other civic woes can be solved by self-tweeting objects, investigate a new kind of VR camera and watch an origami robot self-assemble, walk, climb, dig and dissolve. #TDPothole',
38             'uploader_id': 'b163284d-6b73-44fc-b3e6-3da66c392d40',
39             'uploader': 'Ashley Esqueda',
40             'duration': 1482,
41             'timestamp': 1433289889,
42             'upload_date': '20150603',
43         },
44         'params': {
45             'format': 'mp4',
46         },
47     }, {
48         'url': 'http://www.zdnet.com/video/share/video-keeping-android-smartphones-and-tablets-secure/',
49         'info_dict': {
50             'id': 'bc1af9f0-a2b5-4e54-880d-0d95525781c0',
51             'ext': 'mp4',
52             'title': 'Video: Keeping Android smartphones and tablets secure',
53             'description': 'Here\'s the best way to keep Android devices secure, and what you do when they\'ve come to the end of their lives.',
54             'uploader_id': 'f2d97ea2-8175-11e2-9d12-0018fe8a00b0',
55             'uploader': 'Adrian Kingsley-Hughes',
56             'timestamp': 1449129925,
57             'upload_date': '20151203',
58         },
59         'params': {
60             # m3u8 download
61             'skip_download': True,
62         }
63     }]
64     TP_RELEASE_URL_TEMPLATE = 'http://link.theplatform.com/s/kYEXFC/%s?mbr=true'
65     MPX_ACCOUNTS = {
66         'cnet': 2288573011,
67         'zdnet': 2387448114,
68     }
69
70     def _real_extract(self, url):
71         site, display_id = re.match(self._VALID_URL, url).groups()
72         webpage = self._download_webpage(url, display_id)
73
74         data_json = self._html_search_regex(
75             r"data-(?:cnet|zdnet)-video(?:-uvp(?:js)?)?-options='([^']+)'",
76             webpage, 'data json')
77         data = self._parse_json(data_json, display_id)
78         vdata = data.get('video') or data['videos'][0]
79
80         video_id = vdata['id']
81         title = vdata['title']
82         author = vdata.get('author')
83         if author:
84             uploader = '%s %s' % (author['firstName'], author['lastName'])
85             uploader_id = author.get('id')
86         else:
87             uploader = None
88             uploader_id = None
89
90         media_guid_path = 'media/guid/%d/%s' % (self.MPX_ACCOUNTS[site], vdata['mpxRefId'])
91         formats, subtitles = [], {}
92         for (fkey, vid) in vdata.get('files', {}).items():
93             if fkey == 'hls_phone' and 'hls_tablet' in vdata['files']:
94                 continue
95             release_url = self.TP_RELEASE_URL_TEMPLATE % vid
96             if fkey == 'hds':
97                 release_url += '&manifest=f4m'
98             tp_formats, tp_subtitles = self._extract_theplatform_smil(release_url, video_id, 'Downloading %s SMIL data' % fkey)
99             formats.extend(tp_formats)
100             subtitles = self._merge_subtitles(subtitles, tp_subtitles)
101
102         if 'm3u8' in vdata:
103             parsed_url = compat_urllib_parse.urlparse(url)
104             m3u8_url = ('%s://%s%s'
105                 % (parsed_url.scheme, parsed_url.netloc, vdata['m3u8']))
106             m3u8_formats = self._extract_m3u8_formats(m3u8_url, video_id)
107             for format in m3u8_formats:
108                 format['url'] = format['url'].replace('https://', 'http://')
109             formats.extend(m3u8_formats)
110
111         if 'mp4' in vdata:
112             formats.append({
113                 'url': vdata['mp4'],
114                 'format_id': 'mp4',
115                 'ext': 'mp4',
116             })
117
118         self._sort_formats(formats)
119
120         info = self._extract_theplatform_metadata('kYEXFC/%s' % media_guid_path, video_id)
121         info.update({
122             'id': video_id,
123             'display_id': display_id,
124             'title': title,
125             'duration': int_or_none(vdata.get('duration')),
126             'uploader': uploader,
127             'uploader_id': uploader_id,
128             'subtitles': subtitles,
129             'formats': formats,
130         })
131         return info