nbc.py 8.0 KB

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