nbc.py 18 KB

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