Merge branch 'vgtv' of https://github.com/mrkolby/youtube-dl into mrkolby-vgtv
[youtube-dl] / 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     _TEST = {
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
63     def _real_extract(self, url):
64         mobj = re.match(self._VALID_URL, url)
65         video_id = mobj.group('id')
66         json_url = 'http://video.nhl.com/videocenter/servlets/playlist?ids=%s&format=json' % video_id
67         data = self._download_json(
68             json_url, video_id, transform_source=self._fix_json)
69         return self._extract_video(data[0])
70
71
72 class NHLVideocenterIE(NHLBaseInfoExtractor):
73     IE_NAME = 'nhl.com:videocenter'
74     IE_DESC = 'NHL videocenter category'
75     _VALID_URL = r'https?://video\.(?P<team>[^.]*)\.nhl\.com/videocenter/(console\?.*?catid=(?P<catid>[0-9]+)(?![&?]id=).*?)?$'
76     _TEST = {
77         'url': 'http://video.canucks.nhl.com/videocenter/console?catid=999',
78         'info_dict': {
79             'id': '999',
80             'title': 'Highlights',
81         },
82         'playlist_count': 12,
83     }
84
85     def _real_extract(self, url):
86         mobj = re.match(self._VALID_URL, url)
87         team = mobj.group('team')
88         webpage = self._download_webpage(url, team)
89         cat_id = self._search_regex(
90             [r'var defaultCatId = "(.+?)";',
91              r'{statusIndex:0,index:0,.*?id:(.*?),'],
92             webpage, 'category id')
93         playlist_title = self._html_search_regex(
94             r'tab0"[^>]*?>(.*?)</td>',
95             webpage, 'playlist title', flags=re.DOTALL).lower().capitalize()
96
97         data = compat_urllib_parse.urlencode({
98             'cid': cat_id,
99             # This is the default value
100             'count': 12,
101             'ptrs': 3,
102             'format': 'json',
103         })
104         path = '/videocenter/servlets/browse?' + data
105         request_url = compat_urlparse.urljoin(url, path)
106         response = self._download_webpage(request_url, playlist_title)
107         response = self._fix_json(response)
108         if not response.strip():
109             self._downloader.report_warning(u'Got an empty reponse, trying '
110                                             'adding the "newvideos" parameter')
111             response = self._download_webpage(request_url + '&newvideos=true',
112                 playlist_title)
113             response = self._fix_json(response)
114         videos = json.loads(response)
115
116         return {
117             '_type': 'playlist',
118             'title': playlist_title,
119             'id': cat_id,
120             'entries': [self._extract_video(v) for v in videos],
121         }