]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/nhl.py
debian/changelog: Highlight the fix of youtube videos.
[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 _extract_video(self, info):
24 video_id = info['id']
25 self.report_extraction(video_id)
26
27 initial_video_url = info['publishPoint']
28 if info['formats'] == '1':
29 parsed_url = compat_urllib_parse_urlparse(initial_video_url)
30 filename, ext = os.path.splitext(parsed_url.path)
31 path = '%s_sd%s' % (filename, ext)
32 data = compat_urllib_parse.urlencode({
33 'type': 'fvod',
34 'path': compat_urlparse.urlunparse(parsed_url[:2] + (path,) + parsed_url[3:])
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:
41 video_url = initial_video_url
42
43 join = compat_urlparse.urljoin
44 return {
45 'id': video_id,
46 'title': info['name'],
47 'url': video_url,
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
55 class NHLIE(NHLBaseInfoExtractor):
56 IE_NAME = 'nhl.com'
57 _VALID_URL = r'https?://video(?P<team>\.[^.]*)?\.nhl\.com/videocenter/console(?:\?(?:.*?[?&])?)id=(?P<id>[-0-9a-zA-Z]+)'
58
59 _TESTS = [{
60 'url': 'http://video.canucks.nhl.com/videocenter/console?catid=6?id=453614',
61 'md5': 'db704a4ea09e8d3988c85e36cc892d09',
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',
69 },
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 },
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 },
92 }, {
93 'url': 'http://video.flames.nhl.com/videocenter/console?id=630616',
94 'only_matching': True,
95 }]
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
101 data = self._download_json(
102 json_url, video_id, transform_source=self._fix_json)
103 return self._extract_video(data[0])
104
105
106 class NHLVideocenterIE(NHLBaseInfoExtractor):
107 IE_NAME = 'nhl.com:videocenter'
108 IE_DESC = 'NHL videocenter category'
109 _VALID_URL = r'https?://video\.(?P<team>[^.]*)\.nhl\.com/videocenter/(console\?[^(id=)]*catid=(?P<catid>[0-9]+)(?![&?]id=).*?)?$'
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 }
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:(.*?),'],
126 webpage, 'category id')
127 playlist_title = self._html_search_regex(
128 r'tab0"[^>]*?>(.*?)</td>',
129 webpage, 'playlist title', flags=re.DOTALL).lower().capitalize()
130
131 data = compat_urllib_parse.urlencode({
132 'cid': cat_id,
133 # This is the default value
134 'count': 12,
135 'ptrs': 3,
136 'format': 'json',
137 })
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():
143 self._downloader.report_warning('Got an empty reponse, trying '
144 'adding the "newvideos" parameter')
145 response = self._download_webpage(request_url + '&newvideos=true',
146 playlist_title)
147 response = self._fix_json(response)
148 videos = json.loads(response)
149
150 return {
151 '_type': 'playlist',
152 'title': playlist_title,
153 'id': cat_id,
154 'entries': [self._extract_video(v) for v in videos],
155 }