]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/espn.py
[extractor] Fix bug in 617f658b7ec1193749848c1b7343acab125dbc46
[yt-dlp.git] / yt_dlp / extractor / espn.py
CommitLineData
0f897e09
RA
1import re
2
7e760fc1 3from .common import InfoExtractor
0f897e09 4from .once import OnceIE
ebc7ab1e
S
5from ..compat import compat_str
6from ..utils import (
7 determine_ext,
266a1b5d 8 dict_get,
ebc7ab1e 9 int_or_none,
266a1b5d 10 unified_strdate,
ebc7ab1e
S
11 unified_timestamp,
12)
7e760fc1
S
13
14
0f897e09 15class ESPNIE(OnceIE):
60d4401c
PV
16 _VALID_URL = r'''(?x)
17 https?://
60d4401c
PV
18 (?:
19 (?:
0f897e09
RA
20 (?:
21 (?:(?:\w+\.)+)?espn\.go|
22 (?:www\.)?espn
23 )\.com/
24 (?:
25 (?:
26 video/(?:clip|iframe/twitter)|
27 watch/player
28 )
29 (?:
30 .*?\?.*?\bid=|
31 /_/id/
398e1e21
RA
32 )|
33 [^/]+/video/
0f897e09
RA
34 )
35 )|
36 (?:www\.)espnfc\.(?:com|us)/(?:video/)?[^/]+/\d+/video/
60d4401c
PV
37 )
38 (?P<id>\d+)
39 '''
40
7e760fc1
S
41 _TESTS = [{
42 'url': 'http://espn.go.com/video/clip?id=10365079',
43 'info_dict': {
ebc7ab1e 44 'id': '10365079',
7e760fc1 45 'ext': 'mp4',
69f85952 46 'title': '30 for 30 Shorts: Judging Jewell',
ebc7ab1e
S
47 'description': 'md5:39370c2e016cb4ecf498ffe75bef7f0f',
48 'timestamp': 1390936111,
49 'upload_date': '20140128',
7e760fc1 50 },
688c634b 51 'params': {
52 'skip_download': True,
53 },
930087f2 54 }, {
60d4401c 55 'url': 'https://broadband.espn.go.com/video/clip?id=18910086',
930087f2 56 'info_dict': {
60d4401c 57 'id': '18910086',
930087f2 58 'ext': 'mp4',
60d4401c
PV
59 'title': 'Kyrie spins around defender for two',
60 'description': 'md5:2b0f5bae9616d26fba8808350f0d2b9b',
61 'timestamp': 1489539155,
62 'upload_date': '20170315',
930087f2 63 },
688c634b 64 'params': {
65 'skip_download': True,
66 },
ebc7ab1e 67 'expected_warnings': ['Unable to download f4m manifest'],
60d4401c
PV
68 }, {
69 'url': 'http://nonredline.sports.espn.go.com/video/clip?id=19744672',
70 'only_matching': True,
71 }, {
72 'url': 'https://cdn.espn.go.com/video/clip/_/id/19771774',
73 'only_matching': True,
74 }, {
75 'url': 'http://www.espn.com/watch/player?id=19141491',
76 'only_matching': True,
77 }, {
78 'url': 'http://www.espn.com/watch/player?bucketId=257&id=19505875',
79 'only_matching': True,
80 }, {
81 'url': 'http://www.espn.com/watch/player/_/id/19141491',
82 'only_matching': True,
7e760fc1 83 }, {
ebc7ab1e
S
84 'url': 'http://www.espn.com/video/clip?id=10365079',
85 'only_matching': True,
86 }, {
87 'url': 'http://www.espn.com/video/clip/_/id/17989860',
88 'only_matching': True,
0f897e09
RA
89 }, {
90 'url': 'https://espn.go.com/video/iframe/twitter/?cms=espn&id=10365079',
91 'only_matching': True,
92 }, {
93 'url': 'http://www.espnfc.us/video/espn-fc-tv/86/video/3319154/nashville-unveiled-as-the-newest-club-in-mls',
94 'only_matching': True,
95 }, {
96 'url': 'http://www.espnfc.com/english-premier-league/23/video/3324163/premier-league-in-90-seconds-golden-tweets',
97 'only_matching': True,
398e1e21
RA
98 }, {
99 'url': 'http://www.espn.com/espnw/video/26066627/arkansas-gibson-completes-hr-cycle-four-innings',
100 'only_matching': True,
ebc7ab1e
S
101 }]
102
103 def _real_extract(self, url):
104 video_id = self._match_id(url)
105
106 clip = self._download_json(
107 'http://api-app.espn.com/v1/video/clips/%s' % video_id,
108 video_id)['videos'][0]
109
110 title = clip['headline']
111
112 format_urls = set()
113 formats = []
114
115 def traverse_source(source, base_source_id=None):
116 for source_id, source in source.items():
0f897e09
RA
117 if source_id == 'alert':
118 continue
119 elif isinstance(source, compat_str):
ebc7ab1e
S
120 extract_source(source, base_source_id)
121 elif isinstance(source, dict):
122 traverse_source(
123 source,
124 '%s-%s' % (base_source_id, source_id)
125 if base_source_id else source_id)
126
127 def extract_source(source_url, source_id=None):
128 if source_url in format_urls:
129 return
130 format_urls.add(source_url)
131 ext = determine_ext(source_url)
0f897e09
RA
132 if OnceIE.suitable(source_url):
133 formats.extend(self._extract_once_formats(source_url))
134 elif ext == 'smil':
ebc7ab1e
S
135 formats.extend(self._extract_smil_formats(
136 source_url, video_id, fatal=False))
137 elif ext == 'f4m':
138 formats.extend(self._extract_f4m_formats(
139 source_url, video_id, f4m_id=source_id, fatal=False))
140 elif ext == 'm3u8':
141 formats.extend(self._extract_m3u8_formats(
142 source_url, video_id, 'mp4', entry_protocol='m3u8_native',
143 m3u8_id=source_id, fatal=False))
144 else:
0f897e09 145 f = {
ebc7ab1e
S
146 'url': source_url,
147 'format_id': source_id,
0f897e09
RA
148 }
149 mobj = re.search(r'(\d+)p(\d+)_(\d+)k\.', source_url)
150 if mobj:
151 f.update({
152 'height': int(mobj.group(1)),
153 'fps': int(mobj.group(2)),
154 'tbr': int(mobj.group(3)),
155 })
156 if source_id == 'mezzanine':
f983b875 157 f['quality'] = 1
0f897e09 158 formats.append(f)
ebc7ab1e 159
0f897e09
RA
160 links = clip.get('links', {})
161 traverse_source(links.get('source', {}))
162 traverse_source(links.get('mobile', {}))
ebc7ab1e
S
163 self._sort_formats(formats)
164
165 description = clip.get('caption') or clip.get('description')
166 thumbnail = clip.get('thumbnail')
167 duration = int_or_none(clip.get('duration'))
168 timestamp = unified_timestamp(clip.get('originalPublishDate'))
169
170 return {
171 'id': video_id,
172 'title': title,
173 'description': description,
174 'thumbnail': thumbnail,
175 'timestamp': timestamp,
176 'duration': duration,
177 'formats': formats,
178 }
179
180
181class ESPNArticleIE(InfoExtractor):
182 _VALID_URL = r'https?://(?:espn\.go|(?:www\.)?espn)\.com/(?:[^/]+/)*(?P<id>[^/]+)'
183 _TESTS = [{
7e760fc1
S
184 'url': 'http://espn.go.com/nba/recap?gameId=400793786',
185 'only_matching': True,
186 }, {
187 'url': 'http://espn.go.com/blog/golden-state-warriors/post/_/id/593/how-warriors-rapidly-regained-a-winning-edge',
188 'only_matching': True,
189 }, {
190 'url': 'http://espn.go.com/sports/endurance/story/_/id/12893522/dzhokhar-tsarnaev-sentenced-role-boston-marathon-bombings',
191 'only_matching': True,
192 }, {
193 'url': 'http://espn.go.com/nba/playoffs/2015/story/_/id/12887571/john-wall-washington-wizards-no-swelling-left-hand-wrist-game-5-return',
194 'only_matching': True,
195 }]
196
ebc7ab1e
S
197 @classmethod
198 def suitable(cls, url):
199 return False if ESPNIE.suitable(url) else super(ESPNArticleIE, cls).suitable(url)
200
7e760fc1
S
201 def _real_extract(self, url):
202 video_id = self._match_id(url)
203
204 webpage = self._download_webpage(url, video_id)
205
206 video_id = self._search_regex(
83ab8a79
S
207 r'class=(["\']).*?video-play-button.*?\1[^>]+data-id=["\'](?P<id>\d+)',
208 webpage, 'video id', group='id')
7e760fc1 209
ebc7ab1e
S
210 return self.url_result(
211 'http://espn.go.com/video/clip?id=%s' % video_id, ESPNIE.ie_key())
db145ee5
RA
212
213
214class FiveThirtyEightIE(InfoExtractor):
215 _VALID_URL = r'https?://(?:www\.)?fivethirtyeight\.com/features/(?P<id>[^/?#]+)'
216 _TEST = {
217 'url': 'http://fivethirtyeight.com/features/how-the-6-8-raiders-can-still-make-the-playoffs/',
218 'info_dict': {
16d3672a
RA
219 'id': '56032156',
220 'ext': 'flv',
db145ee5
RA
221 'title': 'FiveThirtyEight: The Raiders can still make the playoffs',
222 'description': 'Neil Paine breaks down the simplest scenario that will put the Raiders into the playoffs at 8-8.',
db145ee5
RA
223 },
224 'params': {
225 'skip_download': True,
226 },
db145ee5
RA
227 }
228
229 def _real_extract(self, url):
230 video_id = self._match_id(url)
231
232 webpage = self._download_webpage(url, video_id)
233
16d3672a
RA
234 embed_url = self._search_regex(
235 r'<iframe[^>]+src=["\'](https?://fivethirtyeight\.abcnews\.go\.com/video/embed/\d+/\d+)',
236 webpage, 'embed url')
db145ee5 237
16d3672a 238 return self.url_result(embed_url, 'AbcNewsVideo')
266a1b5d
AG
239
240
241class ESPNCricInfoIE(InfoExtractor):
242 _VALID_URL = r'https?://(?:www\.)?espncricinfo\.com/video/[^#$&?/]+-(?P<id>\d+)'
243 _TESTS = [{
244 'url': 'https://www.espncricinfo.com/video/finch-chasing-comes-with-risks-despite-world-cup-trend-1289135',
245 'info_dict': {
246 'id': '1289135',
247 'ext': 'mp4',
248 'title': 'Finch: Chasing comes with \'risks\' despite World Cup trend',
249 'description': 'md5:ea32373303e25efbb146efdfc8a37829',
250 'upload_date': '20211113',
251 'duration': 96,
252 },
253 'params': {'skip_download': True}
254 }]
255
256 def _real_extract(self, url):
257 id = self._match_id(url)
258 data_json = self._download_json(f'https://hs-consumer-api.espncricinfo.com/v1/pages/video/video-details?videoId={id}', id)['video']
259 formats, subtitles = [], {}
260 for item in data_json.get('playbacks') or []:
261 if item.get('type') == 'HLS' and item.get('url'):
262 m3u8_frmts, m3u8_subs = self._extract_m3u8_formats_and_subtitles(item['url'], id)
263 formats.extend(m3u8_frmts)
264 subtitles = self._merge_subtitles(subtitles, m3u8_subs)
265 elif item.get('type') == 'AUDIO' and item.get('url'):
266 formats.append({
267 'url': item['url'],
268 'vcodec': 'none',
269 })
270 self._sort_formats(formats)
271 return {
272 'id': id,
273 'title': data_json.get('title'),
274 'description': data_json.get('summary'),
275 'upload_date': unified_strdate(dict_get(data_json, ('publishedAt', 'recordedAt'))),
276 'duration': data_json.get('duration'),
277 'formats': formats,
278 'subtitles': subtitles,
279 }