PEP8: applied even more rules
[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     unified_strdate,
11 )
12
13
14 class NHLBaseInfoExtractor(InfoExtractor):
15     @staticmethod
16     def _fix_json(json_string):
17         return json_string.replace('\\\'', '\'')
18
19     def _extract_video(self, info):
20         video_id = info['id']
21         self.report_extraction(video_id)
22
23         initial_video_url = info['publishPoint']
24         if info['formats'] == '1':
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         else:
34             video_url = initial_video_url
35
36         join = compat_urlparse.urljoin
37         return {
38             'id': video_id,
39             'title': info['name'],
40             'url': video_url,
41             'description': info['description'],
42             'duration': int(info['duration']),
43             'thumbnail': join(join(video_url, '/u/'), info['bigImage']),
44             'upload_date': unified_strdate(info['releaseDate'].split('.')[0]),
45         }
46
47
48 class NHLIE(NHLBaseInfoExtractor):
49     IE_NAME = 'nhl.com'
50     _VALID_URL = r'https?://video(?P<team>\.[^.]*)?\.nhl\.com/videocenter/console(?:\?(?:.*?[?&])?)id=(?P<id>[0-9a-z-]+)'
51
52     _TESTS = [{
53         'url': 'http://video.canucks.nhl.com/videocenter/console?catid=6?id=453614',
54         'md5': 'db704a4ea09e8d3988c85e36cc892d09',
55         'info_dict': {
56             'id': '453614',
57             'ext': 'mp4',
58             'title': 'Quick clip: Weise 4-3 goal vs Flames',
59             'description': 'Dale Weise scores his first of the season to put the Canucks up 4-3.',
60             'duration': 18,
61             'upload_date': '20131006',
62         },
63     }, {
64         'url': 'http://video.nhl.com/videocenter/console?id=2014020024-628-h',
65         'md5': 'd22e82bc592f52d37d24b03531ee9696',
66         'info_dict': {
67             'id': '2014020024-628-h',
68             'ext': 'mp4',
69             'title': 'Alex Galchenyuk Goal on Ray Emery (14:40/3rd)',
70             'description': 'Home broadcast - Montreal Canadiens at Philadelphia Flyers - October 11, 2014',
71             'duration': 0,
72             'upload_date': '20141011',
73         },
74     }, {
75         'url': 'http://video.flames.nhl.com/videocenter/console?id=630616',
76         'only_matching': True,
77     }]
78
79     def _real_extract(self, url):
80         mobj = re.match(self._VALID_URL, url)
81         video_id = mobj.group('id')
82         json_url = 'http://video.nhl.com/videocenter/servlets/playlist?ids=%s&format=json' % video_id
83         data = self._download_json(
84             json_url, video_id, transform_source=self._fix_json)
85         return self._extract_video(data[0])
86
87
88 class NHLVideocenterIE(NHLBaseInfoExtractor):
89     IE_NAME = 'nhl.com:videocenter'
90     IE_DESC = 'NHL videocenter category'
91     _VALID_URL = r'https?://video\.(?P<team>[^.]*)\.nhl\.com/videocenter/(console\?.*?catid=(?P<catid>[0-9]+)(?![&?]id=).*?)?$'
92     _TEST = {
93         'url': 'http://video.canucks.nhl.com/videocenter/console?catid=999',
94         'info_dict': {
95             'id': '999',
96             'title': 'Highlights',
97         },
98         'playlist_count': 12,
99     }
100
101     def _real_extract(self, url):
102         mobj = re.match(self._VALID_URL, url)
103         team = mobj.group('team')
104         webpage = self._download_webpage(url, team)
105         cat_id = self._search_regex(
106             [r'var defaultCatId = "(.+?)";',
107              r'{statusIndex:0,index:0,.*?id:(.*?),'],
108             webpage, 'category id')
109         playlist_title = self._html_search_regex(
110             r'tab0"[^>]*?>(.*?)</td>',
111             webpage, 'playlist title', flags=re.DOTALL).lower().capitalize()
112
113         data = compat_urllib_parse.urlencode({
114             'cid': cat_id,
115             # This is the default value
116             'count': 12,
117             'ptrs': 3,
118             'format': 'json',
119         })
120         path = '/videocenter/servlets/browse?' + data
121         request_url = compat_urlparse.urljoin(url, path)
122         response = self._download_webpage(request_url, playlist_title)
123         response = self._fix_json(response)
124         if not response.strip():
125             self._downloader.report_warning(u'Got an empty reponse, trying '
126                                             'adding the "newvideos" parameter')
127             response = self._download_webpage(request_url + '&newvideos=true',
128                                               playlist_title)
129             response = self._fix_json(response)
130         videos = json.loads(response)
131
132         return {
133             '_type': 'playlist',
134             'title': playlist_title,
135             'id': cat_id,
136             'entries': [self._extract_video(v) for v in videos],
137         }