]> jfr.im git - yt-dlp.git/blame - youtube_dl/extractor/nhl.py
Merge remote-tracking branch 'rupertbaxter2/master'
[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
JMF
9 compat_urlparse,
10 compat_urllib_parse,
62d8b566 11 compat_urllib_parse_urlparse
8865bdeb
PH
12)
13from ..utils import (
2e1fa03b
JMF
14 unified_strdate,
15)
16
17
91dbaef4
JMF
18class NHLBaseInfoExtractor(InfoExtractor):
19 @staticmethod
20 def _fix_json(json_string):
21 return json_string.replace('\\\'', '\'')
22
23 def _extract_video(self, info):
24 video_id = info['id']
25 self.report_extraction(video_id)
26
27 initial_video_url = info['publishPoint']
43d9718f 28 if info['formats'] == '1':
62d8b566 29 parsed_url = compat_urllib_parse_urlparse(initial_video_url)
603c9208
JMF
30 filename, ext = os.path.splitext(parsed_url.path)
31 path = '%s_sd%s' % (filename, ext)
43d9718f
S
32 data = compat_urllib_parse.urlencode({
33 'type': 'fvod',
62d8b566 34 'path': compat_urlparse.urlunparse(parsed_url[:2] + (path,) + parsed_url[3:])
43d9718f
S
35 })
36 path_url = 'http://video.nhl.com/videocenter/servlets/encryptvideopath?' + data
37 path_doc = self._download_xml(
38 path_url, video_id, 'Downloading final video url')
39 video_url = path_doc.find('path').text
40 else:
5f6a1245 41 video_url = initial_video_url
91dbaef4
JMF
42
43 join = compat_urlparse.urljoin
44 return {
45 'id': video_id,
46 'title': info['name'],
47 'url': video_url,
91dbaef4
JMF
48 'description': info['description'],
49 'duration': int(info['duration']),
50 'thumbnail': join(join(video_url, '/u/'), info['bigImage']),
51 'upload_date': unified_strdate(info['releaseDate'].split('.')[0]),
52 }
53
54
55class NHLIE(NHLBaseInfoExtractor):
25945452 56 IE_NAME = 'nhl.com'
b1ccbed3 57 _VALID_URL = r'https?://video(?P<team>\.[^.]*)?\.nhl\.com/videocenter/console(?:\?(?:.*?[?&])?)id=(?P<id>[-0-9a-zA-Z]+)'
2e1fa03b 58
7bb5df1c 59 _TESTS = [{
25945452 60 'url': 'http://video.canucks.nhl.com/videocenter/console?catid=6?id=453614',
43d9718f 61 'md5': 'db704a4ea09e8d3988c85e36cc892d09',
25945452
PH
62 'info_dict': {
63 'id': '453614',
64 'ext': 'mp4',
65 'title': 'Quick clip: Weise 4-3 goal vs Flames',
66 'description': 'Dale Weise scores his first of the season to put the Canucks up 4-3.',
67 'duration': 18,
68 'upload_date': '20131006',
2e1fa03b 69 },
43d9718f
S
70 }, {
71 'url': 'http://video.nhl.com/videocenter/console?id=2014020024-628-h',
72 'md5': 'd22e82bc592f52d37d24b03531ee9696',
73 'info_dict': {
74 'id': '2014020024-628-h',
75 'ext': 'mp4',
76 'title': 'Alex Galchenyuk Goal on Ray Emery (14:40/3rd)',
77 'description': 'Home broadcast - Montreal Canadiens at Philadelphia Flyers - October 11, 2014',
78 'duration': 0,
79 'upload_date': '20141011',
80 },
62d8b566
AK
81 }, {
82 'url': 'http://video.mapleleafs.nhl.com/videocenter/console?id=58665&catid=802',
83 'md5': 'c78fc64ea01777e426cfc202b746c825',
84 'info_dict': {
85 'id': '58665',
86 'ext': 'flv',
87 'title': 'Classic Game In Six - April 22, 1979',
88 '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.',
89 'duration': 400,
90 'upload_date': '20100129'
91 },
7bb5df1c
PH
92 }, {
93 'url': 'http://video.flames.nhl.com/videocenter/console?id=630616',
94 'only_matching': True,
95 }]
2e1fa03b
JMF
96
97 def _real_extract(self, url):
98 mobj = re.match(self._VALID_URL, url)
99 video_id = mobj.group('id')
100 json_url = 'http://video.nhl.com/videocenter/servlets/playlist?ids=%s&format=json' % video_id
25945452
PH
101 data = self._download_json(
102 json_url, video_id, transform_source=self._fix_json)
103 return self._extract_video(data[0])
91dbaef4
JMF
104
105
106class NHLVideocenterIE(NHLBaseInfoExtractor):
25945452
PH
107 IE_NAME = 'nhl.com:videocenter'
108 IE_DESC = 'NHL videocenter category'
ea2ee403 109 _VALID_URL = r'https?://video\.(?P<team>[^.]*)\.nhl\.com/videocenter/(console\?[^(id=)]*catid=(?P<catid>[0-9]+)(?![&?]id=).*?)?$'
25945452
PH
110 _TEST = {
111 'url': 'http://video.canucks.nhl.com/videocenter/console?catid=999',
112 'info_dict': {
113 'id': '999',
114 'title': 'Highlights',
115 },
116 'playlist_count': 12,
117 }
91dbaef4
JMF
118
119 def _real_extract(self, url):
120 mobj = re.match(self._VALID_URL, url)
121 team = mobj.group('team')
122 webpage = self._download_webpage(url, team)
123 cat_id = self._search_regex(
124 [r'var defaultCatId = "(.+?)";',
125 r'{statusIndex:0,index:0,.*?id:(.*?),'],
25945452 126 webpage, 'category id')
91dbaef4 127 playlist_title = self._html_search_regex(
ce68b590 128 r'tab0"[^>]*?>(.*?)</td>',
25945452 129 webpage, 'playlist title', flags=re.DOTALL).lower().capitalize()
2e1fa03b 130
2e1fa03b 131 data = compat_urllib_parse.urlencode({
91dbaef4
JMF
132 'cid': cat_id,
133 # This is the default value
134 'count': 12,
135 'ptrs': 3,
136 'format': 'json',
2e1fa03b 137 })
91dbaef4
JMF
138 path = '/videocenter/servlets/browse?' + data
139 request_url = compat_urlparse.urljoin(url, path)
140 response = self._download_webpage(request_url, playlist_title)
141 response = self._fix_json(response)
142 if not response.strip():
8865bdeb 143 self._downloader.report_warning('Got an empty reponse, trying '
25945452 144 'adding the "newvideos" parameter')
91dbaef4 145 response = self._download_webpage(request_url + '&newvideos=true',
9e1a5b84 146 playlist_title)
91dbaef4
JMF
147 response = self._fix_json(response)
148 videos = json.loads(response)
2e1fa03b 149
2e1fa03b 150 return {
91dbaef4
JMF
151 '_type': 'playlist',
152 'title': playlist_title,
153 'id': cat_id,
25945452 154 'entries': [self._extract_video(v) for v in videos],
2e1fa03b 155 }