]> jfr.im git - yt-dlp.git/blame - youtube_dl/extractor/nhl.py
[youtube] Relax URL expansion in description
[yt-dlp.git] / youtube_dl / extractor / nhl.py
CommitLineData
25945452
PH
1from __future__ import unicode_literals
2
2e1fa03b
JMF
3import re
4import json
603c9208 5import os
2e1fa03b
JMF
6
7from .common import InfoExtractor
8865bdeb 8from ..compat import (
2e1fa03b 9 compat_urlparse,
15707c7e 10 compat_urllib_parse_urlencode,
29a7e8f6 11 compat_urllib_parse_urlparse,
12 compat_str,
8865bdeb
PH
13)
14from ..utils import (
2e1fa03b 15 unified_strdate,
29a7e8f6 16 determine_ext,
17 int_or_none,
18 parse_iso8601,
19 parse_duration,
2e1fa03b
JMF
20)
21
22
91dbaef4
JMF
23class NHLBaseInfoExtractor(InfoExtractor):
24 @staticmethod
25 def _fix_json(json_string):
26 return json_string.replace('\\\'', '\'')
27
e4c17d72 28 def _real_extract_video(self, video_id):
09b412da
YCH
29 vid_parts = video_id.split(',')
30 if len(vid_parts) == 3:
31 video_id = '%s0%s%s-X-h' % (vid_parts[0][:4], vid_parts[1], vid_parts[2].rjust(4, '0'))
e4c17d72
S
32 json_url = 'http://video.nhl.com/videocenter/servlets/playlist?ids=%s&format=json' % video_id
33 data = self._download_json(
34 json_url, video_id, transform_source=self._fix_json)
35 return self._extract_video(data[0])
36
91dbaef4
JMF
37 def _extract_video(self, info):
38 video_id = info['id']
39 self.report_extraction(video_id)
40
41 initial_video_url = info['publishPoint']
43d9718f 42 if info['formats'] == '1':
62d8b566 43 parsed_url = compat_urllib_parse_urlparse(initial_video_url)
603c9208
JMF
44 filename, ext = os.path.splitext(parsed_url.path)
45 path = '%s_sd%s' % (filename, ext)
15707c7e 46 data = compat_urllib_parse_urlencode({
43d9718f 47 'type': 'fvod',
62d8b566 48 'path': compat_urlparse.urlunparse(parsed_url[:2] + (path,) + parsed_url[3:])
43d9718f
S
49 })
50 path_url = 'http://video.nhl.com/videocenter/servlets/encryptvideopath?' + data
51 path_doc = self._download_xml(
52 path_url, video_id, 'Downloading final video url')
53 video_url = path_doc.find('path').text
54 else:
5f6a1245 55 video_url = initial_video_url
91dbaef4
JMF
56
57 join = compat_urlparse.urljoin
7ef00afe 58 ret = {
91dbaef4
JMF
59 'id': video_id,
60 'title': info['name'],
61 'url': video_url,
91dbaef4
JMF
62 'description': info['description'],
63 'duration': int(info['duration']),
64 'thumbnail': join(join(video_url, '/u/'), info['bigImage']),
65 'upload_date': unified_strdate(info['releaseDate'].split('.')[0]),
66 }
7ef00afe
YCH
67 if video_url.startswith('rtmp:'):
68 mobj = re.match(r'(?P<tc_url>rtmp://[^/]+/(?P<app>[a-z0-9/]+))/(?P<play_path>mp4:.*)', video_url)
69 ret.update({
70 'tc_url': mobj.group('tc_url'),
71 'play_path': mobj.group('play_path'),
72 'app': mobj.group('app'),
73 'no_resume': True,
74 })
75 return ret
91dbaef4
JMF
76
77
29a7e8f6 78class NHLVideocenterIE(NHLBaseInfoExtractor):
79 IE_NAME = 'nhl.com:videocenter'
9fbd4b35 80 _VALID_URL = r'https?://video(?P<team>\.[^.]*)?\.nhl\.com/videocenter/(?:console|embed)?(?:\?(?:.*?[?&])?)(?:id|hlg|playlist)=(?P<id>[-0-9a-zA-Z,]+)'
2e1fa03b 81
7bb5df1c 82 _TESTS = [{
25945452 83 'url': 'http://video.canucks.nhl.com/videocenter/console?catid=6?id=453614',
43d9718f 84 'md5': 'db704a4ea09e8d3988c85e36cc892d09',
25945452
PH
85 'info_dict': {
86 'id': '453614',
87 'ext': 'mp4',
88 'title': 'Quick clip: Weise 4-3 goal vs Flames',
89 'description': 'Dale Weise scores his first of the season to put the Canucks up 4-3.',
90 'duration': 18,
91 'upload_date': '20131006',
2e1fa03b 92 },
43d9718f
S
93 }, {
94 'url': 'http://video.nhl.com/videocenter/console?id=2014020024-628-h',
95 'md5': 'd22e82bc592f52d37d24b03531ee9696',
96 'info_dict': {
97 'id': '2014020024-628-h',
98 'ext': 'mp4',
99 'title': 'Alex Galchenyuk Goal on Ray Emery (14:40/3rd)',
100 'description': 'Home broadcast - Montreal Canadiens at Philadelphia Flyers - October 11, 2014',
101 'duration': 0,
102 'upload_date': '20141011',
103 },
62d8b566
AK
104 }, {
105 'url': 'http://video.mapleleafs.nhl.com/videocenter/console?id=58665&catid=802',
106 'md5': 'c78fc64ea01777e426cfc202b746c825',
107 'info_dict': {
108 'id': '58665',
109 'ext': 'flv',
110 'title': 'Classic Game In Six - April 22, 1979',
111 'description': 'It was the last playoff game for the Leafs in the decade, and the last time the Leafs and Habs played in the playoffs. Great game, not a great ending.',
112 'duration': 400,
113 'upload_date': '20100129'
114 },
7bb5df1c
PH
115 }, {
116 'url': 'http://video.flames.nhl.com/videocenter/console?id=630616',
117 'only_matching': True,
2c58674e
S
118 }, {
119 'url': 'http://video.nhl.com/videocenter/?id=736722',
120 'only_matching': True,
09b412da
YCH
121 }, {
122 'url': 'http://video.nhl.com/videocenter/console?hlg=20142015,2,299&lang=en',
123 'md5': '076fcb88c255154aacbf0a7accc3f340',
124 'info_dict': {
125 'id': '2014020299-X-h',
126 'ext': 'mp4',
127 'title': 'Penguins at Islanders / Game Highlights',
128 'description': 'Home broadcast - Pittsburgh Penguins at New York Islanders - November 22, 2014',
129 'duration': 268,
130 'upload_date': '20141122',
131 }
7ef00afe
YCH
132 }, {
133 'url': 'http://video.oilers.nhl.com/videocenter/console?id=691469&catid=4',
134 'info_dict': {
135 'id': '691469',
136 'ext': 'mp4',
137 'title': 'RAW | Craig MacTavish Full Press Conference',
138 'description': 'Oilers GM Craig MacTavish addresses the media at Rexall Place on Friday.',
139 'upload_date': '20141205',
140 },
141 'params': {
142 'skip_download': True, # Requires rtmpdump
143 }
9fbd4b35
S
144 }, {
145 'url': 'http://video.nhl.com/videocenter/embed?playlist=836127',
146 'only_matching': True,
7bb5df1c 147 }]
2e1fa03b
JMF
148
149 def _real_extract(self, url):
e4c17d72
S
150 video_id = self._match_id(url)
151 return self._real_extract_video(video_id)
152
153
154class NHLNewsIE(NHLBaseInfoExtractor):
155 IE_NAME = 'nhl.com:news'
156 IE_DESC = 'NHL news'
9c58885c 157 _VALID_URL = r'https?://(?:.+?\.)?nhl\.com/(?:ice|club)/news\.html?(?:\?(?:.*?[?&])?)id=(?P<id>[-0-9a-zA-Z]+)'
e4c17d72 158
9c58885c 159 _TESTS = [{
e4c17d72
S
160 'url': 'http://www.nhl.com/ice/news.htm?id=750727',
161 'md5': '4b3d1262e177687a3009937bd9ec0be8',
162 'info_dict': {
163 'id': '736722',
164 'ext': 'mp4',
165 'title': 'Cal Clutterbuck has been fined $2,000',
166 'description': 'md5:45fe547d30edab88b23e0dd0ab1ed9e6',
167 'duration': 37,
168 'upload_date': '20150128',
169 },
9c58885c
S
170 }, {
171 # iframe embed
172 'url': 'http://sabres.nhl.com/club/news.htm?id=780189',
173 'md5': '9f663d1c006c90ac9fb82777d4294e12',
174 'info_dict': {
175 'id': '836127',
176 'ext': 'mp4',
177 'title': 'Morning Skate: OTT vs. BUF (9/23/15)',
178 'description': "Brian Duff chats with Tyler Ennis prior to Buffalo's first preseason home game.",
179 'duration': 93,
180 'upload_date': '20150923',
181 },
182 }]
e4c17d72
S
183
184 def _real_extract(self, url):
185 news_id = self._match_id(url)
186 webpage = self._download_webpage(url, news_id)
187 video_id = self._search_regex(
9c58885c
S
188 [r'pVid(\d+)', r"nlid\s*:\s*'(\d+)'",
189 r'<iframe[^>]+src=["\']https?://video.*?\.nhl\.com/videocenter/embed\?.*\bplaylist=(\d+)'],
e4c17d72
S
190 webpage, 'video id')
191 return self._real_extract_video(video_id)
91dbaef4
JMF
192
193
29a7e8f6 194class NHLVideocenterCategoryIE(NHLBaseInfoExtractor):
195 IE_NAME = 'nhl.com:videocenter:category'
25945452 196 IE_DESC = 'NHL videocenter category'
ea2ee403 197 _VALID_URL = r'https?://video\.(?P<team>[^.]*)\.nhl\.com/videocenter/(console\?[^(id=)]*catid=(?P<catid>[0-9]+)(?![&?]id=).*?)?$'
25945452
PH
198 _TEST = {
199 'url': 'http://video.canucks.nhl.com/videocenter/console?catid=999',
200 'info_dict': {
201 'id': '999',
202 'title': 'Highlights',
203 },
204 'playlist_count': 12,
205 }
91dbaef4
JMF
206
207 def _real_extract(self, url):
208 mobj = re.match(self._VALID_URL, url)
209 team = mobj.group('team')
210 webpage = self._download_webpage(url, team)
211 cat_id = self._search_regex(
212 [r'var defaultCatId = "(.+?)";',
213 r'{statusIndex:0,index:0,.*?id:(.*?),'],
25945452 214 webpage, 'category id')
91dbaef4 215 playlist_title = self._html_search_regex(
ce68b590 216 r'tab0"[^>]*?>(.*?)</td>',
25945452 217 webpage, 'playlist title', flags=re.DOTALL).lower().capitalize()
2e1fa03b 218
15707c7e 219 data = compat_urllib_parse_urlencode({
91dbaef4
JMF
220 'cid': cat_id,
221 # This is the default value
222 'count': 12,
223 'ptrs': 3,
224 'format': 'json',
2e1fa03b 225 })
91dbaef4
JMF
226 path = '/videocenter/servlets/browse?' + data
227 request_url = compat_urlparse.urljoin(url, path)
228 response = self._download_webpage(request_url, playlist_title)
229 response = self._fix_json(response)
230 if not response.strip():
dfb1b146 231 self._downloader.report_warning('Got an empty response, trying '
25945452 232 'adding the "newvideos" parameter')
91dbaef4 233 response = self._download_webpage(request_url + '&newvideos=true',
9e1a5b84 234 playlist_title)
91dbaef4
JMF
235 response = self._fix_json(response)
236 videos = json.loads(response)
2e1fa03b 237
2e1fa03b 238 return {
91dbaef4
JMF
239 '_type': 'playlist',
240 'title': playlist_title,
241 'id': cat_id,
25945452 242 'entries': [self._extract_video(v) for v in videos],
2e1fa03b 243 }
29a7e8f6 244
245
246class NHLIE(InfoExtractor):
247 IE_NAME = 'nhl.com'
248 _VALID_URL = r'https?://(?:www\.)?nhl\.com/([^/]+/)*c-(?P<id>\d+)'
249 _TESTS = [{
250 # type=video
251 'url': 'https://www.nhl.com/video/anisimov-cleans-up-mess/t-277752844/c-43663503',
252 'md5': '0f7b9a8f986fb4b4eeeece9a56416eaf',
253 'info_dict': {
254 'id': '43663503',
255 'ext': 'mp4',
256 'title': 'Anisimov cleans up mess',
257 'description': 'md5:a02354acdfe900e940ce40706939ca63',
258 'timestamp': 1461288600,
259 'upload_date': '20160422',
260 },
261 }, {
262 # type=article
263 'url': 'https://www.nhl.com/news/dennis-wideman-suspended/c-278258934',
264 'md5': '1f39f4ea74c1394dea110699a25b366c',
265 'info_dict': {
266 'id': '40784403',
267 'ext': 'mp4',
268 'title': 'Wideman suspended by NHL',
269 'description': 'Flames defenseman Dennis Wideman was banned 20 games for violation of Rule 40 (Physical Abuse of Officials)',
270 'upload_date': '20160204',
271 'timestamp': 1454544904,
272 },
273 }]
274
275 def _real_extract(self, url):
276 tmp_id = self._match_id(url)
277 video_data = self._download_json(
278 'https://nhl.bamcontent.com/nhl/id/v1/%s/details/web-v1.json' % tmp_id,
279 tmp_id)
280 if video_data.get('type') == 'article':
281 video_data = video_data['media']
282
283 video_id = compat_str(video_data['id'])
284 title = video_data['title']
285
286 formats = []
287 for playback in video_data.get('playbacks', []):
288 playback_url = playback.get('url')
289 if not playback_url:
290 continue
291 ext = determine_ext(playback_url)
292 if ext == 'm3u8':
293 formats.extend(self._extract_m3u8_formats(
294 playback_url, video_id, 'mp4', 'm3u8_native',
295 m3u8_id=playback.get('name', 'hls'), fatal=False))
296 else:
297 height = int_or_none(playback.get('height'))
298 formats.append({
299 'format_id': playback.get('name', 'http' + ('-%dp' % height if height else '')),
300 'url': playback_url,
301 'width': int_or_none(playback.get('width')),
302 'height': height,
303 })
304 self._sort_formats(formats, ('preference', 'width', 'height', 'tbr', 'format_id'))
305
306 thumbnails = []
307 for thumbnail_id, thumbnail_data in video_data.get('image', {}).get('cuts', {}).items():
308 thumbnail_url = thumbnail_data.get('src')
309 if not thumbnail_url:
310 continue
311 thumbnails.append({
312 'id': thumbnail_id,
313 'url': thumbnail_url,
314 'width': int_or_none(thumbnail_data.get('width')),
315 'height': int_or_none(thumbnail_data.get('height')),
316 })
317
318 return {
319 'id': video_id,
320 'title': title,
321 'description': video_data.get('description'),
322 'timestamp': parse_iso8601(video_data.get('date')),
323 'duration': parse_duration(video_data.get('duration')),
324 'thumbnails': thumbnails,
325 'formats': formats,
326 }