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)),
57 self
._sort
_formats
(formats
)
60 for cut
in (try_get(feed
, lambda x
: x
['image']['cuts'], list) or []):
65 'height': int_or_none(cut
.get('height')),
67 'width': int_or_none(cut
.get('width')),
70 language
= (video
.get('language') or 'EN').lower()
76 'description': video
.get('description'),
77 'duration': parse_duration(feed
.get('duration')),
78 'thumbnails': thumbnails
,
79 'timestamp': parse_iso8601(video
.get(self
._TIMESTAMP
_KEY
)),
80 'subtitles': self
._extract
_mlb
_subtitles
(feed
, language
),
84 class MLBIE(MLBBaseIE
):
87 (?:[\da-z_-]+\.)*mlb\.com/
90 (?:[^/]+/)*video/[^/]+/c-|
92 shared/video/embed/(?:embed|m-internal-embed)\.html|
93 (?:[^/]+/)+(?:play|index)\.jsp|
100 r
'<iframe[^>]+?src=(["\'])(?P
<url
>https?
://m(?
:lb
)?\
.mlb\
.com
/shared
/video
/embed
/embed\
.html
\?.+?
)\
1',
101 r'data
-video
-link
=["\'](?P<url>http://m\.mlb\.com/video/[^"\']+)',
105 'url
': 'https
://www
.mlb
.com
/mariners
/video
/ackleys
-spectacular
-catch
/c
-34698933',
106 'md5
': '632358dacfceec06bad823b83d21df2d
',
110 'title
': "Ackley's spectacular catch
",
111 'description': 'md5:7f5a981eb4f3cbc8daf2aeffa2215bf0',
113 'timestamp': 1405995000,
114 'upload_date': '20140722',
115 'thumbnail': r're:^https?://.*\.jpg$',
119 'url': 'https://www.mlb.com/video/stanton-prepares-for-derby/c-34496663',
120 'md5': 'bf2619bf9cacc0a564fc35e6aeb9219f',
124 'title': 'Stanton prepares for Derby',
125 'description': 'md5:d00ce1e5fd9c9069e9c13ab4faedfa57',
127 'timestamp': 1405120200,
128 'upload_date': '20140711',
129 'thumbnail': r're:^https?://.*\.jpg$',
133 'url': 'https://www.mlb.com/video/cespedes-repeats-as-derby-champ/c-34578115',
134 'md5': '99bb9176531adc600b90880fb8be9328',
138 'title': 'Cespedes repeats as Derby champ',
139 'description': 'md5:08df253ce265d4cf6fb09f581fafad07',
141 'timestamp': 1405414336,
142 'upload_date': '20140715',
143 'thumbnail': r're:^https?://.*\.jpg$',
147 'url': 'https://www.mlb.com/video/bautista-on-home-run-derby/c-34577915',
148 'md5': 'da8b57a12b060e7663ee1eebd6f330ec',
152 'title': 'Bautista on Home Run Derby',
153 'description': 'md5:b80b34031143d0986dddc64a8839f0fb',
155 'timestamp': 1405405122,
156 'upload_date': '20140715',
157 'thumbnail': r're:^https?://.*\.jpg$',
161 'url': 'https://www.mlb.com/video/hargrove-homers-off-caldwell/c-1352023483?tid=67793694',
162 'only_matching': True,
165 'url': 'http://m.mlb.com/shared/video/embed/embed.html?content_id=35692085&topic_id=6479266&width=400&height=224&property=mlb',
166 'only_matching': True,
169 'url': 'http://mlb.mlb.com/shared/video/embed/embed.html?content_id=36599553',
170 'only_matching': True,
173 'url': 'http://mlb.mlb.com/es/video/play.jsp?content_id=36599553',
174 'only_matching': True,
177 'url': 'https://www.mlb.com/cardinals/video/piscottys-great-sliding-catch/c-51175783',
178 'only_matching': True,
181 # From http://m.mlb.com/news/article/118550098/blue-jays-kevin-pillar-goes-spidey-up-the-wall-to-rob-tim-beckham-of-a-homer
182 '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',
183 'only_matching': True,
186 _TIMESTAMP_KEY = 'date'
189 def _get_feed(video):
193 def _extract_mlb_subtitles(feed, language):
195 for keyword in (feed.get('keywordsAll') or []):
196 keyword_type = keyword.get('type')
197 if keyword_type and keyword_type.startswith('closed_captions_location_'):
198 cc_location = keyword.get('value')
200 subtitles.setdefault(language, []).append({
205 def _download_video_data(self, display_id):
206 return self._download_json(
207 'http://content.mlb.com/mlb/item/id/v1/%s/details/web-v1.json' % display_id,
211 class MLBVideoIE(MLBBaseIE):
212 _VALID_URL = r'https?://(?:www\.)?mlb\.com/(?:[^/]+/)*video/(?P<id>[^/?&#]+)'
214 'url': 'https://www.mlb.com/mariners/video/ackley-s-spectacular-catch-c34698933',
215 'md5': '632358dacfceec06bad823b83d21df2d',
217 'id': 'c04a8863-f569-42e6-9f87-992393657614',
219 'title': "Ackley
's spectacular catch",
220 'description
': 'md5
:7f5a981eb4f3cbc8daf2aeffa2215bf0
',
222 'timestamp
': 1405995000,
223 'upload_date
': '20140722',
224 'thumbnail
': r're
:^https?
://.+',
227 _TIMESTAMP_KEY = 'timestamp
'
230 def suitable(cls, url):
231 return False if MLBIE.suitable(url) else super(MLBVideoIE, cls).suitable(url)
234 def _get_feed(video):
235 return video['feeds
'][0]
238 def _extract_mlb_subtitles(feed, language):
240 for cc_location in (feed.get('closedCaptions
') or []):
241 subtitles.setdefault(language, []).append({
245 def _download_video_data(self, display_id):
246 # https://www.mlb.com/data-service/en/videos/[SLUG]
247 return self._download_json(
248 'https
://fastball
-gateway
.mlb
.com
/graphql
',
251 mediaPlayback(ids: "%s") {
273 })['data
']['mediaPlayback
'][0]
276 class MLBTVIE(InfoExtractor):
277 _VALID_URL = r'https?
://(?
:www\
.)?mlb\
.com
/tv
/g(?P
<id>\d{6}
)'
278 _NETRC_MACHINE = 'mlb
'
281 'url
': 'https
://www
.mlb
.com
/tv
/g661581
/vee2eff5f
-a7df
-4c20
-bdb4
-7b926fa12638
',
285 'title
': '2022-07-02 - St
. Louis Cardinals
@ Philadelphia Phillies
',
288 'skip_download
': True,
293 def _real_initialize(self):
294 if not self._access_token:
295 self.raise_login_required(
296 'All videos are only available to registered users
', method='password
')
298 def _perform_login(self, username, password):
299 data = f'grant_type
=password
&username
={urllib.parse.quote(username)}
&password
={urllib.parse.quote(password)}
&scope
=openid offline_access
&client_id
=0oa3e1nutA
1HLzAKG
356'
300 access_token = self._download_json(
301 'https
://ids
.mlb
.com
/oauth2
/aus1m088yK07noBfh356
/v1
/token
', None,
303 'User
-Agent
': 'okhttp
/3.12.1',
304 'Content
-Type
': 'application
/x
-www
-form
-urlencoded
'
305 }, data=data.encode())['access_token
']
307 entitlement = self._download_webpage(
308 f'https
://media
-entitlement
.mlb
.com
/api
/v3
/jwt?os
=Android
&appname
=AtBat
&did
={str(uuid.uuid4())}
', None,
310 'User
-Agent
': 'okhttp
/3.12.1',
311 'Authorization
': f'Bearer {access_token}
'
314 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
'
315 self._access_token = self._download_json(
316 'https
://us
.edge
.bamgrid
.com
/token
', None,
318 'Accept
': 'application
/json
',
319 'Authorization
': 'Bearer bWxidHYmYW5kcm9pZCYxLjAuMA
.6LZMbH
2r
--rbXcgEabaDdIslpo4RyZrlVfWZhsAgXIk
',
320 'Content
-Type
': 'application
/x
-www
-form
-urlencoded
'
321 }, data=data.encode())['access_token
']
323 def _real_extract(self, url):
324 video_id = self._match_id(url)
325 airings = self._download_json(
326 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
',
327 video_id)['data
']['Airings
']
329 formats, subtitles = [], {}
330 for airing in airings:
331 m3u8_url = self._download_json(
332 airing['playbackUrls
'][0]['href
'].format(scenario='browser~csai
'), video_id,
334 'Authorization
': self._access_token,
335 'Accept
': 'application
/vnd
.media
-service
+json
; version
=2'
336 })['stream
']['complete
']
337 f, s = self._extract_m3u8_formats_and_subtitles(
338 m3u8_url, video_id, 'mp4
', m3u8_id=join_nonempty(airing.get('feedType
'), airing.get('feedLanguage
')))
340 self._merge_subtitles(s, target=subtitles)
342 self._sort_formats(formats)
345 'title
': traverse_obj(airings, (..., 'titles
', 0, 'episodeName
'), get_all=False),
346 'is_live
': traverse_obj(airings, (..., 'mediaConfig
', 'productType
'), get_all=False) == 'LIVE
',
348 'subtitles
': subtitles,
349 'http_headers
': {'Authorization': f'Bearer {self._access_token}'},