]> jfr.im git - yt-dlp.git/blob - youtube_dl/extractor/nhl.py
[youtube] Modernize
[yt-dlp.git] / youtube_dl / extractor / nhl.py
1 from __future__ import unicode_literals
2
3 import re
4 import json
5
6 from .common import InfoExtractor
7 from ..utils import (
8 compat_urlparse,
9 compat_urllib_parse,
10 determine_ext,
11 unified_strdate,
12 )
13
14
15 class NHLBaseInfoExtractor(InfoExtractor):
16 @staticmethod
17 def _fix_json(json_string):
18 return json_string.replace('\\\'', '\'')
19
20 def _extract_video(self, info):
21 video_id = info['id']
22 self.report_extraction(video_id)
23
24 initial_video_url = info['publishPoint']
25 data = compat_urllib_parse.urlencode({
26 'type': 'fvod',
27 'path': initial_video_url.replace('.mp4', '_sd.mp4'),
28 })
29 path_url = 'http://video.nhl.com/videocenter/servlets/encryptvideopath?' + data
30 path_doc = self._download_xml(
31 path_url, video_id, 'Downloading final video url')
32 video_url = path_doc.find('path').text
33
34 join = compat_urlparse.urljoin
35 return {
36 'id': video_id,
37 'title': info['name'],
38 'url': video_url,
39 'ext': determine_ext(video_url),
40 'description': info['description'],
41 'duration': int(info['duration']),
42 'thumbnail': join(join(video_url, '/u/'), info['bigImage']),
43 'upload_date': unified_strdate(info['releaseDate'].split('.')[0]),
44 }
45
46
47 class NHLIE(NHLBaseInfoExtractor):
48 IE_NAME = 'nhl.com'
49 _VALID_URL = r'https?://video(?P<team>\.[^.]*)?\.nhl\.com/videocenter/console(?:\?(?:.*?[?&])?)id=(?P<id>[0-9]+)'
50
51 _TESTS = [{
52 'url': 'http://video.canucks.nhl.com/videocenter/console?catid=6?id=453614',
53 'info_dict': {
54 'id': '453614',
55 'ext': 'mp4',
56 'title': 'Quick clip: Weise 4-3 goal vs Flames',
57 'description': 'Dale Weise scores his first of the season to put the Canucks up 4-3.',
58 'duration': 18,
59 'upload_date': '20131006',
60 },
61 }, {
62 'url': 'http://video.flames.nhl.com/videocenter/console?id=630616',
63 'only_matching': True,
64 }]
65
66 def _real_extract(self, url):
67 mobj = re.match(self._VALID_URL, url)
68 video_id = mobj.group('id')
69 json_url = 'http://video.nhl.com/videocenter/servlets/playlist?ids=%s&format=json' % video_id
70 data = self._download_json(
71 json_url, video_id, transform_source=self._fix_json)
72 return self._extract_video(data[0])
73
74
75 class NHLVideocenterIE(NHLBaseInfoExtractor):
76 IE_NAME = 'nhl.com:videocenter'
77 IE_DESC = 'NHL videocenter category'
78 _VALID_URL = r'https?://video\.(?P<team>[^.]*)\.nhl\.com/videocenter/(console\?.*?catid=(?P<catid>[0-9]+)(?![&?]id=).*?)?$'
79 _TEST = {
80 'url': 'http://video.canucks.nhl.com/videocenter/console?catid=999',
81 'info_dict': {
82 'id': '999',
83 'title': 'Highlights',
84 },
85 'playlist_count': 12,
86 }
87
88 def _real_extract(self, url):
89 mobj = re.match(self._VALID_URL, url)
90 team = mobj.group('team')
91 webpage = self._download_webpage(url, team)
92 cat_id = self._search_regex(
93 [r'var defaultCatId = "(.+?)";',
94 r'{statusIndex:0,index:0,.*?id:(.*?),'],
95 webpage, 'category id')
96 playlist_title = self._html_search_regex(
97 r'tab0"[^>]*?>(.*?)</td>',
98 webpage, 'playlist title', flags=re.DOTALL).lower().capitalize()
99
100 data = compat_urllib_parse.urlencode({
101 'cid': cat_id,
102 # This is the default value
103 'count': 12,
104 'ptrs': 3,
105 'format': 'json',
106 })
107 path = '/videocenter/servlets/browse?' + data
108 request_url = compat_urlparse.urljoin(url, path)
109 response = self._download_webpage(request_url, playlist_title)
110 response = self._fix_json(response)
111 if not response.strip():
112 self._downloader.report_warning(u'Got an empty reponse, trying '
113 'adding the "newvideos" parameter')
114 response = self._download_webpage(request_url + '&newvideos=true',
115 playlist_title)
116 response = self._fix_json(response)
117 videos = json.loads(response)
118
119 return {
120 '_type': 'playlist',
121 'title': playlist_title,
122 'id': cat_id,
123 'entries': [self._extract_video(v) for v in videos],
124 }