nbc.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. from __future__ import unicode_literals
  2. import re
  3. from .common import InfoExtractor
  4. from .theplatform import ThePlatformIE
  5. from ..utils import (
  6. find_xpath_attr,
  7. lowercase_escape,
  8. smuggle_url,
  9. unescapeHTML,
  10. update_url_query,
  11. int_or_none,
  12. HEADRequest,
  13. parse_iso8601,
  14. )
  15. class NBCIE(InfoExtractor):
  16. _VALID_URL = r'https?://www\.nbc\.com/(?:[^/]+/)+(?P<id>n?\d+)'
  17. _TESTS = [
  18. {
  19. 'url': 'http://www.nbc.com/the-tonight-show/segments/112966',
  20. 'info_dict': {
  21. 'id': '112966',
  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/the-tonight-show/episodes/176',
  36. 'info_dict': {
  37. 'id': '176',
  38. 'ext': 'flv',
  39. 'title': 'Ricky Gervais, Steven Van Zandt, ILoveMakonnen',
  40. 'description': 'A brand new episode of The Tonight Show welcomes Ricky Gervais, Steven Van Zandt and ILoveMakonnen.',
  41. },
  42. 'skip': '404 Not Found',
  43. },
  44. {
  45. 'url': 'http://www.nbc.com/saturday-night-live/video/star-wars-teaser/2832821',
  46. 'info_dict': {
  47. 'id': '2832821',
  48. 'ext': 'mp4',
  49. 'title': 'Star Wars Teaser',
  50. 'description': 'md5:0b40f9cbde5b671a7ff62fceccc4f442',
  51. 'timestamp': 1417852800,
  52. 'upload_date': '20141206',
  53. 'uploader': 'NBCU-COM',
  54. },
  55. 'params': {
  56. # m3u8 download
  57. 'skip_download': True,
  58. },
  59. 'skip': 'Only works from US',
  60. },
  61. {
  62. # This video has expired but with an escaped embedURL
  63. 'url': 'http://www.nbc.com/parenthood/episode-guide/season-5/just-like-at-home/515',
  64. 'only_matching': True,
  65. }
  66. ]
  67. def _real_extract(self, url):
  68. video_id = self._match_id(url)
  69. webpage = self._download_webpage(url, video_id)
  70. theplatform_url = unescapeHTML(lowercase_escape(self._html_search_regex(
  71. [
  72. r'(?:class="video-player video-player-full" data-mpx-url|class="player" src)="(.*?)"',
  73. r'<iframe[^>]+src="((?:https?:)?//player\.theplatform\.com/[^"]+)"',
  74. r'"embedURL"\s*:\s*"([^"]+)"'
  75. ],
  76. webpage, 'theplatform url').replace('_no_endcard', '').replace('\\/', '/')))
  77. if theplatform_url.startswith('//'):
  78. theplatform_url = 'http:' + theplatform_url
  79. return {
  80. '_type': 'url_transparent',
  81. 'ie_key': 'ThePlatform',
  82. 'url': smuggle_url(theplatform_url, {'source_url': url}),
  83. 'id': video_id,
  84. }
  85. class NBCSportsVPlayerIE(InfoExtractor):
  86. _VALID_URL = r'https?://vplayer\.nbcsports\.com/(?:[^/]+/)+(?P<id>[0-9a-zA-Z_]+)'
  87. _TESTS = [{
  88. 'url': 'https://vplayer.nbcsports.com/p/BxmELC/nbcsports_share/select/9CsDKds0kvHI',
  89. 'info_dict': {
  90. 'id': '9CsDKds0kvHI',
  91. 'ext': 'flv',
  92. 'description': 'md5:df390f70a9ba7c95ff1daace988f0d8d',
  93. 'title': 'Tyler Kalinoski hits buzzer-beater to lift Davidson',
  94. 'timestamp': 1426270238,
  95. 'upload_date': '20150313',
  96. 'uploader': 'NBCU-SPORTS',
  97. }
  98. }, {
  99. 'url': 'http://vplayer.nbcsports.com/p/BxmELC/nbc_embedshare/select/_hqLjQ95yx8Z',
  100. 'only_matching': True,
  101. }]
  102. @staticmethod
  103. def _extract_url(webpage):
  104. iframe_m = re.search(
  105. r'<iframe[^>]+src="(?P<url>https?://vplayer\.nbcsports\.com/[^"]+)"', webpage)
  106. if iframe_m:
  107. return iframe_m.group('url')
  108. def _real_extract(self, url):
  109. video_id = self._match_id(url)
  110. webpage = self._download_webpage(url, video_id)
  111. theplatform_url = self._og_search_video_url(webpage)
  112. return self.url_result(theplatform_url, 'ThePlatform')
  113. class NBCSportsIE(InfoExtractor):
  114. # Does not include https because its certificate is invalid
  115. _VALID_URL = r'https?://www\.nbcsports\.com//?(?:[^/]+/)+(?P<id>[0-9a-z-]+)'
  116. _TEST = {
  117. 'url': 'http://www.nbcsports.com//college-basketball/ncaab/tom-izzo-michigan-st-has-so-much-respect-duke',
  118. 'info_dict': {
  119. 'id': 'PHJSaFWbrTY9',
  120. 'ext': 'flv',
  121. 'title': 'Tom Izzo, Michigan St. has \'so much respect\' for Duke',
  122. 'description': 'md5:ecb459c9d59e0766ac9c7d5d0eda8113',
  123. }
  124. }
  125. def _real_extract(self, url):
  126. video_id = self._match_id(url)
  127. webpage = self._download_webpage(url, video_id)
  128. return self.url_result(
  129. NBCSportsVPlayerIE._extract_url(webpage), 'NBCSportsVPlayer')
  130. class CSNNEIE(InfoExtractor):
  131. _VALID_URL = r'https?://www\.csnne\.com/video/(?P<id>[0-9a-z-]+)'
  132. _TEST = {
  133. 'url': 'http://www.csnne.com/video/snc-evening-update-wright-named-red-sox-no-5-starter',
  134. 'info_dict': {
  135. 'id': 'yvBLLUgQ8WU0',
  136. 'ext': 'mp4',
  137. 'title': 'SNC evening update: Wright named Red Sox\' No. 5 starter.',
  138. 'description': 'md5:1753cfee40d9352b19b4c9b3e589b9e3',
  139. 'timestamp': 1459369979,
  140. 'upload_date': '20160330',
  141. 'uploader': 'NBCU-SPORTS',
  142. }
  143. }
  144. def _real_extract(self, url):
  145. display_id = self._match_id(url)
  146. webpage = self._download_webpage(url, display_id)
  147. return {
  148. '_type': 'url_transparent',
  149. 'ie_key': 'ThePlatform',
  150. 'url': self._html_search_meta('twitter:player:stream', webpage),
  151. 'display_id': display_id,
  152. }
  153. class NBCNewsIE(ThePlatformIE):
  154. _VALID_URL = r'''(?x)https?://(?:www\.)?(?:nbcnews|today)\.com/
  155. (?:video/.+?/(?P<id>\d+)|
  156. ([^/]+/)*(?P<display_id>[^/?]+))
  157. '''
  158. _TESTS = [
  159. {
  160. 'url': 'http://www.nbcnews.com/video/nbc-news/52753292',
  161. 'md5': '47abaac93c6eaf9ad37ee6c4463a5179',
  162. 'info_dict': {
  163. 'id': '52753292',
  164. 'ext': 'flv',
  165. 'title': 'Crew emerges after four-month Mars food study',
  166. 'description': 'md5:24e632ffac72b35f8b67a12d1b6ddfc1',
  167. },
  168. },
  169. {
  170. 'url': 'http://www.nbcnews.com/watch/nbcnews-com/how-twitter-reacted-to-the-snowden-interview-269389891880',
  171. 'md5': 'af1adfa51312291a017720403826bb64',
  172. 'info_dict': {
  173. 'id': '269389891880',
  174. 'ext': 'mp4',
  175. 'title': 'How Twitter Reacted To The Snowden Interview',
  176. 'description': 'md5:65a0bd5d76fe114f3c2727aa3a81fe64',
  177. },
  178. },
  179. {
  180. 'url': 'http://www.nbcnews.com/feature/dateline-full-episodes/full-episode-family-business-n285156',
  181. 'md5': 'fdbf39ab73a72df5896b6234ff98518a',
  182. 'info_dict': {
  183. 'id': 'Wjf9EDR3A_60',
  184. 'ext': 'mp4',
  185. 'title': 'FULL EPISODE: Family Business',
  186. 'description': 'md5:757988edbaae9d7be1d585eb5d55cc04',
  187. },
  188. 'skip': 'This page is unavailable.',
  189. },
  190. {
  191. 'url': 'http://www.nbcnews.com/nightly-news/video/nightly-news-with-brian-williams-full-broadcast-february-4-394064451844',
  192. 'md5': '73135a2e0ef819107bbb55a5a9b2a802',
  193. 'info_dict': {
  194. 'id': '394064451844',
  195. 'ext': 'mp4',
  196. 'title': 'Nightly News with Brian Williams Full Broadcast (February 4)',
  197. 'description': 'md5:1c10c1eccbe84a26e5debb4381e2d3c5',
  198. },
  199. },
  200. {
  201. 'url': 'http://www.nbcnews.com/business/autos/volkswagen-11-million-vehicles-could-have-suspect-software-emissions-scandal-n431456',
  202. 'md5': 'a49e173825e5fcd15c13fc297fced39d',
  203. 'info_dict': {
  204. 'id': '529953347624',
  205. 'ext': 'mp4',
  206. 'title': 'Volkswagen U.S. Chief: We \'Totally Screwed Up\'',
  207. 'description': 'md5:d22d1281a24f22ea0880741bb4dd6301',
  208. },
  209. 'expected_warnings': ['http-6000 is not available']
  210. },
  211. {
  212. 'url': 'http://www.today.com/video/see-the-aurora-borealis-from-space-in-stunning-new-nasa-video-669831235788',
  213. 'md5': '118d7ca3f0bea6534f119c68ef539f71',
  214. 'info_dict': {
  215. 'id': '669831235788',
  216. 'ext': 'mp4',
  217. 'title': 'See the aurora borealis from space in stunning new NASA video',
  218. 'description': 'md5:74752b7358afb99939c5f8bb2d1d04b1',
  219. 'upload_date': '20160420',
  220. 'timestamp': 1461152093,
  221. },
  222. },
  223. {
  224. 'url': 'http://www.nbcnews.com/watch/dateline/full-episode--deadly-betrayal-386250819952',
  225. 'only_matching': True,
  226. },
  227. ]
  228. def _real_extract(self, url):
  229. mobj = re.match(self._VALID_URL, url)
  230. video_id = mobj.group('id')
  231. if video_id is not None:
  232. all_info = self._download_xml('http://www.nbcnews.com/id/%s/displaymode/1219' % video_id, video_id)
  233. info = all_info.find('video')
  234. return {
  235. 'id': video_id,
  236. 'title': info.find('headline').text,
  237. 'ext': 'flv',
  238. 'url': find_xpath_attr(info, 'media', 'type', 'flashVideo').text,
  239. 'description': info.find('caption').text,
  240. 'thumbnail': find_xpath_attr(info, 'media', 'type', 'thumbnail').text,
  241. }
  242. else:
  243. # "feature" and "nightly-news" pages use theplatform.com
  244. display_id = mobj.group('display_id')
  245. webpage = self._download_webpage(url, display_id)
  246. info = None
  247. bootstrap_json = self._search_regex(
  248. r'(?m)var\s+(?:bootstrapJson|playlistData)\s*=\s*({.+});?\s*$',
  249. webpage, 'bootstrap json', default=None)
  250. if bootstrap_json:
  251. bootstrap = self._parse_json(bootstrap_json, display_id)
  252. info = bootstrap['results'][0]['video']
  253. else:
  254. player_instance_json = self._search_regex(
  255. r'videoObj\s*:\s*({.+})', webpage, 'player instance', default=None)
  256. if not player_instance_json:
  257. player_instance_json = self._html_search_regex(
  258. r'data-video="([^"]+)"', webpage, 'video json')
  259. info = self._parse_json(player_instance_json, display_id)
  260. video_id = info['mpxId']
  261. title = info['title']
  262. subtitles = {}
  263. caption_links = info.get('captionLinks')
  264. if caption_links:
  265. for (sub_key, sub_ext) in (('smpte-tt', 'ttml'), ('web-vtt', 'vtt'), ('srt', 'srt')):
  266. sub_url = caption_links.get(sub_key)
  267. if sub_url:
  268. subtitles.setdefault('en', []).append({
  269. 'url': sub_url,
  270. 'ext': sub_ext,
  271. })
  272. formats = []
  273. for video_asset in info['videoAssets']:
  274. video_url = video_asset.get('publicUrl')
  275. if not video_url:
  276. continue
  277. container = video_asset.get('format')
  278. asset_type = video_asset.get('assetType') or ''
  279. if container == 'ISM' or asset_type == 'FireTV-Once':
  280. continue
  281. elif asset_type == 'OnceURL':
  282. tp_formats, tp_subtitles = self._extract_theplatform_smil(
  283. video_url, video_id)
  284. formats.extend(tp_formats)
  285. subtitles = self._merge_subtitles(subtitles, tp_subtitles)
  286. else:
  287. tbr = int_or_none(video_asset.get('bitRate') or video_asset.get('bitrate'), 1000)
  288. format_id = 'http%s' % ('-%d' % tbr if tbr else '')
  289. video_url = update_url_query(
  290. video_url, {'format': 'redirect'})
  291. # resolve the url so that we can check availability and detect the correct extension
  292. head = self._request_webpage(
  293. HEADRequest(video_url), video_id,
  294. 'Checking %s url' % format_id,
  295. '%s is not available' % format_id,
  296. fatal=False)
  297. if head:
  298. video_url = head.geturl()
  299. formats.append({
  300. 'format_id': format_id,
  301. 'url': video_url,
  302. 'width': int_or_none(video_asset.get('width')),
  303. 'height': int_or_none(video_asset.get('height')),
  304. 'tbr': tbr,
  305. 'container': video_asset.get('format'),
  306. })
  307. self._sort_formats(formats)
  308. return {
  309. 'id': video_id,
  310. 'title': title,
  311. 'description': info.get('description'),
  312. 'thumbnail': info.get('thumbnail'),
  313. 'duration': int_or_none(info.get('duration')),
  314. 'timestamp': parse_iso8601(info.get('pubDate') or info.get('pub_date')),
  315. 'formats': formats,
  316. 'subtitles': subtitles,
  317. }
  318. class MSNBCIE(InfoExtractor):
  319. # https URLs redirect to corresponding http ones
  320. _VALID_URL = r'https?://www\.msnbc\.com/[^/]+/watch/(?P<id>[^/]+)'
  321. _TEST = {
  322. 'url': 'http://www.msnbc.com/all-in-with-chris-hayes/watch/the-chaotic-gop-immigration-vote-314487875924',
  323. 'md5': '6d236bf4f3dddc226633ce6e2c3f814d',
  324. 'info_dict': {
  325. 'id': 'n_hayes_Aimm_140801_272214',
  326. 'ext': 'mp4',
  327. 'title': 'The chaotic GOP immigration vote',
  328. '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.',
  329. 'thumbnail': 're:^https?://.*\.jpg$',
  330. 'timestamp': 1406937606,
  331. 'upload_date': '20140802',
  332. 'uploader': 'NBCU-NEWS',
  333. 'categories': ['MSNBC/Topics/Franchise/Best of last night', 'MSNBC/Topics/General/Congress'],
  334. },
  335. }
  336. def _real_extract(self, url):
  337. video_id = self._match_id(url)
  338. webpage = self._download_webpage(url, video_id)
  339. embed_url = self._html_search_meta('embedURL', webpage)
  340. return self.url_result(embed_url)