2 from __future__ 
import unicode_literals
 
   6 from .common 
import InfoExtractor
 
   8     compat_urllib_parse_urlparse
, 
  17 class NFLIE(InfoExtractor
): 
  19     _VALID_URL 
= r
'''(?x)https?:// 
  20         (?P<host>(?:www\.)?(?:nfl\.com|.*?\.clubs\.nfl\.com))/ 
  22         (?P<id>(?:\d[a-z]{2}\d{13}|\w{8}\-(?:\w{4}\-){3}\w{12}))''' 
  25             'url': 'http://www.nfl.com/videos/nfl-game-highlights/0ap3000000398478/Week-3-Redskins-vs-Eagles-highlights', 
  26             'md5': '394ef771ddcd1354f665b471d78ec4c6', 
  28                 'id': '0ap3000000398478', 
  30                 'title': 'Week 3: Redskins vs. Eagles highlights', 
  31                 'description': 'md5:56323bfb0ac4ee5ab24bd05fdf3bf478', 
  32                 'upload_date': '20140921', 
  33                 'timestamp': 1411337580, 
  34                 'thumbnail': 're:^https?://.*\.jpg$', 
  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', 
  41                 'id': '9d72f26a-9e2b-4718-84d3-09fb4046c266', 
  43                 'title': 'LIVE: Post Game vs. Browns', 
  44                 'description': 'md5:6a97f7e5ebeb4c0e69a418a89e0636e8', 
  45                 'upload_date': '20131229', 
  46                 'timestamp': 1388354455, 
  47                 'thumbnail': 're:^https?://.*\.jpg$', 
  53     def prepend_host(host
, url
): 
  54         if not url
.startswith('http'): 
  55             if not url
.startswith('/'): 
  57             url 
= 'http://{0:}{1:}'.format(host
, url
) 
  61     def format_from_stream(stream
, protocol
, host
, path_prefix
='', 
  62                            preference
=0, note
=None): 
  63         url 
= '{protocol:}://{host:}/{prefix:}{path:}'.format( 
  67             path
=stream
.get('path'), 
  71             'vbr': int_or_none(stream
.get('rate', 0), 1000), 
  72             'preference': preference
, 
  76     def _real_extract(self
, url
): 
  77         mobj 
= re
.match(self
._VALID
_URL
, url
) 
  78         video_id
, host 
= mobj
.group('id'), mobj
.group('host') 
  80         webpage 
= self
._download
_webpage
(url
, video_id
) 
  82         config_url 
= NFLIE
.prepend_host(host
, self
._search
_regex
( 
  83             r
'(?:config|configURL)\s*:\s*"([^"]+)"', webpage
, 'config URL')) 
  84         config 
= self
._download
_json
(config_url
, video_id
, 
  85                                      note
='Downloading player config') 
  86         url_template 
= NFLIE
.prepend_host( 
  87             host
, '{contentURLTemplate:}'.format(**config
)) 
  88         video_data 
= self
._download
_json
( 
  89             url_template
.format(id=video_id
), video_id
) 
  92         cdn_data 
= video_data
.get('cdnData', {}) 
  93         streams 
= cdn_data
.get('bitrateInfo', []) 
  94         if cdn_data
.get('format') == 'EXTERNAL_HTTP_STREAM': 
  95             parts 
= compat_urllib_parse_urlparse(cdn_data
.get('uri')) 
  96             protocol
, host 
= parts
.scheme
, parts
.netloc
 
  97             for stream 
in streams
: 
  99                     NFLIE
.format_from_stream(stream
, protocol
, host
)) 
 101             cdns 
= config
.get('cdns') 
 103                 raise ExtractorError('Failed to get CDN data', expected
=True) 
 105             for name
, cdn 
in cdns
.items(): 
 106                 # LimeLight streams don't seem to work 
 107                 if cdn
.get('name') == 'LIMELIGHT': 
 110                 protocol 
= cdn
.get('protocol') 
 111                 host 
= remove_end(cdn
.get('host', ''), '/') 
 112                 if not (protocol 
and host
): 
 115                 prefix 
= cdn
.get('pathprefix', '') 
 116                 if prefix 
and not prefix
.endswith('/'): 
 117                     prefix 
= '%s/' % prefix
 
 120                 if protocol 
== 'rtmp': 
 122                 elif 'prog' in name
.lower(): 
 125                 for stream 
in streams
: 
 127                         NFLIE
.format_from_stream(stream
, protocol
, host
, 
 128                                                  prefix
, preference
, name
)) 
 130         self
._sort
_formats
(formats
) 
 133         for q 
in ('xl', 'l', 'm', 's', 'xs'): 
 134             thumbnail 
= video_data
.get('imagePaths', {}).get(q
) 
 140             'title': video_data
.get('headline'), 
 142             'description': video_data
.get('caption'), 
 143             'duration': video_data
.get('duration'), 
 144             'thumbnail': thumbnail
, 
 145             'timestamp': int_or_none(video_data
.get('posted'), 1000),