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