nbc.py 8.0 KB

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