]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/nfl.py
cc7c921c364d64ee504fa6d31265d13a96565e8d
[youtubedl] / youtube_dl / extractor / nfl.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .common import InfoExtractor
7 from ..utils import (
8 ExtractorError,
9 compat_urllib_parse_urlparse,
10 int_or_none,
11 remove_end,
12 )
13
14
15 class NFLIE(InfoExtractor):
16 IE_NAME = 'nfl.com'
17 _VALID_URL = r'''(?x)https?://
18 (?P<host>(?:www\.)?(?:nfl\.com|.*?\.clubs\.nfl\.com))/
19 (?:.+?/)*
20 (?P<id>(?:\d[a-z]{2}\d{13}|\w{8}\-(?:\w{4}\-){3}\w{12}))'''
21 _TESTS = [
22 {
23 'url': 'http://www.nfl.com/videos/nfl-game-highlights/0ap3000000398478/Week-3-Redskins-vs-Eagles-highlights',
24 'md5': '394ef771ddcd1354f665b471d78ec4c6',
25 'info_dict': {
26 'id': '0ap3000000398478',
27 'ext': 'mp4',
28 'title': 'Week 3: Redskins vs. Eagles highlights',
29 'description': 'md5:56323bfb0ac4ee5ab24bd05fdf3bf478',
30 'upload_date': '20140921',
31 'timestamp': 1411337580,
32 'thumbnail': 're:^https?://.*\.jpg$',
33 }
34 },
35 {
36 'url': 'http://prod.www.steelers.clubs.nfl.com/video-and-audio/videos/LIVE_Post_Game_vs_Browns/9d72f26a-9e2b-4718-84d3-09fb4046c266',
37 'md5': 'cf85bdb4bc49f6e9d3816d130c78279c',
38 'info_dict': {
39 'id': '9d72f26a-9e2b-4718-84d3-09fb4046c266',
40 'ext': 'mp4',
41 'title': 'LIVE: Post Game vs. Browns',
42 'description': 'md5:6a97f7e5ebeb4c0e69a418a89e0636e8',
43 'upload_date': '20131229',
44 'timestamp': 1388354455,
45 'thumbnail': 're:^https?://.*\.jpg$',
46 }
47 }
48 ]
49
50 @staticmethod
51 def prepend_host(host, url):
52 if not url.startswith('http'):
53 if not url.startswith('/'):
54 url = '/%s' % url
55 url = 'http://{0:}{1:}'.format(host, url)
56 return url
57
58 @staticmethod
59 def format_from_stream(stream, protocol, host, path_prefix='',
60 preference=0, note=None):
61 url = '{protocol:}://{host:}/{prefix:}{path:}'.format(
62 protocol=protocol,
63 host=host,
64 prefix=path_prefix,
65 path=stream.get('path'),
66 )
67 return {
68 'url': url,
69 'vbr': int_or_none(stream.get('rate', 0), 1000),
70 'preference': preference,
71 'format_note': note,
72 }
73
74 def _real_extract(self, url):
75 mobj = re.match(self._VALID_URL, url)
76 video_id, host = mobj.group('id'), mobj.group('host')
77
78 webpage = self._download_webpage(url, video_id)
79
80 config_url = NFLIE.prepend_host(host, self._search_regex(
81 r'(?:config|configURL)\s*:\s*"([^"]+)"', webpage, 'config URL'))
82 config = self._download_json(config_url, video_id,
83 note='Downloading player config')
84 url_template = NFLIE.prepend_host(
85 host, '{contentURLTemplate:}'.format(**config))
86 video_data = self._download_json(
87 url_template.format(id=video_id), video_id)
88
89 formats = []
90 cdn_data = video_data.get('cdnData', {})
91 streams = cdn_data.get('bitrateInfo', [])
92 if cdn_data.get('format') == 'EXTERNAL_HTTP_STREAM':
93 parts = compat_urllib_parse_urlparse(cdn_data.get('uri'))
94 protocol, host = parts.scheme, parts.netloc
95 for stream in streams:
96 formats.append(
97 NFLIE.format_from_stream(stream, protocol, host))
98 else:
99 cdns = config.get('cdns')
100 if not cdns:
101 raise ExtractorError('Failed to get CDN data', expected=True)
102
103 for name, cdn in cdns.items():
104 # LimeLight streams don't seem to work
105 if cdn.get('name') == 'LIMELIGHT':
106 continue
107
108 protocol = cdn.get('protocol')
109 host = remove_end(cdn.get('host', ''), '/')
110 if not (protocol and host):
111 continue
112
113 prefix = cdn.get('pathprefix', '')
114 if prefix and not prefix.endswith('/'):
115 prefix = '%s/' % prefix
116
117 preference = 0
118 if protocol == 'rtmp':
119 preference = -2
120 elif 'prog' in name.lower():
121 preference = 1
122
123 for stream in streams:
124 formats.append(
125 NFLIE.format_from_stream(stream, protocol, host,
126 prefix, preference, name))
127
128 self._sort_formats(formats)
129
130 thumbnail = None
131 for q in ('xl', 'l', 'm', 's', 'xs'):
132 thumbnail = video_data.get('imagePaths', {}).get(q)
133 if thumbnail:
134 break
135
136 return {
137 'id': video_id,
138 'title': video_data.get('headline'),
139 'formats': formats,
140 'description': video_data.get('caption'),
141 'duration': video_data.get('duration'),
142 'thumbnail': thumbnail,
143 'timestamp': int_or_none(video_data.get('posted'), 1000),
144 }