]> git.bitcoin.ninja Git - youtube-dl/blob - youtube_dl/extractor/nytimes.py
[SpankBang] Use python2.6 compatible string formatting spec
[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     _TESTS = [{
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         'url': 'http://www.nytimes.com/video/travel/100000003550828/36-hours-in-dubai.html',
29         'only_matching': True,
30     }]
31
32     def _real_extract(self, url):
33         video_id = self._match_id(url)
34
35         video_data = self._download_json(
36             'http://www.nytimes.com/svc/video/api/v2/video/%s' % video_id,
37             video_id, 'Downloading video JSON')
38
39         title = video_data['headline']
40         description = video_data.get('summary')
41         duration = float_or_none(video_data.get('duration'), 1000)
42
43         uploader = video_data['byline']
44         timestamp = parse_iso8601(video_data['publication_date'][:-8])
45
46         def get_file_size(file_size):
47             if isinstance(file_size, int):
48                 return file_size
49             elif isinstance(file_size, dict):
50                 return int(file_size.get('value', 0))
51             else:
52                 return 0
53
54         formats = [
55             {
56                 'url': video['url'],
57                 'format_id': video.get('type'),
58                 'vcodec': video.get('video_codec'),
59                 'width': int_or_none(video.get('width')),
60                 'height': int_or_none(video.get('height')),
61                 'filesize': get_file_size(video.get('fileSize')),
62             } for video in video_data['renditions']
63         ]
64         self._sort_formats(formats)
65
66         thumbnails = [
67             {
68                 'url': 'http://www.nytimes.com/%s' % image['url'],
69                 'width': int_or_none(image.get('width')),
70                 'height': int_or_none(image.get('height')),
71             } for image in video_data['images']
72         ]
73
74         return {
75             'id': video_id,
76             'title': title,
77             'description': description,
78             'timestamp': timestamp,
79             'uploader': uploader,
80             'duration': duration,
81             'formats': formats,
82             'thumbnails': thumbnails,
83         }