You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

234 lines
8.6 KiB

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