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>(?:[a-z0-9]{16}|\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$', 
  51             'url': 'http://www.nfl.com/news/story/0ap3000000467586/article/patriots-seahawks-involved-in-lategame-skirmish', 
  53                 'id': '0ap3000000467607', 
  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', 
  62             'url': 'http://www.nfl.com/videos/nfl-network-top-ten/09000d5d810a6bd4/Top-10-Gutsiest-Performances-Jack-Youngblood', 
  63             'only_matching': True, 
  68     def prepend_host(host
, url
): 
  69         if not url
.startswith('http'): 
  70             if not url
.startswith('/'): 
  72             url 
= 'http://{0:}{1:}'.format(host
, url
) 
  76     def format_from_stream(stream
, protocol
, host
, path_prefix
='', 
  77                            preference
=0, note
=None): 
  78         url 
= '{protocol:}://{host:}/{prefix:}{path:}'.format( 
  82             path
=stream
.get('path'), 
  86             'vbr': int_or_none(stream
.get('rate', 0), 1000), 
  87             'preference': preference
, 
  91     def _real_extract(self
, url
): 
  92         mobj 
= re
.match(self
._VALID
_URL
, url
) 
  93         video_id
, host 
= mobj
.group('id'), mobj
.group('host') 
  95         webpage 
= self
._download
_webpage
(url
, video_id
) 
  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
) 
 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
: 
 118                     NFLIE
.format_from_stream(stream
, protocol
, host
)) 
 120             cdns 
= config
.get('cdns') 
 122                 raise ExtractorError('Failed to get CDN data', expected
=True) 
 124             for name
, cdn 
in cdns
.items(): 
 125                 # LimeLight streams don't seem to work 
 126                 if cdn
.get('name') == 'LIMELIGHT': 
 129                 protocol 
= cdn
.get('protocol') 
 130                 host 
= remove_end(cdn
.get('host', ''), '/') 
 131                 if not (protocol 
and host
): 
 134                 prefix 
= cdn
.get('pathprefix', '') 
 135                 if prefix 
and not prefix
.endswith('/'): 
 136                     prefix 
= '%s/' % prefix
 
 139                 if protocol 
== 'rtmp': 
 141                 elif 'prog' in name
.lower(): 
 144                 for stream 
in streams
: 
 146                         NFLIE
.format_from_stream(stream
, protocol
, host
, 
 147                                                  prefix
, preference
, name
)) 
 149         self
._sort
_formats
(formats
) 
 152         for q 
in ('xl', 'l', 'm', 's', 'xs'): 
 153             thumbnail 
= video_data
.get('imagePaths', {}).get(q
) 
 159             'title': video_data
.get('headline'), 
 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),