]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/nhl.py
Imported Upstream version 2015.02.06
[youtubedl] / youtube_dl / extractor / nhl.py
1 from __future__ import unicode_literals
2
3 import re
4 import json
5 import os
6
7 from .common import InfoExtractor
8 from ..compat import (
9 compat_urlparse,
10 compat_urllib_parse,
11 compat_urllib_parse_urlparse
12 )
13 from ..utils import (
14 unified_strdate,
15 )
16
17
18 class NHLBaseInfoExtractor(InfoExtractor):
19 @staticmethod
20 def _fix_json(json_string):
21 return json_string.replace('\\\'', '\'')
22
23 def _real_extract_video(self, video_id):
24 json_url = 'http://video.nhl.com/videocenter/servlets/playlist?ids=%s&format=json' % video_id
25 data = self._download_json(
26 json_url, video_id, transform_source=self._fix_json)
27 return self._extract_video(data[0])
28
29 def _extract_video(self, info):
30 video_id = info['id']
31 self.report_extraction(video_id)
32
33 initial_video_url = info['publishPoint']
34 if info['formats'] == '1':
35 parsed_url = compat_urllib_parse_urlparse(initial_video_url)
36 filename, ext = os.path.splitext(parsed_url.path)
37 path = '%s_sd%s' % (filename, ext)
38 data = compat_urllib_parse.urlencode({
39 'type': 'fvod',
40 'path': compat_urlparse.urlunparse(parsed_url[:2] + (path,) + parsed_url[3:])
41 })
42 path_url = 'http://video.nhl.com/videocenter/servlets/encryptvideopath?' + data
43 path_doc = self._download_xml(
44 path_url, video_id, 'Downloading final video url')
45 video_url = path_doc.find('path').text
46 else:
47 video_url = initial_video_url
48
49 join = compat_urlparse.urljoin
50 return {
51 'id': video_id,
52 'title': info['name'],
53 'url': video_url,
54 'description': info['description'],
55 'duration': int(info['duration']),
56 'thumbnail': join(join(video_url, '/u/'), info['bigImage']),
57 'upload_date': unified_strdate(info['releaseDate'].split('.')[0]),
58 }
59
60
61 class NHLIE(NHLBaseInfoExtractor):
62 IE_NAME = 'nhl.com'
63 _VALID_URL = r'https?://video(?P<team>\.[^.]*)?\.nhl\.com/videocenter/(?:console)?(?:\?(?:.*?[?&])?)id=(?P<id>[-0-9a-zA-Z]+)'
64
65 _TESTS = [{
66 'url': 'http://video.canucks.nhl.com/videocenter/console?catid=6?id=453614',
67 'md5': 'db704a4ea09e8d3988c85e36cc892d09',
68 'info_dict': {
69 'id': '453614',
70 'ext': 'mp4',
71 'title': 'Quick clip: Weise 4-3 goal vs Flames',
72 'description': 'Dale Weise scores his first of the season to put the Canucks up 4-3.',
73 'duration': 18,
74 'upload_date': '20131006',
75 },
76 }, {
77 'url': 'http://video.nhl.com/videocenter/console?id=2014020024-628-h',
78 'md5': 'd22e82bc592f52d37d24b03531ee9696',
79 'info_dict': {
80 'id': '2014020024-628-h',
81 'ext': 'mp4',
82 'title': 'Alex Galchenyuk Goal on Ray Emery (14:40/3rd)',
83 'description': 'Home broadcast - Montreal Canadiens at Philadelphia Flyers - October 11, 2014',
84 'duration': 0,
85 'upload_date': '20141011',
86 },
87 }, {
88 'url': 'http://video.mapleleafs.nhl.com/videocenter/console?id=58665&catid=802',
89 'md5': 'c78fc64ea01777e426cfc202b746c825',
90 'info_dict': {
91 'id': '58665',
92 'ext': 'flv',
93 'title': 'Classic Game In Six - April 22, 1979',
94 '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.',
95 'duration': 400,
96 'upload_date': '20100129'
97 },
98 }, {
99 'url': 'http://video.flames.nhl.com/videocenter/console?id=630616',
100 'only_matching': True,
101 }, {
102 'url': 'http://video.nhl.com/videocenter/?id=736722',
103 'only_matching': True,
104 }]
105
106 def _real_extract(self, url):
107 video_id = self._match_id(url)
108 return self._real_extract_video(video_id)
109
110
111 class NHLNewsIE(NHLBaseInfoExtractor):
112 IE_NAME = 'nhl.com:news'
113 IE_DESC = 'NHL news'
114 _VALID_URL = r'https?://(?:www\.)?nhl\.com/ice/news\.html?(?:\?(?:.*?[?&])?)id=(?P<id>[-0-9a-zA-Z]+)'
115
116 _TEST = {
117 'url': 'http://www.nhl.com/ice/news.htm?id=750727',
118 'md5': '4b3d1262e177687a3009937bd9ec0be8',
119 'info_dict': {
120 'id': '736722',
121 'ext': 'mp4',
122 'title': 'Cal Clutterbuck has been fined $2,000',
123 'description': 'md5:45fe547d30edab88b23e0dd0ab1ed9e6',
124 'duration': 37,
125 'upload_date': '20150128',
126 },
127 }
128
129 def _real_extract(self, url):
130 news_id = self._match_id(url)
131 webpage = self._download_webpage(url, news_id)
132 video_id = self._search_regex(
133 [r'pVid(\d+)', r"nlid\s*:\s*'(\d+)'"],
134 webpage, 'video id')
135 return self._real_extract_video(video_id)
136
137
138 class NHLVideocenterIE(NHLBaseInfoExtractor):
139 IE_NAME = 'nhl.com:videocenter'
140 IE_DESC = 'NHL videocenter category'
141 _VALID_URL = r'https?://video\.(?P<team>[^.]*)\.nhl\.com/videocenter/(console\?[^(id=)]*catid=(?P<catid>[0-9]+)(?![&?]id=).*?)?$'
142 _TEST = {
143 'url': 'http://video.canucks.nhl.com/videocenter/console?catid=999',
144 'info_dict': {
145 'id': '999',
146 'title': 'Highlights',
147 },
148 'playlist_count': 12,
149 }
150
151 def _real_extract(self, url):
152 mobj = re.match(self._VALID_URL, url)
153 team = mobj.group('team')
154 webpage = self._download_webpage(url, team)
155 cat_id = self._search_regex(
156 [r'var defaultCatId = "(.+?)";',
157 r'{statusIndex:0,index:0,.*?id:(.*?),'],
158 webpage, 'category id')
159 playlist_title = self._html_search_regex(
160 r'tab0"[^>]*?>(.*?)</td>',
161 webpage, 'playlist title', flags=re.DOTALL).lower().capitalize()
162
163 data = compat_urllib_parse.urlencode({
164 'cid': cat_id,
165 # This is the default value
166 'count': 12,
167 'ptrs': 3,
168 'format': 'json',
169 })
170 path = '/videocenter/servlets/browse?' + data
171 request_url = compat_urlparse.urljoin(url, path)
172 response = self._download_webpage(request_url, playlist_title)
173 response = self._fix_json(response)
174 if not response.strip():
175 self._downloader.report_warning('Got an empty reponse, trying '
176 'adding the "newvideos" parameter')
177 response = self._download_webpage(request_url + '&newvideos=true',
178 playlist_title)
179 response = self._fix_json(response)
180 videos = json.loads(response)
181
182 return {
183 '_type': 'playlist',
184 'title': playlist_title,
185 'id': cat_id,
186 'entries': [self._extract_video(v) for v in videos],
187 }