nbc.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. from __future__ import unicode_literals
  2. import base64
  3. import json
  4. import re
  5. from .common import InfoExtractor
  6. from .theplatform import ThePlatformIE
  7. from .adobepass import AdobePassIE
  8. from ..compat import compat_urllib_parse_unquote
  9. from ..utils import (
  10. int_or_none,
  11. js_to_json,
  12. parse_duration,
  13. smuggle_url,
  14. try_get,
  15. unified_timestamp,
  16. update_url_query,
  17. )
  18. class NBCIE(AdobePassIE):
  19. _VALID_URL = r'https?(?P<permalink>://(?:www\.)?nbc\.com/(?:classic-tv/)?[^/]+/video/[^/]+/(?P<id>n?\d+))'
  20. _TESTS = [
  21. {
  22. 'url': 'http://www.nbc.com/the-tonight-show/video/jimmy-fallon-surprises-fans-at-ben-jerrys/2848237',
  23. 'info_dict': {
  24. 'id': '2848237',
  25. 'ext': 'mp4',
  26. 'title': 'Jimmy Fallon Surprises Fans at Ben & Jerry\'s',
  27. 'description': 'Jimmy gives out free scoops of his new "Tonight Dough" ice cream flavor by surprising customers at the Ben & Jerry\'s scoop shop.',
  28. 'timestamp': 1424246400,
  29. 'upload_date': '20150218',
  30. 'uploader': 'NBCU-COM',
  31. },
  32. 'params': {
  33. # m3u8 download
  34. 'skip_download': True,
  35. },
  36. },
  37. {
  38. 'url': 'http://www.nbc.com/saturday-night-live/video/star-wars-teaser/2832821',
  39. 'info_dict': {
  40. 'id': '2832821',
  41. 'ext': 'mp4',
  42. 'title': 'Star Wars Teaser',
  43. 'description': 'md5:0b40f9cbde5b671a7ff62fceccc4f442',
  44. 'timestamp': 1417852800,
  45. 'upload_date': '20141206',
  46. 'uploader': 'NBCU-COM',
  47. },
  48. 'params': {
  49. # m3u8 download
  50. 'skip_download': True,
  51. },
  52. 'skip': 'Only works from US',
  53. },
  54. {
  55. # HLS streams requires the 'hdnea3' cookie
  56. 'url': 'http://www.nbc.com/Kings/video/goliath/n1806',
  57. 'info_dict': {
  58. 'id': '101528f5a9e8127b107e98c5e6ce4638',
  59. 'ext': 'mp4',
  60. 'title': 'Goliath',
  61. '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.',
  62. 'timestamp': 1237100400,
  63. 'upload_date': '20090315',
  64. 'uploader': 'NBCU-COM',
  65. },
  66. 'params': {
  67. 'skip_download': True,
  68. },
  69. 'skip': 'Only works from US',
  70. },
  71. {
  72. 'url': 'https://www.nbc.com/classic-tv/charles-in-charge/video/charles-in-charge-pilot/n3310',
  73. 'only_matching': True,
  74. },
  75. {
  76. # Percent escaped url
  77. 'url': 'https://www.nbc.com/up-all-night/video/day-after-valentine%27s-day/n2189',
  78. 'only_matching': True,
  79. }
  80. ]
  81. def _real_extract(self, url):
  82. permalink, video_id = re.match(self._VALID_URL, url).groups()
  83. permalink = 'http' + compat_urllib_parse_unquote(permalink)
  84. video_data = self._download_json(
  85. 'https://friendship.nbc.co/v2/graphql', video_id, query={
  86. 'query': '''query bonanzaPage(
  87. $app: NBCUBrands! = nbc
  88. $name: String!
  89. $oneApp: Boolean
  90. $platform: SupportedPlatforms! = web
  91. $type: EntityPageType! = VIDEO
  92. $userId: String!
  93. ) {
  94. bonanzaPage(
  95. app: $app
  96. name: $name
  97. oneApp: $oneApp
  98. platform: $platform
  99. type: $type
  100. userId: $userId
  101. ) {
  102. metadata {
  103. ... on VideoPageData {
  104. description
  105. episodeNumber
  106. keywords
  107. locked
  108. mpxAccountId
  109. mpxGuid
  110. rating
  111. resourceId
  112. seasonNumber
  113. secondaryTitle
  114. seriesShortTitle
  115. }
  116. }
  117. }
  118. }''',
  119. 'variables': json.dumps({
  120. 'name': permalink,
  121. 'oneApp': True,
  122. 'userId': '0',
  123. }),
  124. })['data']['bonanzaPage']['metadata']
  125. query = {
  126. 'mbr': 'true',
  127. 'manifest': 'm3u',
  128. }
  129. video_id = video_data['mpxGuid']
  130. title = video_data['secondaryTitle']
  131. if video_data.get('locked'):
  132. resource = self._get_mvpd_resource(
  133. video_data.get('resourceId') or 'nbcentertainment',
  134. title, video_id, video_data.get('rating'))
  135. query['auth'] = self._extract_mvpd_auth(
  136. url, video_id, 'nbcentertainment', resource)
  137. theplatform_url = smuggle_url(update_url_query(
  138. 'http://link.theplatform.com/s/NnzsPC/media/guid/%s/%s' % (video_data.get('mpxAccountId') or '2410887629', video_id),
  139. query), {'force_smil_url': True})
  140. return {
  141. '_type': 'url_transparent',
  142. 'id': video_id,
  143. 'title': title,
  144. 'url': theplatform_url,
  145. 'description': video_data.get('description'),
  146. 'tags': video_data.get('keywords'),
  147. 'season_number': int_or_none(video_data.get('seasonNumber')),
  148. 'episode_number': int_or_none(video_data.get('episodeNumber')),
  149. 'episode': title,
  150. 'series': video_data.get('seriesShortTitle'),
  151. 'ie_key': 'ThePlatform',
  152. }
  153. class NBCSportsVPlayerIE(InfoExtractor):
  154. _VALID_URL = r'https?://vplayer\.nbcsports\.com/(?:[^/]+/)+(?P<id>[0-9a-zA-Z_]+)'
  155. _TESTS = [{
  156. 'url': 'https://vplayer.nbcsports.com/p/BxmELC/nbcsports_embed/select/9CsDKds0kvHI',
  157. 'info_dict': {
  158. 'id': '9CsDKds0kvHI',
  159. 'ext': 'mp4',
  160. 'description': 'md5:df390f70a9ba7c95ff1daace988f0d8d',
  161. 'title': 'Tyler Kalinoski hits buzzer-beater to lift Davidson',
  162. 'timestamp': 1426270238,
  163. 'upload_date': '20150313',
  164. 'uploader': 'NBCU-SPORTS',
  165. }
  166. }, {
  167. 'url': 'https://vplayer.nbcsports.com/p/BxmELC/nbcsports_embed/select/media/_hqLjQ95yx8Z',
  168. 'only_matching': True,
  169. }]
  170. @staticmethod
  171. def _extract_url(webpage):
  172. iframe_m = re.search(
  173. r'<iframe[^>]+src="(?P<url>https?://vplayer\.nbcsports\.com/[^"]+)"', webpage)
  174. if iframe_m:
  175. return iframe_m.group('url')
  176. def _real_extract(self, url):
  177. video_id = self._match_id(url)
  178. webpage = self._download_webpage(url, video_id)
  179. theplatform_url = self._og_search_video_url(webpage).replace(
  180. 'vplayer.nbcsports.com', 'player.theplatform.com')
  181. return self.url_result(theplatform_url, 'ThePlatform')
  182. class NBCSportsIE(InfoExtractor):
  183. # Does not include https because its certificate is invalid
  184. _VALID_URL = r'https?://(?:www\.)?nbcsports\.com//?(?:[^/]+/)+(?P<id>[0-9a-z-]+)'
  185. _TEST = {
  186. 'url': 'http://www.nbcsports.com//college-basketball/ncaab/tom-izzo-michigan-st-has-so-much-respect-duke',
  187. 'info_dict': {
  188. 'id': 'PHJSaFWbrTY9',
  189. 'ext': 'flv',
  190. 'title': 'Tom Izzo, Michigan St. has \'so much respect\' for Duke',
  191. 'description': 'md5:ecb459c9d59e0766ac9c7d5d0eda8113',
  192. 'uploader': 'NBCU-SPORTS',
  193. 'upload_date': '20150330',
  194. 'timestamp': 1427726529,
  195. }
  196. }
  197. def _real_extract(self, url):
  198. video_id = self._match_id(url)
  199. webpage = self._download_webpage(url, video_id)
  200. return self.url_result(
  201. NBCSportsVPlayerIE._extract_url(webpage), 'NBCSportsVPlayer')
  202. class NBCSportsStreamIE(AdobePassIE):
  203. _VALID_URL = r'https?://stream\.nbcsports\.com/.+?\bpid=(?P<id>\d+)'
  204. _TEST = {
  205. 'url': 'http://stream.nbcsports.com/nbcsn/generic?pid=206559',
  206. 'info_dict': {
  207. 'id': '206559',
  208. 'ext': 'mp4',
  209. 'title': 'Amgen Tour of California Women\'s Recap',
  210. 'description': 'md5:66520066b3b5281ada7698d0ea2aa894',
  211. },
  212. 'params': {
  213. # m3u8 download
  214. 'skip_download': True,
  215. },
  216. 'skip': 'Requires Adobe Pass Authentication',
  217. }
  218. def _real_extract(self, url):
  219. video_id = self._match_id(url)
  220. live_source = self._download_json(
  221. 'http://stream.nbcsports.com/data/live_sources_%s.json' % video_id,
  222. video_id)
  223. video_source = live_source['videoSources'][0]
  224. title = video_source['title']
  225. source_url = None
  226. for k in ('source', 'msl4source', 'iossource', 'hlsv4'):
  227. sk = k + 'Url'
  228. source_url = video_source.get(sk) or video_source.get(sk + 'Alt')
  229. if source_url:
  230. break
  231. else:
  232. source_url = video_source['ottStreamUrl']
  233. is_live = video_source.get('type') == 'live' or video_source.get('status') == 'Live'
  234. resource = self._get_mvpd_resource('nbcsports', title, video_id, '')
  235. token = self._extract_mvpd_auth(url, video_id, 'nbcsports', resource)
  236. tokenized_url = self._download_json(
  237. 'https://token.playmakerservices.com/cdn',
  238. video_id, data=json.dumps({
  239. 'requestorId': 'nbcsports',
  240. 'pid': video_id,
  241. 'application': 'NBCSports',
  242. 'version': 'v1',
  243. 'platform': 'desktop',
  244. 'cdn': 'akamai',
  245. 'url': video_source['sourceUrl'],
  246. 'token': base64.b64encode(token.encode()).decode(),
  247. 'resourceId': base64.b64encode(resource.encode()).decode(),
  248. }).encode())['tokenizedUrl']
  249. formats = self._extract_m3u8_formats(tokenized_url, video_id, 'mp4')
  250. self._sort_formats(formats)
  251. return {
  252. 'id': video_id,
  253. 'title': self._live_title(title) if is_live else title,
  254. 'description': live_source.get('description'),
  255. 'formats': formats,
  256. 'is_live': is_live,
  257. }
  258. class CSNNEIE(InfoExtractor):
  259. _VALID_URL = r'https?://(?:www\.)?csnne\.com/video/(?P<id>[0-9a-z-]+)'
  260. _TEST = {
  261. 'url': 'http://www.csnne.com/video/snc-evening-update-wright-named-red-sox-no-5-starter',
  262. 'info_dict': {
  263. 'id': 'yvBLLUgQ8WU0',
  264. 'ext': 'mp4',
  265. 'title': 'SNC evening update: Wright named Red Sox\' No. 5 starter.',
  266. 'description': 'md5:1753cfee40d9352b19b4c9b3e589b9e3',
  267. 'timestamp': 1459369979,
  268. 'upload_date': '20160330',
  269. 'uploader': 'NBCU-SPORTS',
  270. }
  271. }
  272. def _real_extract(self, url):
  273. display_id = self._match_id(url)
  274. webpage = self._download_webpage(url, display_id)
  275. return {
  276. '_type': 'url_transparent',
  277. 'ie_key': 'ThePlatform',
  278. 'url': self._html_search_meta('twitter:player:stream', webpage),
  279. 'display_id': display_id,
  280. }
  281. class NBCNewsIE(ThePlatformIE):
  282. _VALID_URL = r'(?x)https?://(?:www\.)?(?:nbcnews|today|msnbc)\.com/([^/]+/)*(?:.*-)?(?P<id>[^/?]+)'
  283. _TESTS = [
  284. {
  285. 'url': 'http://www.nbcnews.com/watch/nbcnews-com/how-twitter-reacted-to-the-snowden-interview-269389891880',
  286. 'md5': 'cf4bc9e6ce0130f00f545d80ecedd4bf',
  287. 'info_dict': {
  288. 'id': '269389891880',
  289. 'ext': 'mp4',
  290. 'title': 'How Twitter Reacted To The Snowden Interview',
  291. 'description': 'md5:65a0bd5d76fe114f3c2727aa3a81fe64',
  292. 'timestamp': 1401363060,
  293. 'upload_date': '20140529',
  294. },
  295. },
  296. {
  297. 'url': 'http://www.nbcnews.com/feature/dateline-full-episodes/full-episode-family-business-n285156',
  298. 'md5': 'fdbf39ab73a72df5896b6234ff98518a',
  299. 'info_dict': {
  300. 'id': '529953347624',
  301. 'ext': 'mp4',
  302. 'title': 'FULL EPISODE: Family Business',
  303. 'description': 'md5:757988edbaae9d7be1d585eb5d55cc04',
  304. },
  305. 'skip': 'This page is unavailable.',
  306. },
  307. {
  308. 'url': 'http://www.nbcnews.com/nightly-news/video/nightly-news-with-brian-williams-full-broadcast-february-4-394064451844',
  309. 'md5': '8eb831eca25bfa7d25ddd83e85946548',
  310. 'info_dict': {
  311. 'id': '394064451844',
  312. 'ext': 'mp4',
  313. 'title': 'Nightly News with Brian Williams Full Broadcast (February 4)',
  314. 'description': 'md5:1c10c1eccbe84a26e5debb4381e2d3c5',
  315. 'timestamp': 1423104900,
  316. 'upload_date': '20150205',
  317. },
  318. },
  319. {
  320. 'url': 'http://www.nbcnews.com/business/autos/volkswagen-11-million-vehicles-could-have-suspect-software-emissions-scandal-n431456',
  321. 'md5': '4a8c4cec9e1ded51060bdda36ff0a5c0',
  322. 'info_dict': {
  323. 'id': 'n431456',
  324. 'ext': 'mp4',
  325. 'title': "Volkswagen U.S. Chief: We 'Totally Screwed Up'",
  326. 'description': 'md5:d22d1281a24f22ea0880741bb4dd6301',
  327. 'upload_date': '20150922',
  328. 'timestamp': 1442917800,
  329. },
  330. },
  331. {
  332. 'url': 'http://www.today.com/video/see-the-aurora-borealis-from-space-in-stunning-new-nasa-video-669831235788',
  333. 'md5': '118d7ca3f0bea6534f119c68ef539f71',
  334. 'info_dict': {
  335. 'id': '669831235788',
  336. 'ext': 'mp4',
  337. 'title': 'See the aurora borealis from space in stunning new NASA video',
  338. 'description': 'md5:74752b7358afb99939c5f8bb2d1d04b1',
  339. 'upload_date': '20160420',
  340. 'timestamp': 1461152093,
  341. },
  342. },
  343. {
  344. 'url': 'http://www.msnbc.com/all-in-with-chris-hayes/watch/the-chaotic-gop-immigration-vote-314487875924',
  345. 'md5': '6d236bf4f3dddc226633ce6e2c3f814d',
  346. 'info_dict': {
  347. 'id': '314487875924',
  348. 'ext': 'mp4',
  349. 'title': 'The chaotic GOP immigration vote',
  350. '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.',
  351. 'thumbnail': r're:^https?://.*\.jpg$',
  352. 'timestamp': 1406937606,
  353. 'upload_date': '20140802',
  354. },
  355. },
  356. {
  357. 'url': 'http://www.nbcnews.com/watch/dateline/full-episode--deadly-betrayal-386250819952',
  358. 'only_matching': True,
  359. },
  360. {
  361. # From http://www.vulture.com/2016/06/letterman-couldnt-care-less-about-late-night.html
  362. 'url': 'http://www.nbcnews.com/widget/video-embed/701714499682',
  363. 'only_matching': True,
  364. },
  365. ]
  366. def _real_extract(self, url):
  367. video_id = self._match_id(url)
  368. webpage = self._download_webpage(url, video_id)
  369. data = self._parse_json(self._search_regex(
  370. r'window\.__data\s*=\s*({.+});', webpage,
  371. 'bootstrap json'), video_id, js_to_json)
  372. video_data = try_get(data, lambda x: x['video']['current'], dict)
  373. if not video_data:
  374. video_data = data['article']['content'][0]['primaryMedia']['video']
  375. title = video_data['headline']['primary']
  376. formats = []
  377. for va in video_data.get('videoAssets', []):
  378. public_url = va.get('publicUrl')
  379. if not public_url:
  380. continue
  381. if '://link.theplatform.com/' in public_url:
  382. public_url = update_url_query(public_url, {'format': 'redirect'})
  383. format_id = va.get('format')
  384. if format_id == 'M3U':
  385. formats.extend(self._extract_m3u8_formats(
  386. public_url, video_id, 'mp4', 'm3u8_native',
  387. m3u8_id=format_id, fatal=False))
  388. continue
  389. tbr = int_or_none(va.get('bitrate'), 1000)
  390. if tbr:
  391. format_id += '-%d' % tbr
  392. formats.append({
  393. 'format_id': format_id,
  394. 'url': public_url,
  395. 'width': int_or_none(va.get('width')),
  396. 'height': int_or_none(va.get('height')),
  397. 'tbr': tbr,
  398. 'ext': 'mp4',
  399. })
  400. self._sort_formats(formats)
  401. subtitles = {}
  402. closed_captioning = video_data.get('closedCaptioning')
  403. if closed_captioning:
  404. for cc_url in closed_captioning.values():
  405. if not cc_url:
  406. continue
  407. subtitles.setdefault('en', []).append({
  408. 'url': cc_url,
  409. })
  410. return {
  411. 'id': video_id,
  412. 'title': title,
  413. 'description': try_get(video_data, lambda x: x['description']['primary']),
  414. 'thumbnail': try_get(video_data, lambda x: x['primaryImage']['url']['primary']),
  415. 'duration': parse_duration(video_data.get('duration')),
  416. 'timestamp': unified_timestamp(video_data.get('datePublished')),
  417. 'formats': formats,
  418. 'subtitles': subtitles,
  419. }
  420. class NBCOlympicsIE(InfoExtractor):
  421. IE_NAME = 'nbcolympics'
  422. _VALID_URL = r'https?://www\.nbcolympics\.com/video/(?P<id>[a-z-]+)'
  423. _TEST = {
  424. # Geo-restricted to US
  425. 'url': 'http://www.nbcolympics.com/video/justin-roses-son-leo-was-tears-after-his-dad-won-gold',
  426. 'md5': '54fecf846d05429fbaa18af557ee523a',
  427. 'info_dict': {
  428. 'id': 'WjTBzDXx5AUq',
  429. 'display_id': 'justin-roses-son-leo-was-tears-after-his-dad-won-gold',
  430. 'ext': 'mp4',
  431. 'title': 'Rose\'s son Leo was in tears after his dad won gold',
  432. 'description': 'Olympic gold medalist Justin Rose gets emotional talking to the impact his win in men\'s golf has already had on his children.',
  433. 'timestamp': 1471274964,
  434. 'upload_date': '20160815',
  435. 'uploader': 'NBCU-SPORTS',
  436. },
  437. }
  438. def _real_extract(self, url):
  439. display_id = self._match_id(url)
  440. webpage = self._download_webpage(url, display_id)
  441. drupal_settings = self._parse_json(self._search_regex(
  442. r'jQuery\.extend\(Drupal\.settings\s*,\s*({.+?})\);',
  443. webpage, 'drupal settings'), display_id)
  444. iframe_url = drupal_settings['vod']['iframe_url']
  445. theplatform_url = iframe_url.replace(
  446. 'vplayer.nbcolympics.com', 'player.theplatform.com')
  447. return {
  448. '_type': 'url_transparent',
  449. 'url': theplatform_url,
  450. 'ie_key': ThePlatformIE.ie_key(),
  451. 'display_id': display_id,
  452. }
  453. class NBCOlympicsStreamIE(AdobePassIE):
  454. IE_NAME = 'nbcolympics:stream'
  455. _VALID_URL = r'https?://stream\.nbcolympics\.com/(?P<id>[0-9a-z-]+)'
  456. _TEST = {
  457. 'url': 'http://stream.nbcolympics.com/2018-winter-olympics-nbcsn-evening-feb-8',
  458. 'info_dict': {
  459. 'id': '203493',
  460. 'ext': 'mp4',
  461. 'title': 're:Curling, Alpine, Luge [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$',
  462. },
  463. 'params': {
  464. # m3u8 download
  465. 'skip_download': True,
  466. },
  467. }
  468. _DATA_URL_TEMPLATE = 'http://stream.nbcolympics.com/data/%s_%s.json'
  469. def _real_extract(self, url):
  470. display_id = self._match_id(url)
  471. webpage = self._download_webpage(url, display_id)
  472. pid = self._search_regex(r'pid\s*=\s*(\d+);', webpage, 'pid')
  473. resource = self._search_regex(
  474. r"resource\s*=\s*'(.+)';", webpage,
  475. 'resource').replace("' + pid + '", pid)
  476. event_config = self._download_json(
  477. self._DATA_URL_TEMPLATE % ('event_config', pid),
  478. pid)['eventConfig']
  479. title = self._live_title(event_config['eventTitle'])
  480. source_url = self._download_json(
  481. self._DATA_URL_TEMPLATE % ('live_sources', pid),
  482. pid)['videoSources'][0]['sourceUrl']
  483. media_token = self._extract_mvpd_auth(
  484. url, pid, event_config.get('requestorId', 'NBCOlympics'), resource)
  485. formats = self._extract_m3u8_formats(self._download_webpage(
  486. 'http://sp.auth.adobe.com/tvs/v1/sign', pid, query={
  487. 'cdn': 'akamai',
  488. 'mediaToken': base64.b64encode(media_token.encode()),
  489. 'resource': base64.b64encode(resource.encode()),
  490. 'url': source_url,
  491. }), pid, 'mp4')
  492. self._sort_formats(formats)
  493. return {
  494. 'id': pid,
  495. 'display_id': display_id,
  496. 'title': title,
  497. 'formats': formats,
  498. 'is_live': True,
  499. }