nbc.py 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. from __future__ import unicode_literals
  2. import re
  3. from .common import InfoExtractor
  4. from ..compat import (
  5. compat_str,
  6. compat_HTTPError,
  7. )
  8. from ..utils import (
  9. ExtractorError,
  10. find_xpath_attr,
  11. lowercase_escape,
  12. smuggle_url,
  13. unescapeHTML,
  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. # md5 checksum is not stable
  21. 'info_dict': {
  22. 'id': 'c9xnCo0YPOPH',
  23. 'ext': 'flv',
  24. 'title': 'Jimmy Fallon Surprises Fans at Ben & Jerry\'s',
  25. 'description': 'Jimmy gives out free scoops of his new "Tonight Dough" ice cream flavor by surprising customers at the Ben & Jerry\'s scoop shop.',
  26. },
  27. },
  28. {
  29. 'url': 'http://www.nbc.com/the-tonight-show/episodes/176',
  30. 'info_dict': {
  31. 'id': 'XwU9KZkp98TH',
  32. 'ext': 'flv',
  33. 'title': 'Ricky Gervais, Steven Van Zandt, ILoveMakonnen',
  34. 'description': 'A brand new episode of The Tonight Show welcomes Ricky Gervais, Steven Van Zandt and ILoveMakonnen.',
  35. },
  36. 'skip': 'Only works from US',
  37. },
  38. {
  39. 'url': 'http://www.nbc.com/saturday-night-live/video/star-wars-teaser/2832821',
  40. 'info_dict': {
  41. 'id': '8iUuyzWDdYUZ',
  42. 'ext': 'flv',
  43. 'title': 'Star Wars Teaser',
  44. 'description': 'md5:0b40f9cbde5b671a7ff62fceccc4f442',
  45. },
  46. 'skip': 'Only works from US',
  47. },
  48. {
  49. # This video has expired but with an escaped embedURL
  50. 'url': 'http://www.nbc.com/parenthood/episode-guide/season-5/just-like-at-home/515',
  51. 'skip': 'Expired'
  52. }
  53. ]
  54. def _real_extract(self, url):
  55. video_id = self._match_id(url)
  56. webpage = self._download_webpage(url, video_id)
  57. theplatform_url = unescapeHTML(lowercase_escape(self._html_search_regex(
  58. [
  59. r'(?:class="video-player video-player-full" data-mpx-url|class="player" src)="(.*?)"',
  60. r'"embedURL"\s*:\s*"([^"]+)"'
  61. ],
  62. webpage, 'theplatform url').replace('_no_endcard', '').replace('\\/', '/')))
  63. if theplatform_url.startswith('//'):
  64. theplatform_url = 'http:' + theplatform_url
  65. return self.url_result(smuggle_url(theplatform_url, {'source_url': url}))
  66. class NBCSportsVPlayerIE(InfoExtractor):
  67. _VALID_URL = r'https?://vplayer\.nbcsports\.com/(?:[^/]+/)+(?P<id>[0-9a-zA-Z_]+)'
  68. _TESTS = [{
  69. 'url': 'https://vplayer.nbcsports.com/p/BxmELC/nbcsports_share/select/9CsDKds0kvHI',
  70. 'info_dict': {
  71. 'id': '9CsDKds0kvHI',
  72. 'ext': 'flv',
  73. 'description': 'md5:df390f70a9ba7c95ff1daace988f0d8d',
  74. 'title': 'Tyler Kalinoski hits buzzer-beater to lift Davidson',
  75. }
  76. }, {
  77. 'url': 'http://vplayer.nbcsports.com/p/BxmELC/nbc_embedshare/select/_hqLjQ95yx8Z',
  78. 'only_matching': True,
  79. }]
  80. @staticmethod
  81. def _extract_url(webpage):
  82. iframe_m = re.search(
  83. r'<iframe[^>]+src="(?P<url>https?://vplayer\.nbcsports\.com/[^"]+)"', webpage)
  84. if iframe_m:
  85. return iframe_m.group('url')
  86. def _real_extract(self, url):
  87. video_id = self._match_id(url)
  88. webpage = self._download_webpage(url, video_id)
  89. theplatform_url = self._og_search_video_url(webpage)
  90. return self.url_result(theplatform_url, 'ThePlatform')
  91. class NBCSportsIE(InfoExtractor):
  92. # Does not include https becuase its certificate is invalid
  93. _VALID_URL = r'http://www\.nbcsports\.com//?(?:[^/]+/)+(?P<id>[0-9a-z-]+)'
  94. _TEST = {
  95. 'url': 'http://www.nbcsports.com//college-basketball/ncaab/tom-izzo-michigan-st-has-so-much-respect-duke',
  96. 'info_dict': {
  97. 'id': 'PHJSaFWbrTY9',
  98. 'ext': 'flv',
  99. 'title': 'Tom Izzo, Michigan St. has \'so much respect\' for Duke',
  100. 'description': 'md5:ecb459c9d59e0766ac9c7d5d0eda8113',
  101. }
  102. }
  103. def _real_extract(self, url):
  104. video_id = self._match_id(url)
  105. webpage = self._download_webpage(url, video_id)
  106. return self.url_result(
  107. NBCSportsVPlayerIE._extract_url(webpage), 'NBCSportsVPlayer')
  108. class NBCNewsIE(InfoExtractor):
  109. _VALID_URL = r'''(?x)https?://(?:www\.)?nbcnews\.com/
  110. (?:video/.+?/(?P<id>\d+)|
  111. (?:watch|feature|nightly-news)/[^/]+/(?P<title>.+))
  112. '''
  113. _TESTS = [
  114. {
  115. 'url': 'http://www.nbcnews.com/video/nbc-news/52753292',
  116. 'md5': '47abaac93c6eaf9ad37ee6c4463a5179',
  117. 'info_dict': {
  118. 'id': '52753292',
  119. 'ext': 'flv',
  120. 'title': 'Crew emerges after four-month Mars food study',
  121. 'description': 'md5:24e632ffac72b35f8b67a12d1b6ddfc1',
  122. },
  123. },
  124. {
  125. 'url': 'http://www.nbcnews.com/feature/edward-snowden-interview/how-twitter-reacted-snowden-interview-n117236',
  126. 'md5': 'b2421750c9f260783721d898f4c42063',
  127. 'info_dict': {
  128. 'id': 'I1wpAI_zmhsQ',
  129. 'ext': 'mp4',
  130. 'title': 'How Twitter Reacted To The Snowden Interview',
  131. 'description': 'md5:65a0bd5d76fe114f3c2727aa3a81fe64',
  132. },
  133. 'add_ie': ['ThePlatform'],
  134. },
  135. {
  136. 'url': 'http://www.nbcnews.com/feature/dateline-full-episodes/full-episode-family-business-n285156',
  137. 'md5': 'fdbf39ab73a72df5896b6234ff98518a',
  138. 'info_dict': {
  139. 'id': 'Wjf9EDR3A_60',
  140. 'ext': 'mp4',
  141. 'title': 'FULL EPISODE: Family Business',
  142. 'description': 'md5:757988edbaae9d7be1d585eb5d55cc04',
  143. },
  144. },
  145. {
  146. 'url': 'http://www.nbcnews.com/nightly-news/video/nightly-news-with-brian-williams-full-broadcast-february-4-394064451844',
  147. 'md5': 'b5dda8cddd8650baa0dcb616dd2cf60d',
  148. 'info_dict': {
  149. 'id': 'sekXqyTVnmN3',
  150. 'ext': 'mp4',
  151. 'title': 'Nightly News with Brian Williams Full Broadcast (February 4)',
  152. 'description': 'md5:1c10c1eccbe84a26e5debb4381e2d3c5',
  153. },
  154. },
  155. {
  156. 'url': 'http://www.nbcnews.com/watch/dateline/full-episode--deadly-betrayal-386250819952',
  157. 'only_matching': True,
  158. },
  159. ]
  160. def _real_extract(self, url):
  161. mobj = re.match(self._VALID_URL, url)
  162. video_id = mobj.group('id')
  163. if video_id is not None:
  164. all_info = self._download_xml('http://www.nbcnews.com/id/%s/displaymode/1219' % video_id, video_id)
  165. info = all_info.find('video')
  166. return {
  167. 'id': video_id,
  168. 'title': info.find('headline').text,
  169. 'ext': 'flv',
  170. 'url': find_xpath_attr(info, 'media', 'type', 'flashVideo').text,
  171. 'description': compat_str(info.find('caption').text),
  172. 'thumbnail': find_xpath_attr(info, 'media', 'type', 'thumbnail').text,
  173. }
  174. else:
  175. # "feature" and "nightly-news" pages use theplatform.com
  176. title = mobj.group('title')
  177. webpage = self._download_webpage(url, title)
  178. bootstrap_json = self._search_regex(
  179. r'var\s+(?:bootstrapJson|playlistData)\s*=\s*({.+});?\s*$',
  180. webpage, 'bootstrap json', flags=re.MULTILINE)
  181. bootstrap = self._parse_json(bootstrap_json, video_id)
  182. info = bootstrap['results'][0]['video']
  183. mpxid = info['mpxId']
  184. base_urls = [
  185. info['fallbackPlaylistUrl'],
  186. info['associatedPlaylistUrl'],
  187. ]
  188. for base_url in base_urls:
  189. if not base_url:
  190. continue
  191. playlist_url = base_url + '?form=MPXNBCNewsAPI'
  192. try:
  193. all_videos = self._download_json(playlist_url, title)
  194. except ExtractorError as ee:
  195. if isinstance(ee.cause, compat_HTTPError):
  196. continue
  197. raise
  198. if not all_videos or 'videos' not in all_videos:
  199. continue
  200. try:
  201. info = next(v for v in all_videos['videos'] if v['mpxId'] == mpxid)
  202. break
  203. except StopIteration:
  204. continue
  205. if info is None:
  206. raise ExtractorError('Could not find video in playlists')
  207. return {
  208. '_type': 'url',
  209. # We get the best quality video
  210. 'url': info['videoAssets'][-1]['publicUrl'],
  211. 'ie_key': 'ThePlatform',
  212. }
  213. class MSNBCIE(InfoExtractor):
  214. # https URLs redirect to corresponding http ones
  215. _VALID_URL = r'http://www\.msnbc\.com/[^/]+/watch/(?P<id>[^/]+)'
  216. _TEST = {
  217. 'url': 'http://www.msnbc.com/all-in-with-chris-hayes/watch/the-chaotic-gop-immigration-vote-314487875924',
  218. 'md5': '6d236bf4f3dddc226633ce6e2c3f814d',
  219. 'info_dict': {
  220. 'id': 'n_hayes_Aimm_140801_272214',
  221. 'ext': 'mp4',
  222. 'title': 'The chaotic GOP immigration vote',
  223. '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.',
  224. 'thumbnail': 're:^https?://.*\.jpg$',
  225. 'timestamp': 1406937606,
  226. 'upload_date': '20140802',
  227. 'categories': ['MSNBC/Topics/Franchise/Best of last night', 'MSNBC/Topics/General/Congress'],
  228. },
  229. }
  230. def _real_extract(self, url):
  231. video_id = self._match_id(url)
  232. webpage = self._download_webpage(url, video_id)
  233. embed_url = self._html_search_meta('embedURL', webpage)
  234. return self.url_result(embed_url)