diff --git a/yt_dlp/extractor/_extractors.py b/yt_dlp/extractor/_extractors.py
index 04318a716..36335286c 100644
--- a/yt_dlp/extractor/_extractors.py
+++ b/yt_dlp/extractor/_extractors.py
@@ -1352,6 +1352,7 @@
NYTimesIE,
NYTimesArticleIE,
NYTimesCookingIE,
+ NYTimesCookingRecipeIE,
)
from .nuvid import NuvidIE
from .nzherald import NZHeraldIE
diff --git a/yt_dlp/extractor/nytimes.py b/yt_dlp/extractor/nytimes.py
index 2e21edbb4..354eb02c3 100644
--- a/yt_dlp/extractor/nytimes.py
+++ b/yt_dlp/extractor/nytimes.py
@@ -1,50 +1,92 @@
-import hmac
-import hashlib
-import base64
+import json
+import uuid
from .common import InfoExtractor
from ..utils import (
+ ExtractorError,
+ clean_html,
determine_ext,
+ extract_attributes,
float_or_none,
+ get_elements_html_by_class,
int_or_none,
- js_to_json,
+ merge_dicts,
mimetype2ext,
parse_iso8601,
+ remove_end,
remove_start,
+ str_or_none,
+ traverse_obj,
+ url_or_none,
)
class NYTimesBaseIE(InfoExtractor):
- _SECRET = b'pX(2MbU2);4N{7J8)>YwKRJ+/pQ3JkiU2Q^V>mFYv6g6gYvt6v'
+ _DNS_NAMESPACE = uuid.UUID('36dd619a-56dc-595b-9e09-37f4152c7b5d')
+ _TOKEN = 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAuNIzKBOFB77aT/jN/FQ+/QVKWq5V1ka1AYmCR9hstz1pGNPH5ajOU9gAqta0T89iPnhjwla+3oec/Z3kGjxbpv6miQXufHFq3u2RC6HyU458cLat5kVPSOQCe3VVB5NRpOlRuwKHqn0txfxnwSSj8mqzstR997d3gKB//RO9zE16y3PoWlDQXkASngNJEWvL19iob/xwAkfEWCjyRILWFY0JYX3AvLMSbq7wsqOCE5srJpo7rRU32zsByhsp1D5W9OYqqwDmflsgCEQy2vqTsJjrJohuNg+urMXNNZ7Y3naMoqttsGDrWVxtPBafKMI8pM2ReNZBbGQsQXRzQNo7+QIDAQAB'
+ _GRAPHQL_API = 'https://samizdat-graphql.nytimes.com/graphql/v2'
+ _GRAPHQL_QUERY = '''query VideoQuery($id: String!) {
+ video(id: $id) {
+ ... on Video {
+ bylines {
+ renderedRepresentation
+ }
+ duration
+ promotionalHeadline
+ promotionalMedia {
+ ... on Image {
+ crops {
+ name
+ renditions {
+ name
+ width
+ height
+ url
+ }
+ }
+ }
+ }
+ renditions {
+ type
+ width
+ height
+ url
+ bitrate
+ }
+ summary
+ }
+ }
+}'''
- def _extract_video_from_id(self, video_id):
- # Authorization generation algorithm is reverse engineered from `signer` in
- # http://graphics8.nytimes.com/video/vhs/vhs-2.x.min.js
- path = '/svc/video/api/v3/video/' + video_id
- hm = hmac.new(self._SECRET, (path + ':vhs').encode(), hashlib.sha512).hexdigest()
- video_data = self._download_json('http://www.nytimes.com' + path, video_id, 'Downloading video JSON', headers={
- 'Authorization': 'NYTV ' + base64.b64encode(hm.encode()).decode(),
- 'X-NYTV': 'vhs',
- }, fatal=False)
- if not video_data:
- video_data = self._download_json(
- 'http://www.nytimes.com/svc/video/api/v2/video/' + video_id,
- video_id, 'Downloading video JSON')
+ def _call_api(self, media_id):
+ # reference: `id-to-uri.js`
+ video_uuid = uuid.uuid5(self._DNS_NAMESPACE, 'video')
+ media_uuid = uuid.uuid5(video_uuid, media_id)
- title = video_data['headline']
+ return traverse_obj(self._download_json(
+ self._GRAPHQL_API, media_id, 'Downloading JSON from GraphQL API', data=json.dumps({
+ 'query': self._GRAPHQL_QUERY,
+ 'variables': {'id': f'nyt://video/{media_uuid}'},
+ }, separators=(',', ':')).encode(), headers={
+ 'Content-Type': 'application/json',
+ 'Nyt-App-Type': 'vhs',
+ 'Nyt-App-Version': 'v3.52.21',
+ 'Nyt-Token': self._TOKEN,
+ 'Origin': 'https://nytimes.com',
+ }, fatal=False), ('data', 'video', {dict})) or {}
- def get_file_size(file_size):
- if isinstance(file_size, int):
- return file_size
- elif isinstance(file_size, dict):
- return int(file_size.get('value', 0))
- else:
- return None
+ def _extract_thumbnails(self, thumbs):
+ return traverse_obj(thumbs, (lambda _, v: url_or_none(v['url']), {
+ 'url': 'url',
+ 'width': ('width', {int_or_none}),
+ 'height': ('height', {int_or_none}),
+ }), default=None)
+ def _extract_formats_and_subtitles(self, video_id, content_media_json):
urls = []
formats = []
subtitles = {}
- for video in video_data.get('renditions', []):
+ for video in traverse_obj(content_media_json, ('renditions', ..., {dict})):
video_url = video.get('url')
format_id = video.get('type')
if not video_url or format_id == 'thumbs' or video_url in urls:
@@ -56,11 +98,9 @@ def get_file_size(file_size):
video_url, video_id, 'mp4', 'm3u8_native',
m3u8_id=format_id or 'hls', fatal=False)
formats.extend(m3u8_fmts)
- subtitles = self._merge_subtitles(subtitles, m3u8_subs)
+ self._merge_subtitles(m3u8_subs, target=subtitles)
elif ext == 'mpd':
- continue
- # formats.extend(self._extract_mpd_formats(
- # video_url, video_id, format_id or 'dash', fatal=False))
+ continue # all mpd urls give 404 errors
else:
formats.append({
'url': video_url,
@@ -68,55 +108,49 @@ def get_file_size(file_size):
'vcodec': video.get('videoencoding') or video.get('video_codec'),
'width': int_or_none(video.get('width')),
'height': int_or_none(video.get('height')),
- 'filesize': get_file_size(video.get('file_size') or video.get('fileSize')),
+ 'filesize': traverse_obj(video, (
+ ('file_size', 'fileSize'), (None, ('value')), {int_or_none}), get_all=False),
'tbr': int_or_none(video.get('bitrate'), 1000) or None,
'ext': ext,
})
- thumbnails = []
- for image in video_data.get('images', []):
- image_url = image.get('url')
- if not image_url:
- continue
- thumbnails.append({
- 'url': 'http://www.nytimes.com/' + image_url,
- 'width': int_or_none(image.get('width')),
- 'height': int_or_none(image.get('height')),
- })
+ return formats, subtitles
- publication_date = video_data.get('publication_date')
- timestamp = parse_iso8601(publication_date[:-8]) if publication_date else None
+ def _extract_video(self, media_id):
+ data = self._call_api(media_id)
+ formats, subtitles = self._extract_formats_and_subtitles(media_id, data)
return {
- 'id': video_id,
- 'title': title,
- 'description': video_data.get('summary'),
- 'timestamp': timestamp,
- 'uploader': video_data.get('byline'),
- 'duration': float_or_none(video_data.get('duration'), 1000),
+ 'id': media_id,
+ 'title': data.get('promotionalHeadline'),
+ 'description': data.get('summary'),
+ 'duration': float_or_none(data.get('duration'), scale=1000),
+ 'creator': ', '.join(traverse_obj(data, ( # TODO: change to 'creators'
+ 'bylines', ..., 'renderedRepresentation', {lambda x: remove_start(x, 'By ')}))),
'formats': formats,
'subtitles': subtitles,
- 'thumbnails': thumbnails,
+ 'thumbnails': self._extract_thumbnails(
+ traverse_obj(data, ('promotionalMedia', 'crops', ..., 'renditions', ...))),
}
class NYTimesIE(NYTimesBaseIE):
_VALID_URL = r'https?://(?:(?:www\.)?nytimes\.com/video/(?:[^/]+/)+?|graphics8\.nytimes\.com/bcvideo/\d+(?:\.\d+)?/iframe/embed\.html\?videoId=)(?P