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