nbc.py 8.3 KB

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