nbc.py 18 KB

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