1 from __future__ 
import unicode_literals
 
   7 from .common 
import InfoExtractor
 
   8 from .theplatform 
import ThePlatformIE
 
   9 from .adobepass 
import AdobePassIE
 
  10 from ..compat 
import compat_urllib_parse_unquote
 
  19 class NBCIE(AdobePassIE
): 
  20     _VALID_URL 
= r
'https?(?P<permalink>://(?:www\.)?nbc\.com/(?:classic-tv/)?[^/]+/video/[^/]+/(?P<id>n?\d+))' 
  24             'url': 'http://www.nbc.com/the-tonight-show/video/jimmy-fallon-surprises-fans-at-ben-jerrys/2848237', 
  28                 'title': 'Jimmy Fallon Surprises Fans at Ben & Jerry\'s', 
  29                 'description': 'Jimmy gives out free scoops of his new "Tonight Dough" ice cream flavor by surprising customers at the Ben & Jerry\'s scoop shop.', 
  30                 'timestamp': 1424246400, 
  31                 'upload_date': '20150218', 
  32                 'uploader': 'NBCU-COM', 
  36                 'skip_download': True, 
  40             'url': 'http://www.nbc.com/saturday-night-live/video/star-wars-teaser/2832821', 
  44                 'title': 'Star Wars Teaser', 
  45                 'description': 'md5:0b40f9cbde5b671a7ff62fceccc4f442', 
  46                 'timestamp': 1417852800, 
  47                 'upload_date': '20141206', 
  48                 'uploader': 'NBCU-COM', 
  52                 'skip_download': True, 
  54             'skip': 'Only works from US', 
  57             # HLS streams requires the 'hdnea3' cookie 
  58             'url': 'http://www.nbc.com/Kings/video/goliath/n1806', 
  60                 'id': '101528f5a9e8127b107e98c5e6ce4638', 
  63                 'description': 'When an unknown soldier saves the life of the King\'s son in battle, he\'s thrust into the limelight and politics of the kingdom.', 
  64                 'timestamp': 1237100400, 
  65                 'upload_date': '20090315', 
  66                 'uploader': 'NBCU-COM', 
  69                 'skip_download': True, 
  71             'skip': 'Only works from US', 
  74             'url': 'https://www.nbc.com/classic-tv/charles-in-charge/video/charles-in-charge-pilot/n3310', 
  75             'only_matching': True, 
  79             'url': 'https://www.nbc.com/up-all-night/video/day-after-valentine%27s-day/n2189', 
  80             'only_matching': True, 
  84     def _real_extract(self
, url
): 
  85         permalink
, video_id 
= re
.match(self
._VALID
_URL
, url
).groups() 
  86         permalink 
= 'http' + compat_urllib_parse_unquote(permalink
) 
  87         response 
= self
._download
_json
( 
  88             'https://api.nbc.com/v3/videos', video_id
, query
={ 
  89                 'filter[permalink]': permalink
, 
  90                 'fields[videos]': 'description,entitlement,episodeNumber,guid,keywords,seasonNumber,title,vChipRating', 
  91                 'fields[shows]': 'shortTitle', 
  92                 'include': 'show.shortTitle', 
  94         video_data 
= response
['data'][0]['attributes'] 
  99         video_id 
= video_data
['guid'] 
 100         title 
= video_data
['title'] 
 101         if video_data
.get('entitlement') == 'auth': 
 102             resource 
= self
._get
_mvpd
_resource
( 
 103                 'nbcentertainment', title
, video_id
, 
 104                 video_data
.get('vChipRating')) 
 105             query
['auth'] = self
._extract
_mvpd
_auth
( 
 106                 url
, video_id
, 'nbcentertainment', resource
) 
 107         theplatform_url 
= smuggle_url(update_url_query( 
 108             'http://link.theplatform.com/s/NnzsPC/media/guid/2410887629/' + video_id
, 
 109             query
), {'force_smil_url': True}) 
 111             '_type': 'url_transparent', 
 114             'url': theplatform_url
, 
 115             'description': video_data
.get('description'), 
 116             'tags': video_data
.get('keywords'), 
 117             'season_number': int_or_none(video_data
.get('seasonNumber')), 
 118             'episode_number': int_or_none(video_data
.get('episodeNumber')), 
 120             'series': try_get(response
, lambda x
: x
['included'][0]['attributes']['shortTitle']), 
 121             'ie_key': 'ThePlatform', 
 125 class NBCSportsVPlayerIE(InfoExtractor
): 
 126     _VALID_URL 
= r
'https?://vplayer\.nbcsports\.com/(?:[^/]+/)+(?P<id>[0-9a-zA-Z_]+)' 
 129         'url': 'https://vplayer.nbcsports.com/p/BxmELC/nbcsports_embed/select/9CsDKds0kvHI', 
 131             'id': '9CsDKds0kvHI', 
 133             'description': 'md5:df390f70a9ba7c95ff1daace988f0d8d', 
 134             'title': 'Tyler Kalinoski hits buzzer-beater to lift Davidson', 
 135             'timestamp': 1426270238, 
 136             'upload_date': '20150313', 
 137             'uploader': 'NBCU-SPORTS', 
 140         'url': 'https://vplayer.nbcsports.com/p/BxmELC/nbcsports_embed/select/media/_hqLjQ95yx8Z', 
 141         'only_matching': True, 
 145     def _extract_url(webpage
): 
 146         iframe_m 
= re
.search( 
 147             r
'<iframe[^>]+src="(?P<url>https?://vplayer\.nbcsports\.com/[^"]+)"', webpage
) 
 149             return iframe_m
.group('url') 
 151     def _real_extract(self
, url
): 
 152         video_id 
= self
._match
_id
(url
) 
 153         webpage 
= self
._download
_webpage
(url
, video_id
) 
 154         theplatform_url 
= self
._og
_search
_video
_url
(webpage
).replace( 
 155             'vplayer.nbcsports.com', 'player.theplatform.com') 
 156         return self
.url_result(theplatform_url
, 'ThePlatform') 
 159 class NBCSportsIE(InfoExtractor
): 
 160     # Does not include https because its certificate is invalid 
 161     _VALID_URL 
= r
'https?://(?:www\.)?nbcsports\.com//?(?:[^/]+/)+(?P<id>[0-9a-z-]+)' 
 164         'url': 'http://www.nbcsports.com//college-basketball/ncaab/tom-izzo-michigan-st-has-so-much-respect-duke', 
 166             'id': 'PHJSaFWbrTY9', 
 168             'title': 'Tom Izzo, Michigan St. has \'so much respect\' for Duke', 
 169             'description': 'md5:ecb459c9d59e0766ac9c7d5d0eda8113', 
 170             'uploader': 'NBCU-SPORTS', 
 171             'upload_date': '20150330', 
 172             'timestamp': 1427726529, 
 176     def _real_extract(self
, url
): 
 177         video_id 
= self
._match
_id
(url
) 
 178         webpage 
= self
._download
_webpage
(url
, video_id
) 
 179         return self
.url_result( 
 180             NBCSportsVPlayerIE
._extract
_url
(webpage
), 'NBCSportsVPlayer') 
 183 class NBCSportsStreamIE(AdobePassIE
): 
 184     _VALID_URL 
= r
'https?://stream\.nbcsports\.com/.+?\bpid=(?P<id>\d+)' 
 186         'url': 'http://stream.nbcsports.com/nbcsn/generic?pid=206559', 
 190             'title': 'Amgen Tour of California Women\'s Recap', 
 191             'description': 'md5:66520066b3b5281ada7698d0ea2aa894', 
 195             'skip_download': True, 
 197         'skip': 'Requires Adobe Pass Authentication', 
 200     def _real_extract(self
, url
): 
 201         video_id 
= self
._match
_id
(url
) 
 202         live_source 
= self
._download
_json
( 
 203             'http://stream.nbcsports.com/data/live_sources_%s.json' % video_id
, 
 205         video_source 
= live_source
['videoSources'][0] 
 206         title 
= video_source
['title'] 
 208         for k 
in ('source', 'msl4source', 'iossource', 'hlsv4'): 
 210             source_url 
= video_source
.get(sk
) or video_source
.get(sk 
+ 'Alt') 
 214             source_url 
= video_source
['ottStreamUrl'] 
 215         is_live 
= video_source
.get('type') == 'live' or video_source
.get('status') == 'Live' 
 216         resource 
= self
._get
_mvpd
_resource
('nbcsports', title
, video_id
, '') 
 217         token 
= self
._extract
_mvpd
_auth
(url
, video_id
, 'nbcsports', resource
) 
 218         tokenized_url 
= self
._download
_json
( 
 219             'https://token.playmakerservices.com/cdn', 
 220             video_id
, data
=json
.dumps({ 
 221                 'requestorId': 'nbcsports', 
 223                 'application': 'NBCSports', 
 225                 'platform': 'desktop', 
 227                 'url': video_source
['sourceUrl'], 
 228                 'token': base64
.b64encode(token
.encode()).decode(), 
 229                 'resourceId': base64
.b64encode(resource
.encode()).decode(), 
 230             }).encode())['tokenizedUrl'] 
 231         formats 
= self
._extract
_m
3u8_formats
(tokenized_url
, video_id
, 'mp4') 
 232         self
._sort
_formats
(formats
) 
 235             'title': self
._live
_title
(title
) if is_live 
else title
, 
 236             'description': live_source
.get('description'), 
 242 class CSNNEIE(InfoExtractor
): 
 243     _VALID_URL 
= r
'https?://(?:www\.)?csnne\.com/video/(?P<id>[0-9a-z-]+)' 
 246         'url': 'http://www.csnne.com/video/snc-evening-update-wright-named-red-sox-no-5-starter', 
 248             'id': 'yvBLLUgQ8WU0', 
 250             'title': 'SNC evening update: Wright named Red Sox\' No. 5 starter.', 
 251             'description': 'md5:1753cfee40d9352b19b4c9b3e589b9e3', 
 252             'timestamp': 1459369979, 
 253             'upload_date': '20160330', 
 254             'uploader': 'NBCU-SPORTS', 
 258     def _real_extract(self
, url
): 
 259         display_id 
= self
._match
_id
(url
) 
 260         webpage 
= self
._download
_webpage
(url
, display_id
) 
 262             '_type': 'url_transparent', 
 263             'ie_key': 'ThePlatform', 
 264             'url': self
._html
_search
_meta
('twitter:player:stream', webpage
), 
 265             'display_id': display_id
, 
 269 class NBCNewsIE(ThePlatformIE
): 
 270     _VALID_URL 
= r
'(?x)https?://(?:www\.)?(?:nbcnews|today|msnbc)\.com/([^/]+/)*(?:.*-)?(?P<id>[^/?]+)' 
 274             'url': 'http://www.nbcnews.com/watch/nbcnews-com/how-twitter-reacted-to-the-snowden-interview-269389891880', 
 275             'md5': 'af1adfa51312291a017720403826bb64', 
 277                 'id': '269389891880', 
 279                 'title': 'How Twitter Reacted To The Snowden Interview', 
 280                 'description': 'md5:65a0bd5d76fe114f3c2727aa3a81fe64', 
 281                 'uploader': 'NBCU-NEWS', 
 282                 'timestamp': 1401363060, 
 283                 'upload_date': '20140529', 
 287             'url': 'http://www.nbcnews.com/feature/dateline-full-episodes/full-episode-family-business-n285156', 
 288             'md5': 'fdbf39ab73a72df5896b6234ff98518a', 
 290                 'id': '529953347624', 
 292                 'title': 'FULL EPISODE: Family Business', 
 293                 'description': 'md5:757988edbaae9d7be1d585eb5d55cc04', 
 295             'skip': 'This page is unavailable.', 
 298             'url': 'http://www.nbcnews.com/nightly-news/video/nightly-news-with-brian-williams-full-broadcast-february-4-394064451844', 
 299             'md5': '73135a2e0ef819107bbb55a5a9b2a802', 
 301                 'id': '394064451844', 
 303                 'title': 'Nightly News with Brian Williams Full Broadcast (February 4)', 
 304                 'description': 'md5:1c10c1eccbe84a26e5debb4381e2d3c5', 
 305                 'timestamp': 1423104900, 
 306                 'uploader': 'NBCU-NEWS', 
 307                 'upload_date': '20150205', 
 311             'url': 'http://www.nbcnews.com/business/autos/volkswagen-11-million-vehicles-could-have-suspect-software-emissions-scandal-n431456', 
 312             'md5': 'a49e173825e5fcd15c13fc297fced39d', 
 314                 'id': '529953347624', 
 316                 'title': 'Volkswagen U.S. Chief:\xa0 We Have Totally Screwed Up', 
 317                 'description': 'md5:c8be487b2d80ff0594c005add88d8351', 
 318                 'upload_date': '20150922', 
 319                 'timestamp': 1442917800, 
 320                 'uploader': 'NBCU-NEWS', 
 324             'url': 'http://www.today.com/video/see-the-aurora-borealis-from-space-in-stunning-new-nasa-video-669831235788', 
 325             'md5': '118d7ca3f0bea6534f119c68ef539f71', 
 327                 'id': '669831235788', 
 329                 'title': 'See the aurora borealis from space in stunning new NASA video', 
 330                 'description': 'md5:74752b7358afb99939c5f8bb2d1d04b1', 
 331                 'upload_date': '20160420', 
 332                 'timestamp': 1461152093, 
 333                 'uploader': 'NBCU-NEWS', 
 337             'url': 'http://www.msnbc.com/all-in-with-chris-hayes/watch/the-chaotic-gop-immigration-vote-314487875924', 
 338             'md5': '6d236bf4f3dddc226633ce6e2c3f814d', 
 340                 'id': '314487875924', 
 342                 'title': 'The chaotic GOP immigration vote', 
 343                 'description': 'The Republican House votes on a border bill that has no chance of getting through the Senate or signed by the President and is drawing criticism from all sides.', 
 344                 'thumbnail': r
're:^https?://.*\.jpg$', 
 345                 'timestamp': 1406937606, 
 346                 'upload_date': '20140802', 
 347                 'uploader': 'NBCU-NEWS', 
 351             'url': 'http://www.nbcnews.com/watch/dateline/full-episode--deadly-betrayal-386250819952', 
 352             'only_matching': True, 
 355             # From http://www.vulture.com/2016/06/letterman-couldnt-care-less-about-late-night.html 
 356             'url': 'http://www.nbcnews.com/widget/video-embed/701714499682', 
 357             'only_matching': True, 
 361     def _real_extract(self
, url
): 
 362         video_id 
= self
._match
_id
(url
) 
 363         if not video_id
.isdigit(): 
 364             webpage 
= self
._download
_webpage
(url
, video_id
) 
 366             data 
= self
._parse
_json
(self
._search
_regex
( 
 367                 r
'window\.__data\s*=\s*({.+});', webpage
, 
 368                 'bootstrap json'), video_id
) 
 369             video_id 
= data
['article']['content'][0]['primaryMedia']['video']['mpxMetadata']['id'] 
 372             '_type': 'url_transparent', 
 374             # http://feed.theplatform.com/f/2E2eJC/nbcnews also works 
 375             'url': update_url_query('http://feed.theplatform.com/f/2E2eJC/nnd_NBCNews', {'byId': video_id
}), 
 376             'ie_key': 'ThePlatformFeed', 
 380 class NBCOlympicsIE(InfoExtractor
): 
 381     IE_NAME 
= 'nbcolympics' 
 382     _VALID_URL 
= r
'https?://www\.nbcolympics\.com/video/(?P<id>[a-z-]+)' 
 385         # Geo-restricted to US 
 386         'url': 'http://www.nbcolympics.com/video/justin-roses-son-leo-was-tears-after-his-dad-won-gold', 
 387         'md5': '54fecf846d05429fbaa18af557ee523a', 
 389             'id': 'WjTBzDXx5AUq', 
 390             'display_id': 'justin-roses-son-leo-was-tears-after-his-dad-won-gold', 
 392             'title': 'Rose\'s son Leo was in tears after his dad won gold', 
 393             'description': 'Olympic gold medalist Justin Rose gets emotional talking to the impact his win in men\'s golf has already had on his children.', 
 394             'timestamp': 1471274964, 
 395             'upload_date': '20160815', 
 396             'uploader': 'NBCU-SPORTS', 
 400     def _real_extract(self
, url
): 
 401         display_id 
= self
._match
_id
(url
) 
 403         webpage 
= self
._download
_webpage
(url
, display_id
) 
 405         drupal_settings 
= self
._parse
_json
(self
._search
_regex
( 
 406             r
'jQuery\.extend\(Drupal\.settings\s*,\s*({.+?})\);', 
 407             webpage
, 'drupal settings'), display_id
) 
 409         iframe_url 
= drupal_settings
['vod']['iframe_url'] 
 410         theplatform_url 
= iframe_url
.replace( 
 411             'vplayer.nbcolympics.com', 'player.theplatform.com') 
 414             '_type': 'url_transparent', 
 415             'url': theplatform_url
, 
 416             'ie_key': ThePlatformIE
.ie_key(), 
 417             'display_id': display_id
, 
 421 class NBCOlympicsStreamIE(AdobePassIE
): 
 422     IE_NAME 
= 'nbcolympics:stream' 
 423     _VALID_URL 
= r
'https?://stream\.nbcolympics\.com/(?P<id>[0-9a-z-]+)' 
 425         'url': 'http://stream.nbcolympics.com/2018-winter-olympics-nbcsn-evening-feb-8', 
 429             'title': 're:Curling, Alpine, Luge [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$', 
 433             'skip_download': True, 
 436     _DATA_URL_TEMPLATE 
= 'http://stream.nbcolympics.com/data/%s_%s.json' 
 438     def _real_extract(self
, url
): 
 439         display_id 
= self
._match
_id
(url
) 
 440         webpage 
= self
._download
_webpage
(url
, display_id
) 
 441         pid 
= self
._search
_regex
(r
'pid\s*=\s*(\d+);', webpage
, 'pid') 
 442         resource 
= self
._search
_regex
( 
 443             r
"resource\s*=\s*'(.+)';", webpage
, 
 444             'resource').replace("' + pid + '", pid
) 
 445         event_config 
= self
._download
_json
( 
 446             self
._DATA
_URL
_TEMPLATE 
% ('event_config', pid
), 
 448         title 
= self
._live
_title
(event_config
['eventTitle']) 
 449         source_url 
= self
._download
_json
( 
 450             self
._DATA
_URL
_TEMPLATE 
% ('live_sources', pid
), 
 451             pid
)['videoSources'][0]['sourceUrl'] 
 452         media_token 
= self
._extract
_mvpd
_auth
( 
 453             url
, pid
, event_config
.get('requestorId', 'NBCOlympics'), resource
) 
 454         formats 
= self
._extract
_m
3u8_formats
(self
._download
_webpage
( 
 455             'http://sp.auth.adobe.com/tvs/v1/sign', pid
, query
={ 
 457                 'mediaToken': base64
.b64encode(media_token
.encode()), 
 458                 'resource': base64
.b64encode(resource
.encode()), 
 461         self
._sort
_formats
(formats
) 
 465             'display_id': display_id
,