logo

youtube-dl

[mirror] Download/Watch videos from video hostersgit clone https://hacktivis.me/git/mirror/youtube-dl.git

nbc.py (20411B)


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