]>
Commit | Line | Data |
---|---|---|
1 | import re | |
2 | import urllib.parse | |
3 | import uuid | |
4 | ||
5 | from .common import InfoExtractor | |
6 | from ..utils import ( | |
7 | determine_ext, | |
8 | int_or_none, | |
9 | join_nonempty, | |
10 | parse_duration, | |
11 | parse_iso8601, | |
12 | traverse_obj, | |
13 | try_get, | |
14 | ) | |
15 | ||
16 | ||
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) | |
24 | ||
25 | formats = [] | |
26 | for playback in (feed.get('playbacks') or []): | |
27 | playback_url = playback.get('url') | |
28 | if not playback_url: | |
29 | continue | |
30 | name = playback.get('name') | |
31 | ext = determine_ext(playback_url) | |
32 | if ext == 'm3u8': | |
33 | formats.extend(self._extract_m3u8_formats( | |
34 | playback_url, video_id, 'mp4', | |
35 | 'm3u8_native', m3u8_id=name, fatal=False)) | |
36 | else: | |
37 | f = { | |
38 | 'format_id': name, | |
39 | 'url': playback_url, | |
40 | } | |
41 | mobj = re.search(r'_(\d+)K_(\d+)X(\d+)', name) | |
42 | if mobj: | |
43 | f.update({ | |
44 | 'height': int(mobj.group(3)), | |
45 | 'tbr': int(mobj.group(1)), | |
46 | 'width': int(mobj.group(2)), | |
47 | }) | |
48 | mobj = re.search(r'_(\d+)x(\d+)_(\d+)_(\d+)K\.mp4', playback_url) | |
49 | if mobj: | |
50 | f.update({ | |
51 | 'fps': int(mobj.group(3)), | |
52 | 'height': int(mobj.group(2)), | |
53 | 'tbr': int(mobj.group(4)), | |
54 | 'width': int(mobj.group(1)), | |
55 | }) | |
56 | formats.append(f) | |
57 | self._sort_formats(formats) | |
58 | ||
59 | thumbnails = [] | |
60 | for cut in (try_get(feed, lambda x: x['image']['cuts'], list) or []): | |
61 | src = cut.get('src') | |
62 | if not src: | |
63 | continue | |
64 | thumbnails.append({ | |
65 | 'height': int_or_none(cut.get('height')), | |
66 | 'url': src, | |
67 | 'width': int_or_none(cut.get('width')), | |
68 | }) | |
69 | ||
70 | language = (video.get('language') or 'EN').lower() | |
71 | ||
72 | return { | |
73 | 'id': video_id, | |
74 | 'title': title, | |
75 | 'formats': formats, | |
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), | |
81 | } | |
82 | ||
83 | ||
84 | class MLBIE(MLBBaseIE): | |
85 | _VALID_URL = r'''(?x) | |
86 | https?:// | |
87 | (?:[\da-z_-]+\.)*mlb\.com/ | |
88 | (?: | |
89 | (?: | |
90 | (?:[^/]+/)*video/[^/]+/c-| | |
91 | (?: | |
92 | shared/video/embed/(?:embed|m-internal-embed)\.html| | |
93 | (?:[^/]+/)+(?:play|index)\.jsp| | |
94 | )\?.*?\bcontent_id= | |
95 | ) | |
96 | (?P<id>\d+) | |
97 | ) | |
98 | ''' | |
99 | _EMBED_REGEX = [ | |
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/[^"\']+)', | |
102 | ] | |
103 | _TESTS = [ | |
104 | { | |
105 | 'url': 'https://www.mlb.com/mariners/video/ackleys-spectacular-catch/c-34698933', | |
106 | 'md5': '632358dacfceec06bad823b83d21df2d', | |
107 | 'info_dict': { | |
108 | 'id': '34698933', | |
109 | 'ext': 'mp4', | |
110 | 'title': "Ackley's spectacular catch", | |
111 | 'description': 'md5:7f5a981eb4f3cbc8daf2aeffa2215bf0', | |
112 | 'duration': 66, | |
113 | 'timestamp': 1405995000, | |
114 | 'upload_date': '20140722', | |
115 | 'thumbnail': r're:^https?://.*\.jpg$', | |
116 | }, | |
117 | }, | |
118 | { | |
119 | 'url': 'https://www.mlb.com/video/stanton-prepares-for-derby/c-34496663', | |
120 | 'md5': 'bf2619bf9cacc0a564fc35e6aeb9219f', | |
121 | 'info_dict': { | |
122 | 'id': '34496663', | |
123 | 'ext': 'mp4', | |
124 | 'title': 'Stanton prepares for Derby', | |
125 | 'description': 'md5:d00ce1e5fd9c9069e9c13ab4faedfa57', | |
126 | 'duration': 46, | |
127 | 'timestamp': 1405120200, | |
128 | 'upload_date': '20140711', | |
129 | 'thumbnail': r're:^https?://.*\.jpg$', | |
130 | }, | |
131 | }, | |
132 | { | |
133 | 'url': 'https://www.mlb.com/video/cespedes-repeats-as-derby-champ/c-34578115', | |
134 | 'md5': '99bb9176531adc600b90880fb8be9328', | |
135 | 'info_dict': { | |
136 | 'id': '34578115', | |
137 | 'ext': 'mp4', | |
138 | 'title': 'Cespedes repeats as Derby champ', | |
139 | 'description': 'md5:08df253ce265d4cf6fb09f581fafad07', | |
140 | 'duration': 488, | |
141 | 'timestamp': 1405414336, | |
142 | 'upload_date': '20140715', | |
143 | 'thumbnail': r're:^https?://.*\.jpg$', | |
144 | }, | |
145 | }, | |
146 | { | |
147 | 'url': 'https://www.mlb.com/video/bautista-on-home-run-derby/c-34577915', | |
148 | 'md5': 'da8b57a12b060e7663ee1eebd6f330ec', | |
149 | 'info_dict': { | |
150 | 'id': '34577915', | |
151 | 'ext': 'mp4', | |
152 | 'title': 'Bautista on Home Run Derby', | |
153 | 'description': 'md5:b80b34031143d0986dddc64a8839f0fb', | |
154 | 'duration': 52, | |
155 | 'timestamp': 1405405122, | |
156 | 'upload_date': '20140715', | |
157 | 'thumbnail': r're:^https?://.*\.jpg$', | |
158 | }, | |
159 | }, | |
160 | { | |
161 | 'url': 'https://www.mlb.com/video/hargrove-homers-off-caldwell/c-1352023483?tid=67793694', | |
162 | 'only_matching': True, | |
163 | }, | |
164 | { | |
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, | |
167 | }, | |
168 | { | |
169 | 'url': 'http://mlb.mlb.com/shared/video/embed/embed.html?content_id=36599553', | |
170 | 'only_matching': True, | |
171 | }, | |
172 | { | |
173 | 'url': 'http://mlb.mlb.com/es/video/play.jsp?content_id=36599553', | |
174 | 'only_matching': True, | |
175 | }, | |
176 | { | |
177 | 'url': 'https://www.mlb.com/cardinals/video/piscottys-great-sliding-catch/c-51175783', | |
178 | 'only_matching': True, | |
179 | }, | |
180 | { | |
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, | |
184 | }, | |
185 | ] | |
186 | _TIMESTAMP_KEY = 'date' | |
187 | ||
188 | @staticmethod | |
189 | def _get_feed(video): | |
190 | return video | |
191 | ||
192 | @staticmethod | |
193 | def _extract_mlb_subtitles(feed, language): | |
194 | subtitles = {} | |
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') | |
199 | if cc_location: | |
200 | subtitles.setdefault(language, []).append({ | |
201 | 'url': cc_location, | |
202 | }) | |
203 | return subtitles | |
204 | ||
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, | |
208 | display_id) | |
209 | ||
210 | ||
211 | class MLBVideoIE(MLBBaseIE): | |
212 | _VALID_URL = r'https?://(?:www\.)?mlb\.com/(?:[^/]+/)*video/(?P<id>[^/?&#]+)' | |
213 | _TEST = { | |
214 | 'url': 'https://www.mlb.com/mariners/video/ackley-s-spectacular-catch-c34698933', | |
215 | 'md5': '632358dacfceec06bad823b83d21df2d', | |
216 | 'info_dict': { | |
217 | 'id': 'c04a8863-f569-42e6-9f87-992393657614', | |
218 | 'ext': 'mp4', | |
219 | 'title': "Ackley's spectacular catch", | |
220 | 'description': 'md5:7f5a981eb4f3cbc8daf2aeffa2215bf0', | |
221 | 'duration': 66, | |
222 | 'timestamp': 1405995000, | |
223 | 'upload_date': '20140722', | |
224 | 'thumbnail': r're:^https?://.+', | |
225 | }, | |
226 | } | |
227 | _TIMESTAMP_KEY = 'timestamp' | |
228 | ||
229 | @classmethod | |
230 | def suitable(cls, url): | |
231 | return False if MLBIE.suitable(url) else super(MLBVideoIE, cls).suitable(url) | |
232 | ||
233 | @staticmethod | |
234 | def _get_feed(video): | |
235 | return video['feeds'][0] | |
236 | ||
237 | @staticmethod | |
238 | def _extract_mlb_subtitles(feed, language): | |
239 | subtitles = {} | |
240 | for cc_location in (feed.get('closedCaptions') or []): | |
241 | subtitles.setdefault(language, []).append({ | |
242 | 'url': cc_location, | |
243 | }) | |
244 | ||
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', | |
249 | display_id, query={ | |
250 | 'query': '''{ | |
251 | mediaPlayback(ids: "%s") { | |
252 | description | |
253 | feeds(types: CMS) { | |
254 | closedCaptions | |
255 | duration | |
256 | image { | |
257 | cuts { | |
258 | width | |
259 | height | |
260 | src | |
261 | } | |
262 | } | |
263 | playbacks { | |
264 | name | |
265 | url | |
266 | } | |
267 | } | |
268 | id | |
269 | timestamp | |
270 | title | |
271 | } | |
272 | }''' % display_id, | |
273 | })['data']['mediaPlayback'][0] | |
274 | ||
275 | ||
276 | class MLBTVIE(InfoExtractor): | |
277 | _VALID_URL = r'https?://(?:www\.)?mlb\.com/tv/g(?P<id>\d{6})' | |
278 | _NETRC_MACHINE = 'mlb' | |
279 | ||
280 | _TESTS = [{ | |
281 | 'url': 'https://www.mlb.com/tv/g661581/vee2eff5f-a7df-4c20-bdb4-7b926fa12638', | |
282 | 'info_dict': { | |
283 | 'id': '661581', | |
284 | 'ext': 'mp4', | |
285 | 'title': '2022-07-02 - St. Louis Cardinals @ Philadelphia Phillies', | |
286 | }, | |
287 | 'params': { | |
288 | 'skip_download': True, | |
289 | }, | |
290 | }] | |
291 | _access_token = None | |
292 | ||
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') | |
297 | ||
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=0oa3e1nutA1HLzAKG356' | |
300 | access_token = self._download_json( | |
301 | 'https://ids.mlb.com/oauth2/aus1m088yK07noBfh356/v1/token', None, | |
302 | headers={ | |
303 | 'User-Agent': 'okhttp/3.12.1', | |
304 | 'Content-Type': 'application/x-www-form-urlencoded' | |
305 | }, data=data.encode())['access_token'] | |
306 | ||
307 | entitlement = self._download_webpage( | |
308 | f'https://media-entitlement.mlb.com/api/v3/jwt?os=Android&appname=AtBat&did={str(uuid.uuid4())}', None, | |
309 | headers={ | |
310 | 'User-Agent': 'okhttp/3.12.1', | |
311 | 'Authorization': f'Bearer {access_token}' | |
312 | }) | |
313 | ||
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, | |
317 | headers={ | |
318 | 'Accept': 'application/json', | |
319 | 'Authorization': 'Bearer bWxidHYmYW5kcm9pZCYxLjAuMA.6LZMbH2r--rbXcgEabaDdIslpo4RyZrlVfWZhsAgXIk', | |
320 | 'Content-Type': 'application/x-www-form-urlencoded' | |
321 | }, data=data.encode())['access_token'] | |
322 | ||
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'] | |
328 | ||
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, | |
333 | headers={ | |
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'))) | |
339 | formats.extend(f) | |
340 | self._merge_subtitles(s, target=subtitles) | |
341 | ||
342 | self._sort_formats(formats) | |
343 | return { | |
344 | 'id': video_id, | |
345 | 'title': traverse_obj(airings, (..., 'titles', 0, 'episodeName'), get_all=False), | |
346 | 'formats': formats, | |
347 | 'subtitles': subtitles, | |
348 | 'http_headers': {'Authorization': f'Bearer {self._access_token}'}, | |
349 | } |