nbc.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. def _real_extract(self, url):
  37. video_id = self._match_id(url)
  38. webpage = self._download_webpage(url, video_id)
  39. theplatform_url = self._search_regex(
  40. '(?:class="video-player video-player-full" data-mpx-url|class="player" src)="(.*?)"',
  41. webpage, 'theplatform url').replace('_no_endcard', '')
  42. if theplatform_url.startswith('//'):
  43. theplatform_url = 'http:' + theplatform_url
  44. return self.url_result(theplatform_url)
  45. class NBCSportsVPlayerIE(InfoExtractor):
  46. _VALID_URL = r'https?://vplayer\.nbcsports\.com/(?:[^/]+/)+(?P<id>[0-9a-zA-Z]+)'
  47. _TEST = {
  48. 'url': 'https://vplayer.nbcsports.com/p/BxmELC/nbcsports_share/select/9CsDKds0kvHI',
  49. 'md5': 'ceae8dced5c14a1c1ffcb7a32194cca5',
  50. 'info_dict': {
  51. 'id': '9CsDKds0kvHI',
  52. 'ext': 'flv',
  53. 'description': 'md5:df390f70a9ba7c95ff1daace988f0d8d',
  54. 'title': 'Tyler Kalinoski hits buzzer-beater to lift Davidson',
  55. }
  56. }
  57. @staticmethod
  58. def _extract_url(webpage):
  59. iframe_m = re.search(
  60. r'<iframe[^>]+src="(?P<url>https?://vplayer\.nbcsports\.com/[^"]+)"', webpage)
  61. if iframe_m:
  62. return iframe_m.group('url')
  63. def _real_extract(self, url):
  64. video_id = self._match_id(url)
  65. webpage = self._download_webpage(url, video_id)
  66. theplatform_url = self._og_search_video_url(webpage)
  67. return self.url_result(theplatform_url, 'ThePlatform')
  68. class NBCSportsIE(InfoExtractor):
  69. # Does not include https becuase its certificate is invalid
  70. _VALID_URL = r'http://www\.nbcsports\.com//?(?:[^/]+/)+(?P<id>[0-9a-z-]+)'
  71. _TEST = {
  72. 'url': 'http://www.nbcsports.com//college-basketball/ncaab/tom-izzo-michigan-st-has-so-much-respect-duke',
  73. 'md5': 'ba6c93f96b67bf05344f78bd523dac0f',
  74. 'info_dict': {
  75. 'id': 'PHJSaFWbrTY9',
  76. 'ext': 'flv',
  77. 'title': 'Tom Izzo, Michigan St. has \'so much respect\' for Duke',
  78. 'description': 'md5:ecb459c9d59e0766ac9c7d5d0eda8113',
  79. }
  80. }
  81. def _real_extract(self, url):
  82. video_id = self._match_id(url)
  83. webpage = self._download_webpage(url, video_id)
  84. return self.url_result(
  85. NBCSportsVPlayerIE._extract_url(webpage), 'NBCSportsVPlayer')
  86. class NBCNewsIE(InfoExtractor):
  87. _VALID_URL = r'''(?x)https?://(?:www\.)?nbcnews\.com/
  88. (?:video/.+?/(?P<id>\d+)|
  89. (?:feature|nightly-news)/[^/]+/(?P<title>.+))
  90. '''
  91. _TESTS = [
  92. {
  93. 'url': 'http://www.nbcnews.com/video/nbc-news/52753292',
  94. 'md5': '47abaac93c6eaf9ad37ee6c4463a5179',
  95. 'info_dict': {
  96. 'id': '52753292',
  97. 'ext': 'flv',
  98. 'title': 'Crew emerges after four-month Mars food study',
  99. 'description': 'md5:24e632ffac72b35f8b67a12d1b6ddfc1',
  100. },
  101. },
  102. {
  103. 'url': 'http://www.nbcnews.com/feature/edward-snowden-interview/how-twitter-reacted-snowden-interview-n117236',
  104. 'md5': 'b2421750c9f260783721d898f4c42063',
  105. 'info_dict': {
  106. 'id': 'I1wpAI_zmhsQ',
  107. 'ext': 'mp4',
  108. 'title': 'How Twitter Reacted To The Snowden Interview',
  109. 'description': 'md5:65a0bd5d76fe114f3c2727aa3a81fe64',
  110. },
  111. 'add_ie': ['ThePlatform'],
  112. },
  113. {
  114. 'url': 'http://www.nbcnews.com/feature/dateline-full-episodes/full-episode-family-business-n285156',
  115. 'md5': 'fdbf39ab73a72df5896b6234ff98518a',
  116. 'info_dict': {
  117. 'id': 'Wjf9EDR3A_60',
  118. 'ext': 'mp4',
  119. 'title': 'FULL EPISODE: Family Business',
  120. 'description': 'md5:757988edbaae9d7be1d585eb5d55cc04',
  121. },
  122. },
  123. {
  124. 'url': 'http://www.nbcnews.com/nightly-news/video/nightly-news-with-brian-williams-full-broadcast-february-4-394064451844',
  125. 'md5': 'b5dda8cddd8650baa0dcb616dd2cf60d',
  126. 'info_dict': {
  127. 'id': 'sekXqyTVnmN3',
  128. 'ext': 'mp4',
  129. 'title': 'Nightly News with Brian Williams Full Broadcast (February 4)',
  130. 'description': 'md5:1c10c1eccbe84a26e5debb4381e2d3c5',
  131. },
  132. },
  133. ]
  134. def _real_extract(self, url):
  135. mobj = re.match(self._VALID_URL, url)
  136. video_id = mobj.group('id')
  137. if video_id is not None:
  138. all_info = self._download_xml('http://www.nbcnews.com/id/%s/displaymode/1219' % video_id, video_id)
  139. info = all_info.find('video')
  140. return {
  141. 'id': video_id,
  142. 'title': info.find('headline').text,
  143. 'ext': 'flv',
  144. 'url': find_xpath_attr(info, 'media', 'type', 'flashVideo').text,
  145. 'description': compat_str(info.find('caption').text),
  146. 'thumbnail': find_xpath_attr(info, 'media', 'type', 'thumbnail').text,
  147. }
  148. else:
  149. # "feature" and "nightly-news" pages use theplatform.com
  150. title = mobj.group('title')
  151. webpage = self._download_webpage(url, title)
  152. bootstrap_json = self._search_regex(
  153. r'var\s+(?:bootstrapJson|playlistData)\s*=\s*({.+});?\s*$',
  154. webpage, 'bootstrap json', flags=re.MULTILINE)
  155. bootstrap = self._parse_json(bootstrap_json, video_id)
  156. info = bootstrap['results'][0]['video']
  157. mpxid = info['mpxId']
  158. base_urls = [
  159. info['fallbackPlaylistUrl'],
  160. info['associatedPlaylistUrl'],
  161. ]
  162. for base_url in base_urls:
  163. if not base_url:
  164. continue
  165. playlist_url = base_url + '?form=MPXNBCNewsAPI'
  166. try:
  167. all_videos = self._download_json(playlist_url, title)
  168. except ExtractorError as ee:
  169. if isinstance(ee.cause, compat_HTTPError):
  170. continue
  171. raise
  172. if not all_videos or 'videos' not in all_videos:
  173. continue
  174. try:
  175. info = next(v for v in all_videos['videos'] if v['mpxId'] == mpxid)
  176. break
  177. except StopIteration:
  178. continue
  179. if info is None:
  180. raise ExtractorError('Could not find video in playlists')
  181. return {
  182. '_type': 'url',
  183. # We get the best quality video
  184. 'url': info['videoAssets'][-1]['publicUrl'],
  185. 'ie_key': 'ThePlatform',
  186. }