]>
Commit | Line | Data |
---|---|---|
4191779d RA |
1 | import hmac |
2 | import hashlib | |
3 | import base64 | |
4 | ||
d664de44 | 5 | from .common import InfoExtractor |
f3c0c667 | 6 | from ..utils import ( |
47da7823 | 7 | determine_ext, |
f3c0c667 S |
8 | float_or_none, |
9 | int_or_none, | |
74324a7a | 10 | js_to_json, |
4191779d | 11 | mimetype2ext, |
47da7823 S |
12 | parse_iso8601, |
13 | remove_start, | |
f3c0c667 | 14 | ) |
d664de44 S |
15 | |
16 | ||
50aa43b3 | 17 | class NYTimesBaseIE(InfoExtractor): |
4191779d RA |
18 | _SECRET = b'pX(2MbU2);4N{7J8)>YwKRJ+/pQ3JkiU2Q^V>mFYv6g6gYvt6v' |
19 | ||
50aa43b3 | 20 | def _extract_video_from_id(self, video_id): |
4191779d RA |
21 | # Authorization generation algorithm is reverse engineered from `signer` in |
22 | # http://graphics8.nytimes.com/video/vhs/vhs-2.x.min.js | |
23 | path = '/svc/video/api/v3/video/' + video_id | |
24 | hm = hmac.new(self._SECRET, (path + ':vhs').encode(), hashlib.sha512).hexdigest() | |
25 | video_data = self._download_json('http://www.nytimes.com' + path, video_id, 'Downloading video JSON', headers={ | |
26 | 'Authorization': 'NYTV ' + base64.b64encode(hm.encode()).decode(), | |
27 | 'X-NYTV': 'vhs', | |
28 | }, fatal=False) | |
29 | if not video_data: | |
30 | video_data = self._download_json( | |
31 | 'http://www.nytimes.com/svc/video/api/v2/video/' + video_id, | |
32 | video_id, 'Downloading video JSON') | |
d664de44 S |
33 | |
34 | title = video_data['headline'] | |
d664de44 | 35 | |
65157783 S |
36 | def get_file_size(file_size): |
37 | if isinstance(file_size, int): | |
38 | return file_size | |
39 | elif isinstance(file_size, dict): | |
40 | return int(file_size.get('value', 0)) | |
41 | else: | |
4191779d RA |
42 | return None |
43 | ||
44 | urls = [] | |
45 | formats = [] | |
47f4203d | 46 | subtitles = {} |
4191779d RA |
47 | for video in video_data.get('renditions', []): |
48 | video_url = video.get('url') | |
49 | format_id = video.get('type') | |
50 | if not video_url or format_id == 'thumbs' or video_url in urls: | |
51 | continue | |
52 | urls.append(video_url) | |
53 | ext = mimetype2ext(video.get('mimetype')) or determine_ext(video_url) | |
54 | if ext == 'm3u8': | |
47f4203d | 55 | m3u8_fmts, m3u8_subs = self._extract_m3u8_formats_and_subtitles( |
4191779d | 56 | video_url, video_id, 'mp4', 'm3u8_native', |
47f4203d F |
57 | m3u8_id=format_id or 'hls', fatal=False) |
58 | formats.extend(m3u8_fmts) | |
59 | subtitles = self._merge_subtitles(subtitles, m3u8_subs) | |
4191779d RA |
60 | elif ext == 'mpd': |
61 | continue | |
62 | # formats.extend(self._extract_mpd_formats( | |
63 | # video_url, video_id, format_id or 'dash', fatal=False)) | |
64 | else: | |
65 | formats.append({ | |
66 | 'url': video_url, | |
67 | 'format_id': format_id, | |
68 | 'vcodec': video.get('videoencoding') or video.get('video_codec'), | |
69 | 'width': int_or_none(video.get('width')), | |
70 | 'height': int_or_none(video.get('height')), | |
71 | 'filesize': get_file_size(video.get('file_size') or video.get('fileSize')), | |
f377edec | 72 | 'tbr': int_or_none(video.get('bitrate'), 1000) or None, |
4191779d RA |
73 | 'ext': ext, |
74 | }) | |
d664de44 | 75 | |
4191779d RA |
76 | thumbnails = [] |
77 | for image in video_data.get('images', []): | |
78 | image_url = image.get('url') | |
79 | if not image_url: | |
80 | continue | |
81 | thumbnails.append({ | |
82 | 'url': 'http://www.nytimes.com/' + image_url, | |
f3c0c667 S |
83 | 'width': int_or_none(image.get('width')), |
84 | 'height': int_or_none(image.get('height')), | |
4191779d RA |
85 | }) |
86 | ||
87 | publication_date = video_data.get('publication_date') | |
88 | timestamp = parse_iso8601(publication_date[:-8]) if publication_date else None | |
d664de44 S |
89 | |
90 | return { | |
91 | 'id': video_id, | |
92 | 'title': title, | |
4191779d | 93 | 'description': video_data.get('summary'), |
d664de44 | 94 | 'timestamp': timestamp, |
4191779d RA |
95 | 'uploader': video_data.get('byline'), |
96 | 'duration': float_or_none(video_data.get('duration'), 1000), | |
d664de44 | 97 | 'formats': formats, |
47f4203d | 98 | 'subtitles': subtitles, |
d664de44 | 99 | 'thumbnails': thumbnails, |
5f6a1245 | 100 | } |
50aa43b3 YCH |
101 | |
102 | ||
103 | class NYTimesIE(NYTimesBaseIE): | |
104 | _VALID_URL = r'https?://(?:(?:www\.)?nytimes\.com/video/(?:[^/]+/)+?|graphics8\.nytimes\.com/bcvideo/\d+(?:\.\d+)?/iframe/embed\.html\?videoId=)(?P<id>\d+)' | |
bfd973ec | 105 | _EMBED_REGEX = [r'<iframe[^>]+src=(["\'])(?P<url>(?:https?:)?//graphics8\.nytimes\.com/bcvideo/[^/]+/iframe/embed\.html.+?)\1>'] |
50aa43b3 YCH |
106 | |
107 | _TESTS = [{ | |
108 | 'url': 'http://www.nytimes.com/video/opinion/100000002847155/verbatim-what-is-a-photocopier.html?playlistId=100000001150263', | |
4191779d | 109 | 'md5': 'd665342765db043f7e225cff19df0f2d', |
50aa43b3 YCH |
110 | 'info_dict': { |
111 | 'id': '100000002847155', | |
112 | 'ext': 'mov', | |
113 | 'title': 'Verbatim: What Is a Photocopier?', | |
114 | 'description': 'md5:93603dada88ddbda9395632fdc5da260', | |
115 | 'timestamp': 1398631707, | |
116 | 'upload_date': '20140427', | |
117 | 'uploader': 'Brett Weiner', | |
118 | 'duration': 419, | |
119 | } | |
120 | }, { | |
121 | 'url': 'http://www.nytimes.com/video/travel/100000003550828/36-hours-in-dubai.html', | |
122 | 'only_matching': True, | |
123 | }] | |
124 | ||
125 | def _real_extract(self, url): | |
126 | video_id = self._match_id(url) | |
127 | ||
128 | return self._extract_video_from_id(video_id) | |
129 | ||
130 | ||
131 | class NYTimesArticleIE(NYTimesBaseIE): | |
5332fd91 | 132 | _VALID_URL = r'https?://(?:www\.)?nytimes\.com/(.(?<!video))*?/(?:[^/]+/)*(?P<id>[^.]+)(?:\.html)?' |
df8418ff | 133 | _TESTS = [{ |
50aa43b3 YCH |
134 | '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', |
135 | 'md5': 'e2076d58b4da18e6a001d53fd56db3c9', | |
136 | 'info_dict': { | |
137 | 'id': '100000003628438', | |
138 | 'ext': 'mov', | |
139 | 'title': 'New Minimum Wage: $70,000 a Year', | |
140 | '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.', | |
141 | 'timestamp': 1429033037, | |
142 | 'upload_date': '20150414', | |
143 | 'uploader': 'Matthew Williams', | |
144 | } | |
74324a7a JH |
145 | }, { |
146 | 'url': 'http://www.nytimes.com/2016/10/14/podcasts/revelations-from-the-final-weeks.html', | |
147 | 'md5': 'e0d52040cafb07662acf3c9132db3575', | |
148 | 'info_dict': { | |
47da7823 S |
149 | 'id': '100000004709062', |
150 | 'title': 'The Run-Up: ‘He Was Like an Octopus’', | |
74324a7a | 151 | 'ext': 'mp3', |
47da7823 S |
152 | 'description': 'md5:fb5c6b93b12efc51649b4847fe066ee4', |
153 | 'series': 'The Run-Up', | |
154 | 'episode': '‘He Was Like an Octopus’', | |
155 | 'episode_number': 20, | |
156 | 'duration': 2130, | |
74324a7a JH |
157 | } |
158 | }, { | |
159 | 'url': 'http://www.nytimes.com/2016/10/16/books/review/inside-the-new-york-times-book-review-the-rise-of-hitler.html', | |
74324a7a | 160 | 'info_dict': { |
47da7823 S |
161 | 'id': '100000004709479', |
162 | 'title': 'The Rise of Hitler', | |
74324a7a | 163 | 'ext': 'mp3', |
47da7823 S |
164 | 'description': 'md5:bce877fd9e3444990cb141875fab0028', |
165 | 'creator': 'Pamela Paul', | |
166 | 'duration': 3475, | |
167 | }, | |
168 | 'params': { | |
169 | 'skip_download': True, | |
170 | }, | |
df8418ff YCH |
171 | }, { |
172 | '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', | |
173 | 'only_matching': True, | |
174 | }] | |
50aa43b3 | 175 | |
47da7823 S |
176 | def _extract_podcast_from_json(self, json, page_id, webpage): |
177 | podcast_audio = self._parse_json( | |
178 | json, page_id, transform_source=js_to_json) | |
179 | ||
180 | audio_data = podcast_audio['data'] | |
181 | track = audio_data['track'] | |
182 | ||
183 | episode_title = track['title'] | |
184 | video_url = track['source'] | |
185 | ||
186 | description = track.get('description') or self._html_search_meta( | |
187 | ['og:description', 'twitter:description'], webpage) | |
188 | ||
189 | podcast_title = audio_data.get('podcast', {}).get('title') | |
190 | title = ('%s: %s' % (podcast_title, episode_title) | |
191 | if podcast_title else episode_title) | |
192 | ||
193 | episode = audio_data.get('podcast', {}).get('episode') or '' | |
194 | episode_number = int_or_none(self._search_regex( | |
df4939b1 | 195 | r'[Ee]pisode\s+(\d+)', episode, 'episode number', default=None)) |
47da7823 S |
196 | |
197 | return { | |
198 | 'id': remove_start(podcast_audio.get('target'), 'FT') or page_id, | |
199 | 'url': video_url, | |
200 | 'title': title, | |
201 | 'description': description, | |
202 | 'creator': track.get('credit'), | |
203 | 'series': podcast_title, | |
204 | 'episode': episode_title, | |
205 | 'episode_number': episode_number, | |
206 | 'duration': int_or_none(track.get('duration')), | |
207 | } | |
208 | ||
50aa43b3 | 209 | def _real_extract(self, url): |
74324a7a | 210 | page_id = self._match_id(url) |
50aa43b3 | 211 | |
74324a7a | 212 | webpage = self._download_webpage(url, page_id) |
50aa43b3 | 213 | |
47da7823 S |
214 | video_id = self._search_regex( |
215 | r'data-videoid=["\'](\d+)', webpage, 'video id', | |
216 | default=None, fatal=False) | |
74324a7a JH |
217 | if video_id is not None: |
218 | return self._extract_video_from_id(video_id) | |
47da7823 S |
219 | |
220 | podcast_data = self._search_regex( | |
221 | (r'NYTD\.FlexTypes\.push\s*\(\s*({.+?})\s*\)\s*;\s*</script', | |
222 | r'NYTD\.FlexTypes\.push\s*\(\s*({.+})\s*\)\s*;'), | |
223 | webpage, 'podcast data') | |
224 | return self._extract_podcast_from_json(podcast_data, page_id, webpage) | |
70c5802b | 225 | |
226 | ||
227 | class NYTimesCookingIE(NYTimesBaseIE): | |
228 | _VALID_URL = r'https?://cooking\.nytimes\.com/(?:guid|recip)es/(?P<id>\d+)' | |
229 | _TESTS = [{ | |
230 | 'url': 'https://cooking.nytimes.com/recipes/1017817-cranberry-curd-tart', | |
231 | 'md5': 'dab81fa2eaeb3f9ed47498bdcfcdc1d3', | |
232 | 'info_dict': { | |
233 | 'id': '100000004756089', | |
234 | 'ext': 'mov', | |
235 | 'timestamp': 1479383008, | |
236 | 'uploader': 'By SHAW LASH, ADAM SAEWITZ and JAMES HERRON', | |
237 | 'title': 'Cranberry Tart', | |
238 | 'upload_date': '20161117', | |
239 | 'description': 'If you are a fan of lemon curd or the classic French tarte au citron, you will love this cranberry version.', | |
240 | }, | |
241 | }, { | |
242 | 'url': 'https://cooking.nytimes.com/guides/13-how-to-cook-a-turkey', | |
243 | 'md5': '4b2e8c70530a89b8d905a2b572316eb8', | |
244 | 'info_dict': { | |
245 | 'id': '100000003951728', | |
246 | 'ext': 'mov', | |
247 | 'timestamp': 1445509539, | |
248 | 'description': 'Turkey guide', | |
249 | 'upload_date': '20151022', | |
250 | 'title': 'Turkey', | |
251 | } | |
252 | }] | |
253 | ||
254 | def _real_extract(self, url): | |
255 | page_id = self._match_id(url) | |
256 | ||
257 | webpage = self._download_webpage(url, page_id) | |
258 | ||
259 | video_id = self._search_regex( | |
260 | r'data-video-id=["\'](\d+)', webpage, 'video id') | |
261 | ||
262 | return self._extract_video_from_id(video_id) |