Merge pull request #2681 from phihag/readme-dev-instructions
[youtube-dl] / youtube_dl / extractor / cnet.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import json
5 import re
6
7 from .common import InfoExtractor
8 from ..utils import (
9     int_or_none,
10 )
11
12
13 class CNETIE(InfoExtractor):
14     _VALID_URL = r'https?://(?:www\.)?cnet\.com/videos/(?P<id>[^/]+)/'
15     _TEST = {
16         'url': 'http://www.cnet.com/videos/hands-on-with-microsofts-windows-8-1-update/',
17         'md5': '041233212a0d06b179c87cbcca1577b8',
18         'info_dict': {
19             'id': '56f4ea68-bd21-4852-b08c-4de5b8354c60',
20             'ext': 'mp4',
21             'title': 'Hands-on with Microsoft Windows 8.1 Update',
22             'description': 'The new update to the Windows 8 OS brings improved performance for mouse and keyboard users.',
23             'thumbnail': 're:^http://.*/flmswindows8.jpg$',
24             'uploader_id': 'sarah.mitroff@cbsinteractive.com',
25             'uploader': 'Sarah Mitroff',
26         }
27     }
28
29     def _real_extract(self, url):
30         mobj = re.match(self._VALID_URL, url)
31         display_id = mobj.group('id')
32
33         webpage = self._download_webpage(url, display_id)
34         data_json = self._html_search_regex(
35             r"<div class=\"cnetVideoPlayer\" data-cnet-video-options='([^']+)'",
36             webpage, 'data json')
37         data = json.loads(data_json)
38         vdata = data['video']
39
40         video_id = vdata['id']
41         title = vdata['headline']
42         description = vdata.get('dek')
43         thumbnail = vdata.get('image', {}).get('path')
44         author = vdata.get('author')
45         if author:
46             uploader = '%s %s' % (author['firstName'], author['lastName'])
47             uploader_id = author.get('email')
48         else:
49             uploader = None
50             uploader_id = None
51
52         formats = [{
53             'format_id': '%s-%s-%s' % (
54                 f['type'], f['format'],
55                 int_or_none(f.get('bitrate'), 1000, default='')),
56             'url': f['uri'],
57             'tbr': int_or_none(f.get('bitrate'), 1000),
58         } for f in vdata['files']['data']]
59         self._sort_formats(formats)
60
61         return {
62             'id': video_id,
63             'display_id': display_id,
64             'title': title,
65             'formats': formats,
66             'description': description,
67             'uploader': uploader,
68             'uploader_id': uploader_id,
69             'thumbnail': thumbnail,
70         }