]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/nhl.py
Imported Upstream version 2013.10.23
[youtubedl] / youtube_dl / extractor / nhl.py
1 import re
2 import json
3 import xml.etree.ElementTree
4
5 from .common import InfoExtractor
6 from ..utils import (
7 compat_urlparse,
8 compat_urllib_parse,
9 determine_ext,
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 data = compat_urllib_parse.urlencode({
25 'type': 'fvod',
26 'path': initial_video_url.replace('.mp4', '_sd.mp4'),
27 })
28 path_url = 'http://video.nhl.com/videocenter/servlets/encryptvideopath?' + data
29 path_response = self._download_webpage(path_url, video_id,
30 u'Downloading final video url')
31 path_doc = xml.etree.ElementTree.fromstring(path_response)
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 = u'nhl.com'
49 _VALID_URL = r'https?://video(?P<team>\.[^.]*)?\.nhl\.com/videocenter/console\?.*?(?<=[?&])id=(?P<id>\d+)'
50
51 _TEST = {
52 u'url': u'http://video.canucks.nhl.com/videocenter/console?catid=6?id=453614',
53 u'file': u'453614.mp4',
54 u'info_dict': {
55 u'title': u'Quick clip: Weise 4-3 goal vs Flames',
56 u'description': u'Dale Weise scores his first of the season to put the Canucks up 4-3.',
57 u'duration': 18,
58 u'upload_date': u'20131006',
59 },
60 }
61
62 def _real_extract(self, url):
63 mobj = re.match(self._VALID_URL, url)
64 video_id = mobj.group('id')
65 json_url = 'http://video.nhl.com/videocenter/servlets/playlist?ids=%s&format=json' % video_id
66 info_json = self._download_webpage(json_url, video_id,
67 u'Downloading info json')
68 info_json = self._fix_json(info_json)
69 info = json.loads(info_json)[0]
70 return self._extract_video(info)
71
72
73 class NHLVideocenterIE(NHLBaseInfoExtractor):
74 IE_NAME = u'nhl.com:videocenter'
75 IE_DESC = u'Download the first 12 videos from a videocenter category'
76 _VALID_URL = r'https?://video\.(?P<team>[^.]*)\.nhl\.com/videocenter/(console\?.*?catid=(?P<catid>[^&]+))?'
77
78 @classmethod
79 def suitable(cls, url):
80 if NHLIE.suitable(url):
81 return False
82 return super(NHLVideocenterIE, cls).suitable(url)
83
84 def _real_extract(self, url):
85 mobj = re.match(self._VALID_URL, url)
86 team = mobj.group('team')
87 webpage = self._download_webpage(url, team)
88 cat_id = self._search_regex(
89 [r'var defaultCatId = "(.+?)";',
90 r'{statusIndex:0,index:0,.*?id:(.*?),'],
91 webpage, u'category id')
92 playlist_title = self._html_search_regex(
93 r'tab0"[^>]*?>(.*?)</td>',
94 webpage, u'playlist title', flags=re.DOTALL).lower().capitalize()
95
96 data = compat_urllib_parse.urlencode({
97 'cid': cat_id,
98 # This is the default value
99 'count': 12,
100 'ptrs': 3,
101 'format': 'json',
102 })
103 path = '/videocenter/servlets/browse?' + data
104 request_url = compat_urlparse.urljoin(url, path)
105 response = self._download_webpage(request_url, playlist_title)
106 response = self._fix_json(response)
107 if not response.strip():
108 self._downloader.report_warning(u'Got an empty reponse, trying '
109 u'adding the "newvideos" parameter')
110 response = self._download_webpage(request_url + '&newvideos=true',
111 playlist_title)
112 response = self._fix_json(response)
113 videos = json.loads(response)
114
115 return {
116 '_type': 'playlist',
117 'title': playlist_title,
118 'id': cat_id,
119 'entries': [self._extract_video(i) for i in videos],
120 }