[cbsinteractive] fix extractor
[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         'info_dict': {
16             'id': '56f4ea68-bd21-4852-b08c-4de5b8354c60',
17             'ext': 'flv',
18             'title': 'Hands-on with Microsoft Windows 8.1 Update',
19             'description': 'The new update to the Windows 8 OS brings improved performance for mouse and keyboard users.',
20             'uploader_id': '6085384d-619e-11e3-b231-14feb5ca9861',
21             'uploader': 'Sarah Mitroff',
22             'duration': 70,
23             'timestamp': 1396479627,
24             'upload_date': '20140402',
25         },
26     }, {
27         'url': 'http://www.cnet.com/videos/whiny-pothole-tweets-at-local-government-when-hit-by-cars-tomorrow-daily-187/',
28         'info_dict': {
29             'id': '56527b93-d25d-44e3-b738-f989ce2e49ba',
30             'ext': 'flv',
31             'title': 'Whiny potholes tweet at local government when hit by cars (Tomorrow Daily 187)',
32             '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',
33             'uploader_id': 'b163284d-6b73-44fc-b3e6-3da66c392d40',
34             'uploader': 'Ashley Esqueda',
35             'duration': 1482,
36             'timestamp': 1433289889,
37             'upload_date': '20150603',
38         },
39     }, {
40         'url': 'http://www.zdnet.com/video/share/video-keeping-android-smartphones-and-tablets-secure/',
41         'info_dict': {
42             'id': 'bc1af9f0-a2b5-4e54-880d-0d95525781c0',
43             'ext': 'mp4',
44             'title': 'Video: Keeping Android smartphones and tablets secure',
45             '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.',
46             'uploader_id': 'f2d97ea2-8175-11e2-9d12-0018fe8a00b0',
47             'uploader': 'Adrian Kingsley-Hughes',
48             'timestamp': 1448961720,
49             'upload_date': '20151201',
50         },
51         'params': {
52             # m3u8 download
53             'skip_download': True,
54         }
55     }]
56     TP_RELEASE_URL_TEMPLATE = 'http://link.theplatform.com/s/kYEXFC/%s?mbr=true'
57     MPX_ACCOUNTS = {
58         'cnet': 2288573011,
59         'zdnet': 2387448114,
60     }
61
62     def _real_extract(self, url):
63         site, display_id = re.match(self._VALID_URL, url).groups()
64         webpage = self._download_webpage(url, display_id)
65
66         data_json = self._html_search_regex(
67             r"data-(?:cnet|zdnet)-video(?:-uvp(?:js)?)?-options='([^']+)'",
68             webpage, 'data json')
69         data = self._parse_json(data_json, display_id)
70         vdata = data.get('video') or data['videos'][0]
71
72         video_id = vdata['id']
73         title = vdata['title']
74         author = vdata.get('author')
75         if author:
76             uploader = '%s %s' % (author['firstName'], author['lastName'])
77             uploader_id = author.get('id')
78         else:
79             uploader = None
80             uploader_id = None
81
82         media_guid_path = 'media/guid/%d/%s' % (self.MPX_ACCOUNTS[site], vdata['mpxRefId'])
83         formats, subtitles = [], {}
84         for (fkey, vid) in vdata.get('files', {}).items():
85             if fkey == 'hls_phone' and 'hls_tablet' in vdata['files']:
86                 continue
87             release_url = self.TP_RELEASE_URL_TEMPLATE % vid
88             if fkey == 'hds':
89                 release_url += '&manifest=f4m'
90             tp_formats, tp_subtitles = self._extract_theplatform_smil(release_url, video_id, 'Downloading %s SMIL data' % fkey)
91             formats.extend(tp_formats)
92             subtitles = self._merge_subtitles(subtitles, tp_subtitles)
93
94         if 'm3u8' in vdata:
95             parsed_url = compat_urllib_parse.urlparse(url)
96             m3u8_url = ('%s://%s%s'
97                 % (parsed_url.scheme, parsed_url.netloc, vdata['m3u8']))
98             m3u8_formats = self._extract_m3u8_formats(m3u8_url, video_id)
99             for format in m3u8_formats:
100                 format['url'] = format['url'].replace('https://', 'http://')
101             formats.extend(m3u8_formats)
102
103         if 'mp4' in vdata:
104             formats.append({
105                 'url': vdata['mp4'],
106                 'format_id': 'mp4',
107                 'ext': 'mp4',
108             })
109
110         self._sort_formats(formats)
111
112         info = self._extract_theplatform_metadata('kYEXFC/%s' % media_guid_path, video_id)
113         info.update({
114             'id': video_id,
115             'display_id': display_id,
116             'title': title,
117             'duration': int_or_none(vdata.get('duration')),
118             'uploader': uploader,
119             'uploader_id': uploader_id,
120             'subtitles': subtitles,
121             'formats': formats,
122         })
123         return info