]> git.bitcoin.ninja Git - youtube-dl/blob - youtube_dl/extractor/nfl.py
Merge remote-tracking branch 'yan12125/download-dash-segments' (#5886)
[youtube-dl] / 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 ..compat import (
8     compat_urllib_parse_urlparse,
9 )
10 from ..utils import (
11     ExtractorError,
12     int_or_none,
13     remove_end,
14 )
15
16
17 class NFLIE(InfoExtractor):
18     IE_NAME = 'nfl.com'
19     _VALID_URL = r'''(?x)https?://
20         (?P<host>(?:www\.)?(?:nfl\.com|.*?\.clubs\.nfl\.com))/
21         (?:.+?/)*
22         (?P<id>(?:[a-z0-9]{16}|\w{8}\-(?:\w{4}\-){3}\w{12}))'''
23     _TESTS = [
24         {
25             'url': 'http://www.nfl.com/videos/nfl-game-highlights/0ap3000000398478/Week-3-Redskins-vs-Eagles-highlights',
26             'md5': '394ef771ddcd1354f665b471d78ec4c6',
27             'info_dict': {
28                 'id': '0ap3000000398478',
29                 'ext': 'mp4',
30                 'title': 'Week 3: Redskins vs. Eagles highlights',
31                 'description': 'md5:56323bfb0ac4ee5ab24bd05fdf3bf478',
32                 'upload_date': '20140921',
33                 'timestamp': 1411337580,
34                 'thumbnail': 're:^https?://.*\.jpg$',
35             }
36         },
37         {
38             'url': 'http://prod.www.steelers.clubs.nfl.com/video-and-audio/videos/LIVE_Post_Game_vs_Browns/9d72f26a-9e2b-4718-84d3-09fb4046c266',
39             'md5': 'cf85bdb4bc49f6e9d3816d130c78279c',
40             'info_dict': {
41                 'id': '9d72f26a-9e2b-4718-84d3-09fb4046c266',
42                 'ext': 'mp4',
43                 'title': 'LIVE: Post Game vs. Browns',
44                 'description': 'md5:6a97f7e5ebeb4c0e69a418a89e0636e8',
45                 'upload_date': '20131229',
46                 'timestamp': 1388354455,
47                 'thumbnail': 're:^https?://.*\.jpg$',
48             }
49         },
50         {
51             'url': 'http://www.nfl.com/news/story/0ap3000000467586/article/patriots-seahawks-involved-in-lategame-skirmish',
52             'info_dict': {
53                 'id': '0ap3000000467607',
54                 'ext': 'mp4',
55                 'title': 'Frustrations flare on the field',
56                 'description': 'Emotions ran high at the end of the Super Bowl on both sides of the ball after a dramatic finish.',
57                 'timestamp': 1422850320,
58                 'upload_date': '20150202',
59             },
60         },
61         {
62             'url': 'http://www.nfl.com/videos/nfl-network-top-ten/09000d5d810a6bd4/Top-10-Gutsiest-Performances-Jack-Youngblood',
63             'only_matching': True,
64         }
65     ]
66
67     @staticmethod
68     def prepend_host(host, url):
69         if not url.startswith('http'):
70             if not url.startswith('/'):
71                 url = '/%s' % url
72             url = 'http://{0:}{1:}'.format(host, url)
73         return url
74
75     @staticmethod
76     def format_from_stream(stream, protocol, host, path_prefix='',
77                            preference=0, note=None):
78         url = '{protocol:}://{host:}/{prefix:}{path:}'.format(
79             protocol=protocol,
80             host=host,
81             prefix=path_prefix,
82             path=stream.get('path'),
83         )
84         return {
85             'url': url,
86             'vbr': int_or_none(stream.get('rate', 0), 1000),
87             'preference': preference,
88             'format_note': note,
89         }
90
91     def _real_extract(self, url):
92         mobj = re.match(self._VALID_URL, url)
93         video_id, host = mobj.group('id'), mobj.group('host')
94
95         webpage = self._download_webpage(url, video_id)
96
97         config_url = NFLIE.prepend_host(host, self._search_regex(
98             r'(?:config|configURL)\s*:\s*"([^"]+)"', webpage, 'config URL',
99             default='static/content/static/config/video/config.json'))
100         # For articles, the id in the url is not the video id
101         video_id = self._search_regex(
102             r'contentId\s*:\s*"([^"]+)"', webpage, 'video id', default=video_id)
103         config = self._download_json(config_url, video_id,
104                                      note='Downloading player config')
105         url_template = NFLIE.prepend_host(
106             host, '{contentURLTemplate:}'.format(**config))
107         video_data = self._download_json(
108             url_template.format(id=video_id), video_id)
109
110         formats = []
111         cdn_data = video_data.get('cdnData', {})
112         streams = cdn_data.get('bitrateInfo', [])
113         if cdn_data.get('format') == 'EXTERNAL_HTTP_STREAM':
114             parts = compat_urllib_parse_urlparse(cdn_data.get('uri'))
115             protocol, host = parts.scheme, parts.netloc
116             for stream in streams:
117                 formats.append(
118                     NFLIE.format_from_stream(stream, protocol, host))
119         else:
120             cdns = config.get('cdns')
121             if not cdns:
122                 raise ExtractorError('Failed to get CDN data', expected=True)
123
124             for name, cdn in cdns.items():
125                 # LimeLight streams don't seem to work
126                 if cdn.get('name') == 'LIMELIGHT':
127                     continue
128
129                 protocol = cdn.get('protocol')
130                 host = remove_end(cdn.get('host', ''), '/')
131                 if not (protocol and host):
132                     continue
133
134                 prefix = cdn.get('pathprefix', '')
135                 if prefix and not prefix.endswith('/'):
136                     prefix = '%s/' % prefix
137
138                 preference = 0
139                 if protocol == 'rtmp':
140                     preference = -2
141                 elif 'prog' in name.lower():
142                     preference = 1
143
144                 for stream in streams:
145                     formats.append(
146                         NFLIE.format_from_stream(stream, protocol, host,
147                                                  prefix, preference, name))
148
149         self._sort_formats(formats)
150
151         thumbnail = None
152         for q in ('xl', 'l', 'm', 's', 'xs'):
153             thumbnail = video_data.get('imagePaths', {}).get(q)
154             if thumbnail:
155                 break
156
157         return {
158             'id': video_id,
159             'title': video_data.get('headline'),
160             'formats': formats,
161             'description': video_data.get('caption'),
162             'duration': video_data.get('duration'),
163             'thumbnail': thumbnail,
164             'timestamp': int_or_none(video_data.get('posted'), 1000),
165         }