]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/nytimes.py
Rebuild with only Python 3 support.
[youtubedl] / 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 NYTimesBaseIE(InfoExtractor):
12 def _extract_video_from_id(self, video_id):
13 video_data = self._download_json(
14 'http://www.nytimes.com/svc/video/api/v2/video/%s' % video_id,
15 video_id, 'Downloading video JSON')
16
17 title = video_data['headline']
18 description = video_data.get('summary')
19 duration = float_or_none(video_data.get('duration'), 1000)
20
21 uploader = video_data.get('byline')
22 publication_date = video_data.get('publication_date')
23 timestamp = parse_iso8601(publication_date[:-8]) if publication_date else None
24
25 def get_file_size(file_size):
26 if isinstance(file_size, int):
27 return file_size
28 elif isinstance(file_size, dict):
29 return int(file_size.get('value', 0))
30 else:
31 return 0
32
33 formats = [
34 {
35 'url': video['url'],
36 'format_id': video.get('type'),
37 'vcodec': video.get('video_codec'),
38 'width': int_or_none(video.get('width')),
39 'height': int_or_none(video.get('height')),
40 'filesize': get_file_size(video.get('fileSize')),
41 } for video in video_data['renditions'] if video.get('url')
42 ]
43 self._sort_formats(formats)
44
45 thumbnails = [
46 {
47 'url': 'http://www.nytimes.com/%s' % image['url'],
48 'width': int_or_none(image.get('width')),
49 'height': int_or_none(image.get('height')),
50 } for image in video_data.get('images', []) if image.get('url')
51 ]
52
53 return {
54 'id': video_id,
55 'title': title,
56 'description': description,
57 'timestamp': timestamp,
58 'uploader': uploader,
59 'duration': duration,
60 'formats': formats,
61 'thumbnails': thumbnails,
62 }
63
64
65 class NYTimesIE(NYTimesBaseIE):
66 _VALID_URL = r'https?://(?:(?:www\.)?nytimes\.com/video/(?:[^/]+/)+?|graphics8\.nytimes\.com/bcvideo/\d+(?:\.\d+)?/iframe/embed\.html\?videoId=)(?P<id>\d+)'
67
68 _TESTS = [{
69 'url': 'http://www.nytimes.com/video/opinion/100000002847155/verbatim-what-is-a-photocopier.html?playlistId=100000001150263',
70 'md5': '18a525a510f942ada2720db5f31644c0',
71 'info_dict': {
72 'id': '100000002847155',
73 'ext': 'mov',
74 'title': 'Verbatim: What Is a Photocopier?',
75 'description': 'md5:93603dada88ddbda9395632fdc5da260',
76 'timestamp': 1398631707,
77 'upload_date': '20140427',
78 'uploader': 'Brett Weiner',
79 'duration': 419,
80 }
81 }, {
82 'url': 'http://www.nytimes.com/video/travel/100000003550828/36-hours-in-dubai.html',
83 'only_matching': True,
84 }]
85
86 def _real_extract(self, url):
87 video_id = self._match_id(url)
88
89 return self._extract_video_from_id(video_id)
90
91
92 class NYTimesArticleIE(NYTimesBaseIE):
93 _VALID_URL = r'https?://(?:www\.)?nytimes\.com/(.(?<!video))*?/(?:[^/]+/)*(?P<id>[^.]+)(?:\.html)?'
94 _TESTS = [{
95 'url': 'http://www.nytimes.com/2015/04/14/business/owner-of-gravity-payments-a-credit-card-processor-is-setting-a-new-minimum-wage-70000-a-year.html?_r=0',
96 'md5': 'e2076d58b4da18e6a001d53fd56db3c9',
97 'info_dict': {
98 'id': '100000003628438',
99 'ext': 'mov',
100 'title': 'New Minimum Wage: $70,000 a Year',
101 'description': 'Dan Price, C.E.O. of Gravity Payments, surprised his 120-person staff by announcing that he planned over the next three years to raise the salary of every employee to $70,000 a year.',
102 'timestamp': 1429033037,
103 'upload_date': '20150414',
104 'uploader': 'Matthew Williams',
105 }
106 }, {
107 'url': 'http://www.nytimes.com/news/minute/2014/03/17/times-minute-whats-next-in-crimea/?_php=true&_type=blogs&_php=true&_type=blogs&_r=1',
108 'only_matching': True,
109 }]
110
111 def _real_extract(self, url):
112 video_id = self._match_id(url)
113
114 webpage = self._download_webpage(url, video_id)
115
116 video_id = self._html_search_regex(r'data-videoid="(\d+)"', webpage, 'video id')
117
118 return self._extract_video_from_id(video_id)