]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/mlb.py
[extractor] Support multiple archive ids for one video (#4307)
[yt-dlp.git] / yt_dlp / extractor / mlb.py
1 import re
2
3 from .common import InfoExtractor
4 from ..utils import (
5 determine_ext,
6 int_or_none,
7 parse_duration,
8 parse_iso8601,
9 try_get,
10 )
11
12
13 class MLBBaseIE(InfoExtractor):
14 def _real_extract(self, url):
15 display_id = self._match_id(url)
16 video = self._download_video_data(display_id)
17 video_id = video['id']
18 title = video['title']
19 feed = self._get_feed(video)
20
21 formats = []
22 for playback in (feed.get('playbacks') or []):
23 playback_url = playback.get('url')
24 if not playback_url:
25 continue
26 name = playback.get('name')
27 ext = determine_ext(playback_url)
28 if ext == 'm3u8':
29 formats.extend(self._extract_m3u8_formats(
30 playback_url, video_id, 'mp4',
31 'm3u8_native', m3u8_id=name, fatal=False))
32 else:
33 f = {
34 'format_id': name,
35 'url': playback_url,
36 }
37 mobj = re.search(r'_(\d+)K_(\d+)X(\d+)', name)
38 if mobj:
39 f.update({
40 'height': int(mobj.group(3)),
41 'tbr': int(mobj.group(1)),
42 'width': int(mobj.group(2)),
43 })
44 mobj = re.search(r'_(\d+)x(\d+)_(\d+)_(\d+)K\.mp4', playback_url)
45 if mobj:
46 f.update({
47 'fps': int(mobj.group(3)),
48 'height': int(mobj.group(2)),
49 'tbr': int(mobj.group(4)),
50 'width': int(mobj.group(1)),
51 })
52 formats.append(f)
53 self._sort_formats(formats)
54
55 thumbnails = []
56 for cut in (try_get(feed, lambda x: x['image']['cuts'], list) or []):
57 src = cut.get('src')
58 if not src:
59 continue
60 thumbnails.append({
61 'height': int_or_none(cut.get('height')),
62 'url': src,
63 'width': int_or_none(cut.get('width')),
64 })
65
66 language = (video.get('language') or 'EN').lower()
67
68 return {
69 'id': video_id,
70 'title': title,
71 'formats': formats,
72 'description': video.get('description'),
73 'duration': parse_duration(feed.get('duration')),
74 'thumbnails': thumbnails,
75 'timestamp': parse_iso8601(video.get(self._TIMESTAMP_KEY)),
76 'subtitles': self._extract_mlb_subtitles(feed, language),
77 }
78
79
80 class MLBIE(MLBBaseIE):
81 _VALID_URL = r'''(?x)
82 https?://
83 (?:[\da-z_-]+\.)*mlb\.com/
84 (?:
85 (?:
86 (?:[^/]+/)*video/[^/]+/c-|
87 (?:
88 shared/video/embed/(?:embed|m-internal-embed)\.html|
89 (?:[^/]+/)+(?:play|index)\.jsp|
90 )\?.*?\bcontent_id=
91 )
92 (?P<id>\d+)
93 )
94 '''
95 _TESTS = [
96 {
97 'url': 'https://www.mlb.com/mariners/video/ackleys-spectacular-catch/c-34698933',
98 'md5': '632358dacfceec06bad823b83d21df2d',
99 'info_dict': {
100 'id': '34698933',
101 'ext': 'mp4',
102 'title': "Ackley's spectacular catch",
103 'description': 'md5:7f5a981eb4f3cbc8daf2aeffa2215bf0',
104 'duration': 66,
105 'timestamp': 1405995000,
106 'upload_date': '20140722',
107 'thumbnail': r're:^https?://.*\.jpg$',
108 },
109 },
110 {
111 'url': 'https://www.mlb.com/video/stanton-prepares-for-derby/c-34496663',
112 'md5': 'bf2619bf9cacc0a564fc35e6aeb9219f',
113 'info_dict': {
114 'id': '34496663',
115 'ext': 'mp4',
116 'title': 'Stanton prepares for Derby',
117 'description': 'md5:d00ce1e5fd9c9069e9c13ab4faedfa57',
118 'duration': 46,
119 'timestamp': 1405120200,
120 'upload_date': '20140711',
121 'thumbnail': r're:^https?://.*\.jpg$',
122 },
123 },
124 {
125 'url': 'https://www.mlb.com/video/cespedes-repeats-as-derby-champ/c-34578115',
126 'md5': '99bb9176531adc600b90880fb8be9328',
127 'info_dict': {
128 'id': '34578115',
129 'ext': 'mp4',
130 'title': 'Cespedes repeats as Derby champ',
131 'description': 'md5:08df253ce265d4cf6fb09f581fafad07',
132 'duration': 488,
133 'timestamp': 1405414336,
134 'upload_date': '20140715',
135 'thumbnail': r're:^https?://.*\.jpg$',
136 },
137 },
138 {
139 'url': 'https://www.mlb.com/video/bautista-on-home-run-derby/c-34577915',
140 'md5': 'da8b57a12b060e7663ee1eebd6f330ec',
141 'info_dict': {
142 'id': '34577915',
143 'ext': 'mp4',
144 'title': 'Bautista on Home Run Derby',
145 'description': 'md5:b80b34031143d0986dddc64a8839f0fb',
146 'duration': 52,
147 'timestamp': 1405405122,
148 'upload_date': '20140715',
149 'thumbnail': r're:^https?://.*\.jpg$',
150 },
151 },
152 {
153 'url': 'https://www.mlb.com/video/hargrove-homers-off-caldwell/c-1352023483?tid=67793694',
154 'only_matching': True,
155 },
156 {
157 'url': 'http://m.mlb.com/shared/video/embed/embed.html?content_id=35692085&topic_id=6479266&width=400&height=224&property=mlb',
158 'only_matching': True,
159 },
160 {
161 'url': 'http://mlb.mlb.com/shared/video/embed/embed.html?content_id=36599553',
162 'only_matching': True,
163 },
164 {
165 'url': 'http://mlb.mlb.com/es/video/play.jsp?content_id=36599553',
166 'only_matching': True,
167 },
168 {
169 'url': 'https://www.mlb.com/cardinals/video/piscottys-great-sliding-catch/c-51175783',
170 'only_matching': True,
171 },
172 {
173 # From http://m.mlb.com/news/article/118550098/blue-jays-kevin-pillar-goes-spidey-up-the-wall-to-rob-tim-beckham-of-a-homer
174 'url': 'http://mlb.mlb.com/shared/video/embed/m-internal-embed.html?content_id=75609783&property=mlb&autoplay=true&hashmode=false&siteSection=mlb/multimedia/article_118550098/article_embed&club=mlb',
175 'only_matching': True,
176 },
177 ]
178 _TIMESTAMP_KEY = 'date'
179
180 @staticmethod
181 def _get_feed(video):
182 return video
183
184 @staticmethod
185 def _extract_mlb_subtitles(feed, language):
186 subtitles = {}
187 for keyword in (feed.get('keywordsAll') or []):
188 keyword_type = keyword.get('type')
189 if keyword_type and keyword_type.startswith('closed_captions_location_'):
190 cc_location = keyword.get('value')
191 if cc_location:
192 subtitles.setdefault(language, []).append({
193 'url': cc_location,
194 })
195 return subtitles
196
197 def _download_video_data(self, display_id):
198 return self._download_json(
199 'http://content.mlb.com/mlb/item/id/v1/%s/details/web-v1.json' % display_id,
200 display_id)
201
202
203 class MLBVideoIE(MLBBaseIE):
204 _VALID_URL = r'https?://(?:www\.)?mlb\.com/(?:[^/]+/)*video/(?P<id>[^/?&#]+)'
205 _TEST = {
206 'url': 'https://www.mlb.com/mariners/video/ackley-s-spectacular-catch-c34698933',
207 'md5': '632358dacfceec06bad823b83d21df2d',
208 'info_dict': {
209 'id': 'c04a8863-f569-42e6-9f87-992393657614',
210 'ext': 'mp4',
211 'title': "Ackley's spectacular catch",
212 'description': 'md5:7f5a981eb4f3cbc8daf2aeffa2215bf0',
213 'duration': 66,
214 'timestamp': 1405995000,
215 'upload_date': '20140722',
216 'thumbnail': r're:^https?://.+',
217 },
218 }
219 _TIMESTAMP_KEY = 'timestamp'
220
221 @classmethod
222 def suitable(cls, url):
223 return False if MLBIE.suitable(url) else super(MLBVideoIE, cls).suitable(url)
224
225 @staticmethod
226 def _get_feed(video):
227 return video['feeds'][0]
228
229 @staticmethod
230 def _extract_mlb_subtitles(feed, language):
231 subtitles = {}
232 for cc_location in (feed.get('closedCaptions') or []):
233 subtitles.setdefault(language, []).append({
234 'url': cc_location,
235 })
236
237 def _download_video_data(self, display_id):
238 # https://www.mlb.com/data-service/en/videos/[SLUG]
239 return self._download_json(
240 'https://fastball-gateway.mlb.com/graphql',
241 display_id, query={
242 'query': '''{
243 mediaPlayback(ids: "%s") {
244 description
245 feeds(types: CMS) {
246 closedCaptions
247 duration
248 image {
249 cuts {
250 width
251 height
252 src
253 }
254 }
255 playbacks {
256 name
257 url
258 }
259 }
260 id
261 timestamp
262 title
263 }
264 }''' % display_id,
265 })['data']['mediaPlayback'][0]