]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/mlb.py
[extractors] Use new framework for existing embeds (#4307)
[yt-dlp.git] / yt_dlp / extractor / mlb.py
CommitLineData
421a4595 1import re
b1b01841 2
421a4595 3from .common import InfoExtractor
4from ..utils import (
5 determine_ext,
6 int_or_none,
7 parse_duration,
8 parse_iso8601,
9 try_get,
10)
b1b01841 11
421a4595 12
13class 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
80class MLBIE(MLBBaseIE):
d1feb308
S
81 _VALID_URL = r'''(?x)
82 https?://
421a4595 83 (?:[\da-z_-]+\.)*mlb\.com/
d1feb308
S
84 (?:
85 (?:
421a4595 86 (?:[^/]+/)*video/[^/]+/c-|
d1feb308
S
87 (?:
88 shared/video/embed/(?:embed|m-internal-embed)\.html|
3800b908 89 (?:[^/]+/)+(?:play|index)\.jsp|
d1feb308
S
90 )\?.*?\bcontent_id=
91 )
acca2ac7 92 (?P<id>\d+)
d1feb308
S
93 )
94 '''
bfd973ec 95 _EMBED_REGEX = [
96 r'<iframe[^>]+?src=(["\'])(?P<url>https?://m(?:lb)?\.mlb\.com/shared/video/embed/embed\.html\?.+?)\1',
97 r'data-video-link=["\'](?P<url>http://m\.mlb\.com/video/[^"\']+)',
98 ]
7bb49d10 99 _TESTS = [
07cc63f3 100 {
acca2ac7
RA
101 'url': 'https://www.mlb.com/mariners/video/ackleys-spectacular-catch/c-34698933',
102 'md5': '632358dacfceec06bad823b83d21df2d',
07cc63f3
CC
103 'info_dict': {
104 'id': '34698933',
105 'ext': 'mp4',
106 'title': "Ackley's spectacular catch",
107 'description': 'md5:7f5a981eb4f3cbc8daf2aeffa2215bf0',
108 'duration': 66,
acca2ac7
RA
109 'timestamp': 1405995000,
110 'upload_date': '20140722',
ec85ded8 111 'thumbnail': r're:^https?://.*\.jpg$',
07cc63f3
CC
112 },
113 },
7bb49d10 114 {
acca2ac7
RA
115 'url': 'https://www.mlb.com/video/stanton-prepares-for-derby/c-34496663',
116 'md5': 'bf2619bf9cacc0a564fc35e6aeb9219f',
7bb49d10
S
117 'info_dict': {
118 'id': '34496663',
119 'ext': 'mp4',
120 'title': 'Stanton prepares for Derby',
121 'description': 'md5:d00ce1e5fd9c9069e9c13ab4faedfa57',
122 'duration': 46,
acca2ac7 123 'timestamp': 1405120200,
7bb49d10 124 'upload_date': '20140711',
ec85ded8 125 'thumbnail': r're:^https?://.*\.jpg$',
7bb49d10 126 },
b1b01841 127 },
7bb49d10 128 {
acca2ac7
RA
129 'url': 'https://www.mlb.com/video/cespedes-repeats-as-derby-champ/c-34578115',
130 'md5': '99bb9176531adc600b90880fb8be9328',
7bb49d10
S
131 'info_dict': {
132 'id': '34578115',
133 'ext': 'mp4',
134 'title': 'Cespedes repeats as Derby champ',
135 'description': 'md5:08df253ce265d4cf6fb09f581fafad07',
136 'duration': 488,
acca2ac7 137 'timestamp': 1405414336,
7bb49d10 138 'upload_date': '20140715',
ec85ded8 139 'thumbnail': r're:^https?://.*\.jpg$',
7bb49d10
S
140 },
141 },
142 {
acca2ac7
RA
143 'url': 'https://www.mlb.com/video/bautista-on-home-run-derby/c-34577915',
144 'md5': 'da8b57a12b060e7663ee1eebd6f330ec',
7bb49d10
S
145 'info_dict': {
146 'id': '34577915',
147 'ext': 'mp4',
148 'title': 'Bautista on Home Run Derby',
149 'description': 'md5:b80b34031143d0986dddc64a8839f0fb',
150 'duration': 52,
acca2ac7 151 'timestamp': 1405405122,
7bb49d10 152 'upload_date': '20140715',
ec85ded8 153 'thumbnail': r're:^https?://.*\.jpg$',
7bb49d10
S
154 },
155 },
11a6793f
MC
156 {
157 'url': 'https://www.mlb.com/video/hargrove-homers-off-caldwell/c-1352023483?tid=67793694',
158 'only_matching': True,
159 },
1a94ff68
S
160 {
161 'url': 'http://m.mlb.com/shared/video/embed/embed.html?content_id=35692085&topic_id=6479266&width=400&height=224&property=mlb',
162 'only_matching': True,
163 },
b2a68d14
S
164 {
165 'url': 'http://mlb.mlb.com/shared/video/embed/embed.html?content_id=36599553',
166 'only_matching': True,
167 },
168 {
169 'url': 'http://mlb.mlb.com/es/video/play.jsp?content_id=36599553',
170 'only_matching': True,
171 },
9f790b99 172 {
acca2ac7 173 'url': 'https://www.mlb.com/cardinals/video/piscottys-great-sliding-catch/c-51175783',
9f790b99 174 'only_matching': True,
3e7202c1
YCH
175 },
176 {
177 # From http://m.mlb.com/news/article/118550098/blue-jays-kevin-pillar-goes-spidey-up-the-wall-to-rob-tim-beckham-of-a-homer
178 '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',
179 'only_matching': True,
3800b908 180 },
7bb49d10 181 ]
421a4595 182 _TIMESTAMP_KEY = 'date'
183
184 @staticmethod
185 def _get_feed(video):
186 return video
187
188 @staticmethod
189 def _extract_mlb_subtitles(feed, language):
190 subtitles = {}
191 for keyword in (feed.get('keywordsAll') or []):
192 keyword_type = keyword.get('type')
193 if keyword_type and keyword_type.startswith('closed_captions_location_'):
194 cc_location = keyword.get('value')
195 if cc_location:
196 subtitles.setdefault(language, []).append({
197 'url': cc_location,
198 })
199 return subtitles
200
201 def _download_video_data(self, display_id):
202 return self._download_json(
203 'http://content.mlb.com/mlb/item/id/v1/%s/details/web-v1.json' % display_id,
204 display_id)
205
206
207class MLBVideoIE(MLBBaseIE):
208 _VALID_URL = r'https?://(?:www\.)?mlb\.com/(?:[^/]+/)*video/(?P<id>[^/?&#]+)'
209 _TEST = {
210 'url': 'https://www.mlb.com/mariners/video/ackley-s-spectacular-catch-c34698933',
211 'md5': '632358dacfceec06bad823b83d21df2d',
212 'info_dict': {
213 'id': 'c04a8863-f569-42e6-9f87-992393657614',
214 'ext': 'mp4',
215 'title': "Ackley's spectacular catch",
216 'description': 'md5:7f5a981eb4f3cbc8daf2aeffa2215bf0',
217 'duration': 66,
218 'timestamp': 1405995000,
219 'upload_date': '20140722',
220 'thumbnail': r're:^https?://.+',
221 },
222 }
223 _TIMESTAMP_KEY = 'timestamp'
224
225 @classmethod
226 def suitable(cls, url):
227 return False if MLBIE.suitable(url) else super(MLBVideoIE, cls).suitable(url)
228
229 @staticmethod
230 def _get_feed(video):
231 return video['feeds'][0]
232
233 @staticmethod
234 def _extract_mlb_subtitles(feed, language):
235 subtitles = {}
236 for cc_location in (feed.get('closedCaptions') or []):
237 subtitles.setdefault(language, []).append({
238 'url': cc_location,
239 })
240
241 def _download_video_data(self, display_id):
242 # https://www.mlb.com/data-service/en/videos/[SLUG]
243 return self._download_json(
244 'https://fastball-gateway.mlb.com/graphql',
245 display_id, query={
246 'query': '''{
247 mediaPlayback(ids: "%s") {
248 description
249 feeds(types: CMS) {
250 closedCaptions
251 duration
252 image {
253 cuts {
254 width
255 height
256 src
257 }
258 }
259 playbacks {
260 name
261 url
262 }
263 }
264 id
265 timestamp
266 title
267 }
268}''' % display_id,
269 })['data']['mediaPlayback'][0]