5 from .common
import InfoExtractor
17 class MLBBaseIE(InfoExtractor
):
18 def _real_extract(self
, url
):
19 display_id
= self
._match
_id
(url
)
20 video
= self
._download
_video
_data
(display_id
)
21 video_id
= video
['id']
22 title
= video
['title']
23 feed
= self
._get
_feed
(video
)
26 for playback
in (feed
.get('playbacks') or []):
27 playback_url
= playback
.get('url')
30 name
= playback
.get('name')
31 ext
= determine_ext(playback_url
)
33 formats
.extend(self
._extract
_m
3u8_formats
(
34 playback_url
, video_id
, 'mp4',
35 'm3u8_native', m3u8_id
=name
, fatal
=False))
41 mobj
= re
.search(r
'_(\d+)K_(\d+)X(\d+)', name
)
44 'height': int(mobj
.group(3)),
45 'tbr': int(mobj
.group(1)),
46 'width': int(mobj
.group(2)),
48 mobj
= re
.search(r
'_(\d+)x(\d+)_(\d+)_(\d+)K\.mp4', playback_url
)
51 'fps': int(mobj
.group(3)),
52 'height': int(mobj
.group(2)),
53 'tbr': int(mobj
.group(4)),
54 'width': int(mobj
.group(1)),
59 for cut
in (try_get(feed
, lambda x
: x
['image']['cuts'], list) or []):
64 'height': int_or_none(cut
.get('height')),
66 'width': int_or_none(cut
.get('width')),
69 language
= (video
.get('language') or 'EN').lower()
75 'description': video
.get('description'),
76 'duration': parse_duration(feed
.get('duration')),
77 'thumbnails': thumbnails
,
78 'timestamp': parse_iso8601(video
.get(self
._TIMESTAMP
_KEY
)),
79 'subtitles': self
._extract
_mlb
_subtitles
(feed
, language
),
83 class MLBIE(MLBBaseIE
):
86 (?:[\da-z_-]+\.)*mlb\.com/
89 (?:[^/]+/)*video/[^/]+/c-|
91 shared/video/embed/(?:embed|m-internal-embed)\.html|
92 (?:[^/]+/)+(?:play|index)\.jsp|
99 r
'<iframe[^>]+?src=(["\'])(?P
<url
>https?
://m(?
:lb
)?\
.mlb\
.com
/shared
/video
/embed
/embed\
.html
\?.+?
)\
1',
100 r'data
-video
-link
=["\'](?P<url>http://m\.mlb\.com/video/[^"\']+)',
104 'url
': 'https
://www
.mlb
.com
/mariners
/video
/ackleys
-spectacular
-catch
/c
-34698933',
105 'md5
': '632358dacfceec06bad823b83d21df2d
',
109 'title
': "Ackley's spectacular catch
",
110 'description': 'md5:7f5a981eb4f3cbc8daf2aeffa2215bf0',
112 'timestamp': 1405995000,
113 'upload_date': '20140722',
114 'thumbnail': r're:^https?://.*\.jpg$',
118 'url': 'https://www.mlb.com/video/stanton-prepares-for-derby/c-34496663',
119 'md5': 'bf2619bf9cacc0a564fc35e6aeb9219f',
123 'title': 'Stanton prepares for Derby',
124 'description': 'md5:d00ce1e5fd9c9069e9c13ab4faedfa57',
126 'timestamp': 1405120200,
127 'upload_date': '20140711',
128 'thumbnail': r're:^https?://.*\.jpg$',
132 'url': 'https://www.mlb.com/video/cespedes-repeats-as-derby-champ/c-34578115',
133 'md5': '99bb9176531adc600b90880fb8be9328',
137 'title': 'Cespedes repeats as Derby champ',
138 'description': 'md5:08df253ce265d4cf6fb09f581fafad07',
140 'timestamp': 1405414336,
141 'upload_date': '20140715',
142 'thumbnail': r're:^https?://.*\.jpg$',
146 'url': 'https://www.mlb.com/video/bautista-on-home-run-derby/c-34577915',
147 'md5': 'da8b57a12b060e7663ee1eebd6f330ec',
151 'title': 'Bautista on Home Run Derby',
152 'description': 'md5:b80b34031143d0986dddc64a8839f0fb',
154 'timestamp': 1405405122,
155 'upload_date': '20140715',
156 'thumbnail': r're:^https?://.*\.jpg$',
160 'url': 'https://www.mlb.com/video/hargrove-homers-off-caldwell/c-1352023483?tid=67793694',
161 'only_matching': True,
164 'url': 'http://m.mlb.com/shared/video/embed/embed.html?content_id=35692085&topic_id=6479266&width=400&height=224&property=mlb',
165 'only_matching': True,
168 'url': 'http://mlb.mlb.com/shared/video/embed/embed.html?content_id=36599553',
169 'only_matching': True,
172 'url': 'http://mlb.mlb.com/es/video/play.jsp?content_id=36599553',
173 'only_matching': True,
176 'url': 'https://www.mlb.com/cardinals/video/piscottys-great-sliding-catch/c-51175783',
177 'only_matching': True,
180 # From http://m.mlb.com/news/article/118550098/blue-jays-kevin-pillar-goes-spidey-up-the-wall-to-rob-tim-beckham-of-a-homer
181 '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',
182 'only_matching': True,
185 _TIMESTAMP_KEY = 'date'
188 def _get_feed(video):
192 def _extract_mlb_subtitles(feed, language):
194 for keyword in (feed.get('keywordsAll') or []):
195 keyword_type = keyword.get('type')
196 if keyword_type and keyword_type.startswith('closed_captions_location_'):
197 cc_location = keyword.get('value')
199 subtitles.setdefault(language, []).append({
204 def _download_video_data(self, display_id):
205 return self._download_json(
206 'http://content.mlb.com/mlb/item/id/v1/%s/details/web-v1.json' % display_id,
210 class MLBVideoIE(MLBBaseIE):
211 _VALID_URL = r'https?://(?:www\.)?mlb\.com/(?:[^/]+/)*video/(?P<id>[^/?&#]+)'
213 'url': 'https://www.mlb.com/mariners/video/ackley-s-spectacular-catch-c34698933',
214 'md5': '632358dacfceec06bad823b83d21df2d',
216 'id': 'c04a8863-f569-42e6-9f87-992393657614',
218 'title': "Ackley
's spectacular catch",
219 'description
': 'md5
:7f5a981eb4f3cbc8daf2aeffa2215bf0
',
221 'timestamp
': 1405995000,
222 'upload_date
': '20140722',
223 'thumbnail
': r're
:^https?
://.+',
226 _TIMESTAMP_KEY = 'timestamp
'
229 def suitable(cls, url):
230 return False if MLBIE.suitable(url) else super(MLBVideoIE, cls).suitable(url)
233 def _get_feed(video):
234 return video['feeds
'][0]
237 def _extract_mlb_subtitles(feed, language):
239 for cc_location in (feed.get('closedCaptions
') or []):
240 subtitles.setdefault(language, []).append({
244 def _download_video_data(self, display_id):
245 # https://www.mlb.com/data-service/en/videos/[SLUG]
246 return self._download_json(
247 'https
://fastball
-gateway
.mlb
.com
/graphql
',
250 mediaPlayback(ids: "%s") {
272 })['data
']['mediaPlayback
'][0]
275 class MLBTVIE(InfoExtractor):
276 _VALID_URL = r'https?
://(?
:www\
.)?mlb\
.com
/tv
/g(?P
<id>\d{6}
)'
277 _NETRC_MACHINE = 'mlb
'
280 'url
': 'https
://www
.mlb
.com
/tv
/g661581
/vee2eff5f
-a7df
-4c20
-bdb4
-7b926fa12638
',
284 'title
': '2022-07-02 - St
. Louis Cardinals
@ Philadelphia Phillies
',
287 'skip_download
': True,
292 def _real_initialize(self):
293 if not self._access_token:
294 self.raise_login_required(
295 'All videos are only available to registered users
', method='password
')
297 def _perform_login(self, username, password):
298 data = f'grant_type
=password
&username
={urllib
.parse
.quote(username
)}&password
={urllib
.parse
.quote(password
)}&scope
=openid offline_access
&client_id
=0oa3e1nutA
1HLzAKG
356'
299 access_token = self._download_json(
300 'https
://ids
.mlb
.com
/oauth2
/aus1m088yK07noBfh356
/v1
/token
', None,
302 'User
-Agent
': 'okhttp
/3.12.1',
303 'Content
-Type
': 'application
/x
-www
-form
-urlencoded
'
304 }, data=data.encode())['access_token
']
306 entitlement = self._download_webpage(
307 f'https
://media
-entitlement
.mlb
.com
/api
/v3
/jwt?os
=Android
&appname
=AtBat
&did
={str(uuid
.uuid4())}', None,
309 'User
-Agent
': 'okhttp
/3.12.1',
310 'Authorization
': f'Bearer {access_token}
'
313 data = f'grant_type
=urn
:ietf
:params
:oauth
:grant
-type:token
-exchange
&subject_token
={entitlement}
&subject_token_type
=urn
:ietf
:params
:oauth
:token
-type:jwt
&platform
=android
-tv
'
314 self._access_token = self._download_json(
315 'https
://us
.edge
.bamgrid
.com
/token
', None,
317 'Accept
': 'application
/json
',
318 'Authorization
': 'Bearer bWxidHYmYW5kcm9pZCYxLjAuMA
.6LZMbH
2r
--rbXcgEabaDdIslpo4RyZrlVfWZhsAgXIk
',
319 'Content
-Type
': 'application
/x
-www
-form
-urlencoded
'
320 }, data=data.encode())['access_token
']
322 def _real_extract(self, url):
323 video_id = self._match_id(url)
324 airings = self._download_json(
325 f'https
://search
-api
-mlbtv
.mlb
.com
/svc
/search
/v2
/graphql
/persisted
/query
/core
/Airings?variables
=%7B
%22partnerProgramIds
%22%3A
%5B
%22{video_id}
%22%5D
%2C
%22applyEsniMediaRightsLabels
%22%3Atrue
%7D
',
326 video_id)['data
']['Airings
']
328 formats, subtitles = [], {}
329 for airing in airings:
330 m3u8_url = self._download_json(
331 airing['playbackUrls
'][0]['href
'].format(scenario='browser~csai
'), video_id,
333 'Authorization
': self._access_token,
334 'Accept
': 'application
/vnd
.media
-service
+json
; version
=2'
335 })['stream
']['complete
']
336 f, s = self._extract_m3u8_formats_and_subtitles(
337 m3u8_url, video_id, 'mp4
', m3u8_id=join_nonempty(airing.get('feedType
'), airing.get('feedLanguage
')))
339 self._merge_subtitles(s, target=subtitles)
343 'title
': traverse_obj(airings, (..., 'titles
', 0, 'episodeName
'), get_all=False),
344 'is_live
': traverse_obj(airings, (..., 'mediaConfig
', 'productType
'), get_all=False) == 'LIVE
',
346 'subtitles
': subtitles,
347 'http_headers
': {'Authorization
': f'Bearer
{self
._access
_token
}'},
351 class MLBArticleIE(InfoExtractor):
352 _VALID_URL = r'https?
://www\
.mlb\
.com
/news
/(?P
<id>[\w
-]+)'
354 'url
': 'https
://www
.mlb
.com
/news
/manny
-machado
-robs
-guillermo
-heredia
-reacts
',
356 'id': '36db7394
-343c
-4ea3
-b8ca
-ead2e61bca9a
',
357 'title
': 'Machado
\'s grab draws hilarious irate reaction
',
358 'modified_timestamp
': 1675888370,
359 'description
': 'md5
:a19d4eb0487b2cb304e9a176f6b67676
',
360 'modified_date
': '20230208',
362 'playlist_mincount
': 2,
365 def _real_extract(self, url):
366 display_id = self._match_id(url)
367 webpage = self._download_webpage(url, display_id)
368 apollo_cache_json = self._search_json(r'window\
.initState\s
*=', webpage, 'window
.initState
', display_id)['apolloCache
']
370 content_real_info = traverse_obj(
371 apollo_cache_json, ('ROOT_QUERY
', lambda k, _: k.startswith('getArticle
')), get_all=False)
373 return self.playlist_from_matches(
374 traverse_obj(content_real_info, ('parts
', lambda _, v: v['__typename
'] == 'Video
' or v['type'] == 'video
')),
375 getter=lambda x: f'https
://www
.mlb
.com
/video
/{x
["slug"]}',
376 ie=MLBVideoIE, playlist_id=content_real_info.get('translationId
'),
377 title=self._html_search_meta('og
:title
', webpage),
378 description=content_real_info.get('summary
'),
379 modified_timestamp=parse_iso8601(content_real_info.get('lastUpdatedDate
')))