[nytimes] Add support for nytimes.com (Closes #2846)
[youtube-dl] / youtube_dl / extractor / nytimes.py
1 from __future__ import unicode_literals
2
3 import re
4
5 from .common import InfoExtractor
6 from ..utils import parse_iso8601
7
8
9 class NYTimesIE(InfoExtractor):
10     _VALID_URL = r'https?://(?:www\.)?nytimes\.com/video/(?:[^/]+/)+(?P<id>\d+)'
11
12     _TEST = {
13         'url': 'http://www.nytimes.com/video/opinion/100000002847155/verbatim-what-is-a-photocopier.html?playlistId=100000001150263',
14         'md5': '18a525a510f942ada2720db5f31644c0',
15         'info_dict': {
16             'id': '100000002847155',
17             'ext': 'mov',
18             'title': 'Verbatim: What Is a Photocopier?',
19             'description': 'md5:93603dada88ddbda9395632fdc5da260',
20             'timestamp': 1398631707,
21             'upload_date': '20140427',
22             'uploader': 'Brett Weiner',
23             'duration': 419,
24         }
25     }
26
27     def _real_extract(self, url):
28         mobj = re.match(self._VALID_URL, url)
29         video_id = mobj.group('id')
30
31         video_data = self._download_json(
32             'http://www.nytimes.com/svc/video/api/v2/video/%s' % video_id, video_id, 'Downloading video JSON')
33
34         title = video_data['headline']
35         description = video_data['summary']
36         duration = video_data['duration'] / 1000.0
37
38         uploader = video_data['byline']
39         timestamp = parse_iso8601(video_data['publication_date'][:-8])
40
41         formats = [
42             {
43                 'url': video['url'],
44                 'format_id': video['type'],
45                 'vcodec': video['video_codec'],
46                 'width': video['width'],
47                 'height': video['height'],
48                 'filesize': video['fileSize'],
49             } for video in video_data['renditions']
50         ]
51         self._sort_formats(formats)
52
53         thumbnails = [
54             {
55                 'url': 'http://www.nytimes.com/%s' % image['url'],
56                 'resolution': '%dx%d' % (image['width'], image['height']),
57             } for image in video_data['images']
58         ]
59
60         return {
61             'id': video_id,
62             'title': title,
63             'description': description,
64             'timestamp': timestamp,
65             'uploader': uploader,
66             'duration': duration,
67             'formats': formats,
68             'thumbnails': thumbnails,
69         }