nbc.py 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  1. import base64
  2. import json
  3. import re
  4. from .common import InfoExtractor
  5. from .theplatform import ThePlatformIE
  6. from .adobepass import AdobePassIE
  7. from ..compat import compat_urllib_parse_unquote
  8. from ..utils import (
  9. ExtractorError,
  10. int_or_none,
  11. parse_age_limit,
  12. parse_duration,
  13. RegexNotFoundError,
  14. smuggle_url,
  15. str_or_none,
  16. traverse_obj,
  17. try_get,
  18. unified_strdate,
  19. unified_timestamp,
  20. update_url_query,
  21. url_basename,
  22. variadic,
  23. )
  24. class NBCIE(ThePlatformIE): # XXX: Do not subclass from concrete IE
  25. _VALID_URL = r'https?(?P<permalink>://(?:www\.)?nbc\.com/(?:classic-tv/)?[^/]+/video/[^/]+/(?P<id>n?\d+))'
  26. _TESTS = [
  27. {
  28. 'url': 'http://www.nbc.com/the-tonight-show/video/jimmy-fallon-surprises-fans-at-ben-jerrys/2848237',
  29. 'info_dict': {
  30. 'id': '2848237',
  31. 'ext': 'mp4',
  32. 'title': 'Jimmy Fallon Surprises Fans at Ben & Jerry\'s',
  33. 'description': 'Jimmy gives out free scoops of his new "Tonight Dough" ice cream flavor by surprising customers at the Ben & Jerry\'s scoop shop.',
  34. 'timestamp': 1424246400,
  35. 'upload_date': '20150218',
  36. 'uploader': 'NBCU-COM',
  37. },
  38. 'params': {
  39. # m3u8 download
  40. 'skip_download': True,
  41. },
  42. },
  43. {
  44. 'url': 'http://www.nbc.com/saturday-night-live/video/star-wars-teaser/2832821',
  45. 'info_dict': {
  46. 'id': '2832821',
  47. 'ext': 'mp4',
  48. 'title': 'Star Wars Teaser',
  49. 'description': 'md5:0b40f9cbde5b671a7ff62fceccc4f442',
  50. 'timestamp': 1417852800,
  51. 'upload_date': '20141206',
  52. 'uploader': 'NBCU-COM',
  53. },
  54. 'params': {
  55. # m3u8 download
  56. 'skip_download': True,
  57. },
  58. 'skip': 'Only works from US',
  59. },
  60. {
  61. # HLS streams requires the 'hdnea3' cookie
  62. 'url': 'http://www.nbc.com/Kings/video/goliath/n1806',
  63. 'info_dict': {
  64. 'id': '101528f5a9e8127b107e98c5e6ce4638',
  65. 'ext': 'mp4',
  66. 'title': 'Goliath',
  67. '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.',
  68. 'timestamp': 1237100400,
  69. 'upload_date': '20090315',
  70. 'uploader': 'NBCU-COM',
  71. },
  72. 'params': {
  73. 'skip_download': True,
  74. },
  75. 'skip': 'Only works from US',
  76. },
  77. {
  78. 'url': 'https://www.nbc.com/classic-tv/charles-in-charge/video/charles-in-charge-pilot/n3310',
  79. 'only_matching': True,
  80. },
  81. {
  82. # Percent escaped url
  83. 'url': 'https://www.nbc.com/up-all-night/video/day-after-valentine%27s-day/n2189',
  84. 'only_matching': True,
  85. }
  86. ]
  87. def _real_extract(self, url):
  88. permalink, video_id = self._match_valid_url(url).groups()
  89. permalink = 'http' + compat_urllib_parse_unquote(permalink)
  90. video_data = self._download_json(
  91. 'https://friendship.nbc.co/v2/graphql', video_id, query={
  92. 'query': '''query bonanzaPage(
  93. $app: NBCUBrands! = nbc
  94. $name: String!
  95. $oneApp: Boolean
  96. $platform: SupportedPlatforms! = web
  97. $type: EntityPageType! = VIDEO
  98. $userId: String!
  99. ) {
  100. bonanzaPage(
  101. app: $app
  102. name: $name
  103. oneApp: $oneApp
  104. platform: $platform
  105. type: $type
  106. userId: $userId
  107. ) {
  108. metadata {
  109. ... on VideoPageData {
  110. description
  111. episodeNumber
  112. keywords
  113. locked
  114. mpxAccountId
  115. mpxGuid
  116. rating
  117. resourceId
  118. seasonNumber
  119. secondaryTitle
  120. seriesShortTitle
  121. }
  122. }
  123. }
  124. }''',
  125. 'variables': json.dumps({
  126. 'name': permalink,
  127. 'oneApp': True,
  128. 'userId': '0',
  129. }),
  130. })['data']['bonanzaPage']['metadata']
  131. query = {
  132. 'mbr': 'true',
  133. 'manifest': 'm3u',
  134. }
  135. video_id = video_data['mpxGuid']
  136. tp_path = 'NnzsPC/media/guid/%s/%s' % (video_data.get('mpxAccountId') or '2410887629', video_id)
  137. tpm = self._download_theplatform_metadata(tp_path, video_id)
  138. title = tpm.get('title') or video_data.get('secondaryTitle')
  139. if video_data.get('locked'):
  140. resource = self._get_mvpd_resource(
  141. video_data.get('resourceId') or 'nbcentertainment',
  142. title, video_id, video_data.get('rating'))
  143. query['auth'] = self._extract_mvpd_auth(
  144. url, video_id, 'nbcentertainment', resource)
  145. theplatform_url = smuggle_url(update_url_query(
  146. 'http://link.theplatform.com/s/NnzsPC/media/guid/%s/%s' % (video_data.get('mpxAccountId') or '2410887629', video_id),
  147. query), {'force_smil_url': True})
  148. # Empty string or 0 can be valid values for these. So the check must be `is None`
  149. description = video_data.get('description')
  150. if description is None:
  151. description = tpm.get('description')
  152. episode_number = int_or_none(video_data.get('episodeNumber'))
  153. if episode_number is None:
  154. episode_number = int_or_none(tpm.get('nbcu$airOrder'))
  155. rating = video_data.get('rating')
  156. if rating is None:
  157. try_get(tpm, lambda x: x['ratings'][0]['rating'])
  158. season_number = int_or_none(video_data.get('seasonNumber'))
  159. if season_number is None:
  160. season_number = int_or_none(tpm.get('nbcu$seasonNumber'))
  161. series = video_data.get('seriesShortTitle')
  162. if series is None:
  163. series = tpm.get('nbcu$seriesShortTitle')
  164. tags = video_data.get('keywords')
  165. if tags is None or len(tags) == 0:
  166. tags = tpm.get('keywords')
  167. return {
  168. '_type': 'url_transparent',
  169. 'age_limit': parse_age_limit(rating),
  170. 'description': description,
  171. 'episode': title,
  172. 'episode_number': episode_number,
  173. 'id': video_id,
  174. 'ie_key': 'ThePlatform',
  175. 'season_number': season_number,
  176. 'series': series,
  177. 'tags': tags,
  178. 'title': title,
  179. 'url': theplatform_url,
  180. }
  181. class NBCSportsVPlayerIE(InfoExtractor):
  182. _VALID_URL_BASE = r'https?://(?:vplayer\.nbcsports\.com|(?:www\.)?nbcsports\.com/vplayer)/'
  183. _VALID_URL = _VALID_URL_BASE + r'(?:[^/]+/)+(?P<id>[0-9a-zA-Z_]+)'
  184. _EMBED_REGEX = [r'(?:iframe[^>]+|var video|div[^>]+data-(?:mpx-)?)[sS]rc\s?=\s?"(?P<url>%s[^\"]+)' % _VALID_URL_BASE]
  185. _TESTS = [{
  186. 'url': 'https://vplayer.nbcsports.com/p/BxmELC/nbcsports_embed/select/9CsDKds0kvHI',
  187. 'info_dict': {
  188. 'id': '9CsDKds0kvHI',
  189. 'ext': 'mp4',
  190. 'description': 'md5:df390f70a9ba7c95ff1daace988f0d8d',
  191. 'title': 'Tyler Kalinoski hits buzzer-beater to lift Davidson',
  192. 'timestamp': 1426270238,
  193. 'upload_date': '20150313',
  194. 'uploader': 'NBCU-SPORTS',
  195. 'duration': 72.818,
  196. 'chapters': [],
  197. 'thumbnail': r're:^https?://.*\.jpg$'
  198. }
  199. }, {
  200. 'url': 'https://vplayer.nbcsports.com/p/BxmELC/nbcsports_embed/select/media/PEgOtlNcC_y2',
  201. 'only_matching': True,
  202. }, {
  203. 'url': 'https://www.nbcsports.com/vplayer/p/BxmELC/nbcsports/select/PHJSaFWbrTY9?form=html&autoPlay=true',
  204. 'only_matching': True,
  205. }]
  206. def _real_extract(self, url):
  207. video_id = self._match_id(url)
  208. webpage = self._download_webpage(url, video_id)
  209. theplatform_url = self._html_search_regex(r'tp:releaseUrl="(.+?)"', webpage, 'url')
  210. return self.url_result(theplatform_url, 'ThePlatform')
  211. class NBCSportsIE(InfoExtractor):
  212. _VALID_URL = r'https?://(?:www\.)?nbcsports\.com//?(?!vplayer/)(?:[^/]+/)+(?P<id>[0-9a-z-]+)'
  213. _TESTS = [{
  214. # iframe src
  215. 'url': 'http://www.nbcsports.com//college-basketball/ncaab/tom-izzo-michigan-st-has-so-much-respect-duke',
  216. 'info_dict': {
  217. 'id': 'PHJSaFWbrTY9',
  218. 'ext': 'mp4',
  219. 'title': 'Tom Izzo, Michigan St. has \'so much respect\' for Duke',
  220. 'description': 'md5:ecb459c9d59e0766ac9c7d5d0eda8113',
  221. 'uploader': 'NBCU-SPORTS',
  222. 'upload_date': '20150330',
  223. 'timestamp': 1427726529,
  224. 'chapters': [],
  225. 'thumbnail': 'https://hdliveextra-a.akamaihd.net/HD/image_sports/NBCU_Sports_Group_-_nbcsports/253/303/izzodps.jpg',
  226. 'duration': 528.395,
  227. }
  228. }, {
  229. # data-mpx-src
  230. 'url': 'https://www.nbcsports.com/philadelphia/philadelphia-phillies/bruce-bochy-hector-neris-hes-idiot',
  231. 'only_matching': True,
  232. }, {
  233. # data-src
  234. 'url': 'https://www.nbcsports.com/boston/video/report-card-pats-secondary-no-match-josh-allen',
  235. 'only_matching': True,
  236. }]
  237. def _real_extract(self, url):
  238. video_id = self._match_id(url)
  239. webpage = self._download_webpage(url, video_id)
  240. return self.url_result(
  241. NBCSportsVPlayerIE._extract_url(webpage), 'NBCSportsVPlayer')
  242. class NBCSportsStreamIE(AdobePassIE):
  243. _VALID_URL = r'https?://stream\.nbcsports\.com/.+?\bpid=(?P<id>\d+)'
  244. _TEST = {
  245. 'url': 'http://stream.nbcsports.com/nbcsn/generic?pid=206559',
  246. 'info_dict': {
  247. 'id': '206559',
  248. 'ext': 'mp4',
  249. 'title': 'Amgen Tour of California Women\'s Recap',
  250. 'description': 'md5:66520066b3b5281ada7698d0ea2aa894',
  251. },
  252. 'params': {
  253. # m3u8 download
  254. 'skip_download': True,
  255. },
  256. 'skip': 'Requires Adobe Pass Authentication',
  257. }
  258. def _real_extract(self, url):
  259. video_id = self._match_id(url)
  260. live_source = self._download_json(
  261. 'http://stream.nbcsports.com/data/live_sources_%s.json' % video_id,
  262. video_id)
  263. video_source = live_source['videoSources'][0]
  264. title = video_source['title']
  265. source_url = None
  266. for k in ('source', 'msl4source', 'iossource', 'hlsv4'):
  267. sk = k + 'Url'
  268. source_url = video_source.get(sk) or video_source.get(sk + 'Alt')
  269. if source_url:
  270. break
  271. else:
  272. source_url = video_source['ottStreamUrl']
  273. is_live = video_source.get('type') == 'live' or video_source.get('status') == 'Live'
  274. resource = self._get_mvpd_resource('nbcsports', title, video_id, '')
  275. token = self._extract_mvpd_auth(url, video_id, 'nbcsports', resource)
  276. tokenized_url = self._download_json(
  277. 'https://token.playmakerservices.com/cdn',
  278. video_id, data=json.dumps({
  279. 'requestorId': 'nbcsports',
  280. 'pid': video_id,
  281. 'application': 'NBCSports',
  282. 'version': 'v1',
  283. 'platform': 'desktop',
  284. 'cdn': 'akamai',
  285. 'url': video_source['sourceUrl'],
  286. 'token': base64.b64encode(token.encode()).decode(),
  287. 'resourceId': base64.b64encode(resource.encode()).decode(),
  288. }).encode())['tokenizedUrl']
  289. formats = self._extract_m3u8_formats(tokenized_url, video_id, 'mp4')
  290. return {
  291. 'id': video_id,
  292. 'title': title,
  293. 'description': live_source.get('description'),
  294. 'formats': formats,
  295. 'is_live': is_live,
  296. }
  297. class NBCNewsIE(ThePlatformIE): # XXX: Do not subclass from concrete IE
  298. _VALID_URL = r'(?x)https?://(?:www\.)?(?:nbcnews|today|msnbc)\.com/([^/]+/)*(?:.*-)?(?P<id>[^/?]+)'
  299. _EMBED_REGEX = [r'<iframe[^>]+src=(["\'])(?P<url>(?:https?:)?//www\.nbcnews\.com/widget/video-embed/[^"\']+)\1']
  300. _TESTS = [
  301. {
  302. 'url': 'http://www.nbcnews.com/watch/nbcnews-com/how-twitter-reacted-to-the-snowden-interview-269389891880',
  303. 'md5': 'cf4bc9e6ce0130f00f545d80ecedd4bf',
  304. 'info_dict': {
  305. 'id': '269389891880',
  306. 'ext': 'mp4',
  307. 'title': 'How Twitter Reacted To The Snowden Interview',
  308. 'description': 'md5:65a0bd5d76fe114f3c2727aa3a81fe64',
  309. 'timestamp': 1401363060,
  310. 'upload_date': '20140529',
  311. },
  312. },
  313. {
  314. 'url': 'http://www.nbcnews.com/feature/dateline-full-episodes/full-episode-family-business-n285156',
  315. 'md5': 'fdbf39ab73a72df5896b6234ff98518a',
  316. 'info_dict': {
  317. 'id': '529953347624',
  318. 'ext': 'mp4',
  319. 'title': 'FULL EPISODE: Family Business',
  320. 'description': 'md5:757988edbaae9d7be1d585eb5d55cc04',
  321. },
  322. 'skip': 'This page is unavailable.',
  323. },
  324. {
  325. 'url': 'http://www.nbcnews.com/nightly-news/video/nightly-news-with-brian-williams-full-broadcast-february-4-394064451844',
  326. 'md5': '8eb831eca25bfa7d25ddd83e85946548',
  327. 'info_dict': {
  328. 'id': '394064451844',
  329. 'ext': 'mp4',
  330. 'title': 'Nightly News with Brian Williams Full Broadcast (February 4)',
  331. 'description': 'md5:1c10c1eccbe84a26e5debb4381e2d3c5',
  332. 'timestamp': 1423104900,
  333. 'upload_date': '20150205',
  334. },
  335. },
  336. {
  337. 'url': 'http://www.nbcnews.com/business/autos/volkswagen-11-million-vehicles-could-have-suspect-software-emissions-scandal-n431456',
  338. 'md5': '4a8c4cec9e1ded51060bdda36ff0a5c0',
  339. 'info_dict': {
  340. 'id': 'n431456',
  341. 'ext': 'mp4',
  342. 'title': "Volkswagen U.S. Chief: We 'Totally Screwed Up'",
  343. 'description': 'md5:d22d1281a24f22ea0880741bb4dd6301',
  344. 'upload_date': '20150922',
  345. 'timestamp': 1442917800,
  346. },
  347. },
  348. {
  349. 'url': 'http://www.today.com/video/see-the-aurora-borealis-from-space-in-stunning-new-nasa-video-669831235788',
  350. 'md5': '118d7ca3f0bea6534f119c68ef539f71',
  351. 'info_dict': {
  352. 'id': '669831235788',
  353. 'ext': 'mp4',
  354. 'title': 'See the aurora borealis from space in stunning new NASA video',
  355. 'description': 'md5:74752b7358afb99939c5f8bb2d1d04b1',
  356. 'upload_date': '20160420',
  357. 'timestamp': 1461152093,
  358. },
  359. },
  360. {
  361. 'url': 'http://www.msnbc.com/all-in-with-chris-hayes/watch/the-chaotic-gop-immigration-vote-314487875924',
  362. 'md5': '6d236bf4f3dddc226633ce6e2c3f814d',
  363. 'info_dict': {
  364. 'id': '314487875924',
  365. 'ext': 'mp4',
  366. 'title': 'The chaotic GOP immigration vote',
  367. '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.',
  368. 'thumbnail': r're:^https?://.*\.jpg$',
  369. 'timestamp': 1406937606,
  370. 'upload_date': '20140802',
  371. },
  372. },
  373. {
  374. 'url': 'http://www.nbcnews.com/watch/dateline/full-episode--deadly-betrayal-386250819952',
  375. 'only_matching': True,
  376. },
  377. {
  378. # From http://www.vulture.com/2016/06/letterman-couldnt-care-less-about-late-night.html
  379. 'url': 'http://www.nbcnews.com/widget/video-embed/701714499682',
  380. 'only_matching': True,
  381. },
  382. ]
  383. def _real_extract(self, url):
  384. video_id = self._match_id(url)
  385. webpage = self._download_webpage(url, video_id)
  386. data = self._search_nextjs_data(webpage, video_id)['props']['initialState']
  387. video_data = try_get(data, lambda x: x['video']['current'], dict)
  388. if not video_data:
  389. video_data = data['article']['content'][0]['primaryMedia']['video']
  390. title = video_data['headline']['primary']
  391. formats = []
  392. for va in video_data.get('videoAssets', []):
  393. public_url = va.get('publicUrl')
  394. if not public_url:
  395. continue
  396. if '://link.theplatform.com/' in public_url:
  397. public_url = update_url_query(public_url, {'format': 'redirect'})
  398. format_id = va.get('format')
  399. if format_id == 'M3U':
  400. formats.extend(self._extract_m3u8_formats(
  401. public_url, video_id, 'mp4', 'm3u8_native',
  402. m3u8_id=format_id, fatal=False))
  403. continue
  404. tbr = int_or_none(va.get('bitrate'), 1000)
  405. if tbr:
  406. format_id += '-%d' % tbr
  407. formats.append({
  408. 'format_id': format_id,
  409. 'url': public_url,
  410. 'width': int_or_none(va.get('width')),
  411. 'height': int_or_none(va.get('height')),
  412. 'tbr': tbr,
  413. 'ext': 'mp4',
  414. })
  415. subtitles = {}
  416. closed_captioning = video_data.get('closedCaptioning')
  417. if closed_captioning:
  418. for cc_url in closed_captioning.values():
  419. if not cc_url:
  420. continue
  421. subtitles.setdefault('en', []).append({
  422. 'url': cc_url,
  423. })
  424. return {
  425. 'id': video_id,
  426. 'title': title,
  427. 'description': try_get(video_data, lambda x: x['description']['primary']),
  428. 'thumbnail': try_get(video_data, lambda x: x['primaryImage']['url']['primary']),
  429. 'duration': parse_duration(video_data.get('duration')),
  430. 'timestamp': unified_timestamp(video_data.get('datePublished')),
  431. 'formats': formats,
  432. 'subtitles': subtitles,
  433. }
  434. class NBCOlympicsIE(InfoExtractor):
  435. IE_NAME = 'nbcolympics'
  436. _VALID_URL = r'https?://www\.nbcolympics\.com/videos?/(?P<id>[0-9a-z-]+)'
  437. _TEST = {
  438. # Geo-restricted to US
  439. 'url': 'http://www.nbcolympics.com/video/justin-roses-son-leo-was-tears-after-his-dad-won-gold',
  440. 'md5': '54fecf846d05429fbaa18af557ee523a',
  441. 'info_dict': {
  442. 'id': 'WjTBzDXx5AUq',
  443. 'display_id': 'justin-roses-son-leo-was-tears-after-his-dad-won-gold',
  444. 'ext': 'mp4',
  445. 'title': 'Rose\'s son Leo was in tears after his dad won gold',
  446. 'description': 'Olympic gold medalist Justin Rose gets emotional talking to the impact his win in men\'s golf has already had on his children.',
  447. 'timestamp': 1471274964,
  448. 'upload_date': '20160815',
  449. 'uploader': 'NBCU-SPORTS',
  450. },
  451. }
  452. def _real_extract(self, url):
  453. display_id = self._match_id(url)
  454. webpage = self._download_webpage(url, display_id)
  455. try:
  456. drupal_settings = self._parse_json(self._search_regex(
  457. r'jQuery\.extend\(Drupal\.settings\s*,\s*({.+?})\);',
  458. webpage, 'drupal settings'), display_id)
  459. iframe_url = drupal_settings['vod']['iframe_url']
  460. theplatform_url = iframe_url.replace(
  461. 'vplayer.nbcolympics.com', 'player.theplatform.com')
  462. except RegexNotFoundError:
  463. theplatform_url = self._search_regex(
  464. r"([\"'])embedUrl\1: *([\"'])(?P<embedUrl>.+)\2",
  465. webpage, 'embedding URL', group="embedUrl")
  466. return {
  467. '_type': 'url_transparent',
  468. 'url': theplatform_url,
  469. 'ie_key': ThePlatformIE.ie_key(),
  470. 'display_id': display_id,
  471. }
  472. class NBCOlympicsStreamIE(AdobePassIE):
  473. IE_NAME = 'nbcolympics:stream'
  474. _VALID_URL = r'https?://stream\.nbcolympics\.com/(?P<id>[0-9a-z-]+)'
  475. _TESTS = [
  476. {
  477. 'note': 'Tokenized m3u8 source URL',
  478. 'url': 'https://stream.nbcolympics.com/womens-soccer-group-round-11',
  479. 'info_dict': {
  480. 'id': '2019740',
  481. 'ext': 'mp4',
  482. 'title': r"re:Women's Group Stage - Netherlands vs\. Brazil [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$",
  483. },
  484. 'params': {
  485. 'skip_download': 'm3u8',
  486. },
  487. }, {
  488. 'note': 'Plain m3u8 source URL',
  489. 'url': 'https://stream.nbcolympics.com/gymnastics-event-finals-mens-floor-pommel-horse-womens-vault-bars',
  490. 'info_dict': {
  491. 'id': '2021729',
  492. 'ext': 'mp4',
  493. 'title': r're:Event Finals: M Floor, W Vault, M Pommel, W Uneven Bars [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$',
  494. },
  495. 'params': {
  496. 'skip_download': 'm3u8',
  497. },
  498. },
  499. ]
  500. def _real_extract(self, url):
  501. display_id = self._match_id(url)
  502. webpage = self._download_webpage(url, display_id)
  503. pid = self._search_regex(r'pid\s*=\s*(\d+);', webpage, 'pid')
  504. event_config = self._download_json(
  505. f'http://stream.nbcolympics.com/data/event_config_{pid}.json',
  506. pid, 'Downloading event config')['eventConfig']
  507. title = event_config['eventTitle']
  508. is_live = {'live': True, 'replay': False}.get(event_config.get('eventStatus'))
  509. source_url = self._download_json(
  510. f'https://api-leap.nbcsports.com/feeds/assets/{pid}?application=NBCOlympics&platform=desktop&format=nbc-player&env=staging',
  511. pid, 'Downloading leap config'
  512. )['videoSources'][0]['cdnSources']['primary'][0]['sourceUrl']
  513. if event_config.get('cdnToken'):
  514. ap_resource = self._get_mvpd_resource(
  515. event_config.get('resourceId', 'NBCOlympics'),
  516. re.sub(r'[^\w\d ]+', '', event_config['eventTitle']), pid,
  517. event_config.get('ratingId', 'NO VALUE'))
  518. media_token = self._extract_mvpd_auth(url, pid, event_config.get('requestorId', 'NBCOlympics'), ap_resource)
  519. source_url = self._download_json(
  520. 'https://tokens.playmakerservices.com/', pid, 'Retrieving tokenized URL',
  521. data=json.dumps({
  522. 'application': 'NBCSports',
  523. 'authentication-type': 'adobe-pass',
  524. 'cdn': 'akamai',
  525. 'pid': pid,
  526. 'platform': 'desktop',
  527. 'requestorId': 'NBCOlympics',
  528. 'resourceId': base64.b64encode(ap_resource.encode()).decode(),
  529. 'token': base64.b64encode(media_token.encode()).decode(),
  530. 'url': source_url,
  531. 'version': 'v1',
  532. }).encode(),
  533. )['akamai'][0]['tokenizedUrl']
  534. formats = self._extract_m3u8_formats(source_url, pid, 'mp4', live=is_live)
  535. for f in formats:
  536. # -http_seekable requires ffmpeg 4.3+ but it doesnt seem possible to
  537. # download with ffmpeg without this option
  538. f['downloader_options'] = {'ffmpeg_args': ['-seekable', '0', '-http_seekable', '0', '-icy', '0']}
  539. return {
  540. 'id': pid,
  541. 'display_id': display_id,
  542. 'title': title,
  543. 'formats': formats,
  544. 'is_live': is_live,
  545. }
  546. class NBCStationsIE(InfoExtractor):
  547. _DOMAIN_RE = '|'.join(map(re.escape, (
  548. 'nbcbayarea', 'nbcboston', 'nbcchicago', 'nbcconnecticut', 'nbcdfw', 'nbclosangeles',
  549. 'nbcmiami', 'nbcnewyork', 'nbcphiladelphia', 'nbcsandiego', 'nbcwashington',
  550. 'necn', 'telemundo52', 'telemundoarizona', 'telemundochicago', 'telemundonuevainglaterra',
  551. )))
  552. _VALID_URL = rf'https?://(?:www\.)?(?P<site>{_DOMAIN_RE})\.com/(?:[^/?#]+/)*(?P<id>[^/?#]+)/?(?:$|[#?])'
  553. _TESTS = [{
  554. 'url': 'https://www.nbclosangeles.com/news/local/large-structure-fire-in-downtown-la-prompts-smoke-odor-advisory/2968618/',
  555. 'md5': '462041d91bd762ef5a38b7d85d6dc18f',
  556. 'info_dict': {
  557. 'id': '2968618',
  558. 'ext': 'mp4',
  559. 'title': 'Large Structure Fire in Downtown LA Prompts Smoke Odor Advisory',
  560. 'description': None,
  561. 'timestamp': 1661135892,
  562. 'upload_date': '20220821',
  563. 'uploader': 'NBC 4',
  564. 'uploader_id': 'KNBC',
  565. 'channel': 'nbclosangeles',
  566. },
  567. }, {
  568. 'url': 'https://www.telemundoarizona.com/responde/huracan-complica-reembolso-para-televidente-de-tucson/2247002/',
  569. 'md5': '0917dcf7885be1023a9220630d415f67',
  570. 'info_dict': {
  571. 'id': '2247002',
  572. 'ext': 'mp4',
  573. 'title': 'Huracán complica que televidente de Tucson reciba reembolso',
  574. 'description': 'md5:af298dc73aab74d4fca6abfb12acb6cf',
  575. 'timestamp': 1660886507,
  576. 'upload_date': '20220819',
  577. 'uploader': 'Telemundo Arizona',
  578. 'uploader_id': 'KTAZ',
  579. 'channel': 'telemundoarizona',
  580. },
  581. }]
  582. _RESOLUTIONS = {
  583. '1080': '1920',
  584. '720': '1280',
  585. '540': '960',
  586. '360': '640',
  587. '234': '416',
  588. }
  589. def _real_extract(self, url):
  590. channel, video_id = self._match_valid_url(url).group('site', 'id')
  591. webpage = self._download_webpage(url, video_id)
  592. nbc_data = self._search_json(
  593. r'<script>var\s*nbc\s*=', webpage, 'NBC JSON data', video_id)
  594. pdk_acct = nbc_data.get('pdkAcct') or 'Yh1nAC'
  595. fw_ssid = traverse_obj(nbc_data, ('video', 'fwSSID'))
  596. fw_network_id = traverse_obj(nbc_data, ('video', 'fwNetworkID'), default='382114')
  597. video_data = self._parse_json(self._html_search_regex(
  598. r'data-videos="([^"]*)"', webpage, 'video data', default='{}'), video_id)
  599. video_data = variadic(video_data)[0]
  600. video_data.update(self._parse_json(self._html_search_regex(
  601. r'data-meta="([^"]*)"', webpage, 'metadata', default='{}'), video_id))
  602. formats = []
  603. if video_data.get('mpx_is_livestream') == '1':
  604. live = True
  605. player_id = traverse_obj(
  606. video_data, 'mpx_m3upid', ('video', 'meta', 'mpx_m3upid'), 'mpx_pid',
  607. ('video', 'meta', 'mpx_pid'), 'pid_streaming_web_medium')
  608. query = {
  609. 'mbr': 'true',
  610. 'assetTypes': 'LegacyRelease',
  611. 'fwsitesection': fw_ssid,
  612. 'fwNetworkID': fw_network_id,
  613. 'pprofile': 'ots_desktop_html',
  614. 'sensitive': 'false',
  615. 'w': '1920',
  616. 'h': '1080',
  617. 'rnd': '1660303',
  618. 'mode': 'LIVE',
  619. 'format': 'SMIL',
  620. 'tracking': 'true',
  621. 'formats': 'M3U+none,MPEG-DASH+none,MPEG4,MP3',
  622. 'vpaid': 'script',
  623. 'schema': '2.0',
  624. 'SDK': 'PDK+6.1.3',
  625. }
  626. info = {
  627. 'title': f'{channel} livestream',
  628. }
  629. else:
  630. live = False
  631. player_id = traverse_obj(
  632. video_data, ('video', 'meta', 'pid_streaming_web_high'), 'pid_streaming_web_high',
  633. ('video', 'meta', 'mpx_pid'), 'mpx_pid')
  634. date_string = traverse_obj(video_data, 'date_string', 'date_gmt')
  635. if date_string:
  636. date_string = self._search_regex(
  637. r'datetime="([^"]+)"', date_string, 'date string', fatal=False)
  638. else:
  639. date_string = traverse_obj(
  640. nbc_data, ('dataLayer', 'adobe', 'prop70'), ('dataLayer', 'adobe', 'eVar70'),
  641. ('dataLayer', 'adobe', 'eVar59'))
  642. video_url = traverse_obj(video_data, ('video', 'meta', 'mp4_url'), 'mp4_url')
  643. if video_url:
  644. height = url_basename(video_url).split('-')[1].split('p')[0]
  645. formats.append({
  646. 'url': video_url,
  647. 'ext': 'mp4',
  648. 'width': int_or_none(self._RESOLUTIONS.get(height)),
  649. 'height': int_or_none(height),
  650. 'format_id': f'http-{height}',
  651. })
  652. query = {
  653. 'mbr': 'true',
  654. 'assetTypes': 'LegacyRelease',
  655. 'fwsitesection': fw_ssid,
  656. 'fwNetworkID': fw_network_id,
  657. 'format': 'redirect',
  658. 'manifest': 'm3u',
  659. 'Tracking': 'true',
  660. 'Embedded': 'true',
  661. 'formats': 'MPEG4',
  662. }
  663. info = {
  664. 'title': video_data.get('title') or traverse_obj(
  665. nbc_data, ('dataLayer', 'contenttitle'), ('dataLayer', 'title'),
  666. ('dataLayer', 'adobe', 'prop22'), ('dataLayer', 'id')),
  667. 'description': traverse_obj(video_data, 'summary', 'excerpt', 'video_hero_text'),
  668. 'upload_date': str_or_none(unified_strdate(date_string)),
  669. 'timestamp': int_or_none(unified_timestamp(date_string)),
  670. }
  671. if not player_id:
  672. raise ExtractorError(
  673. 'No video player ID or livestream player ID found in webpage', expected=True)
  674. headers = {'Origin': f'https://www.{channel}.com'}
  675. manifest, urlh = self._download_webpage_handle(
  676. f'https://link.theplatform.com/s/{pdk_acct}/{player_id}', video_id,
  677. headers=headers, query=query, note='Downloading manifest')
  678. if live:
  679. manifest_url = self._search_regex(r'<video src="([^"]*)', manifest, 'manifest URL')
  680. else:
  681. manifest_url = urlh.geturl()
  682. formats.extend(self._extract_m3u8_formats(
  683. manifest_url, video_id, 'mp4', headers=headers, m3u8_id='hls',
  684. fatal=live, live=live, errnote='No HLS formats found'))
  685. return {
  686. 'id': str_or_none(video_id),
  687. 'channel': channel,
  688. 'uploader': str_or_none(nbc_data.get('on_air_name')),
  689. 'uploader_id': str_or_none(nbc_data.get('callLetters')),
  690. 'formats': formats,
  691. 'is_live': live,
  692. **info,
  693. }