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.

163 lines
6.0 KiB

11 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. )
  12. class NBCIE(InfoExtractor):
  13. _VALID_URL = r'http://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 NBCNewsIE(InfoExtractor):
  46. _VALID_URL = r'''(?x)https?://(?:www\.)?nbcnews\.com/
  47. (?:video/.+?/(?P<id>\d+)|
  48. (?:feature|nightly-news)/[^/]+/(?P<title>.+))
  49. '''
  50. _TESTS = [
  51. {
  52. 'url': 'http://www.nbcnews.com/video/nbc-news/52753292',
  53. 'md5': '47abaac93c6eaf9ad37ee6c4463a5179',
  54. 'info_dict': {
  55. 'id': '52753292',
  56. 'ext': 'flv',
  57. 'title': 'Crew emerges after four-month Mars food study',
  58. 'description': 'md5:24e632ffac72b35f8b67a12d1b6ddfc1',
  59. },
  60. },
  61. {
  62. 'url': 'http://www.nbcnews.com/feature/edward-snowden-interview/how-twitter-reacted-snowden-interview-n117236',
  63. 'md5': 'b2421750c9f260783721d898f4c42063',
  64. 'info_dict': {
  65. 'id': 'I1wpAI_zmhsQ',
  66. 'ext': 'mp4',
  67. 'title': 'How Twitter Reacted To The Snowden Interview',
  68. 'description': 'md5:65a0bd5d76fe114f3c2727aa3a81fe64',
  69. },
  70. 'add_ie': ['ThePlatform'],
  71. },
  72. {
  73. 'url': 'http://www.nbcnews.com/feature/dateline-full-episodes/full-episode-family-business-n285156',
  74. 'md5': 'fdbf39ab73a72df5896b6234ff98518a',
  75. 'info_dict': {
  76. 'id': 'Wjf9EDR3A_60',
  77. 'ext': 'mp4',
  78. 'title': 'FULL EPISODE: Family Business',
  79. 'description': 'md5:757988edbaae9d7be1d585eb5d55cc04',
  80. },
  81. },
  82. {
  83. 'url': 'http://www.nbcnews.com/nightly-news/video/nightly-news-with-brian-williams-full-broadcast-february-4-394064451844',
  84. 'md5': 'b5dda8cddd8650baa0dcb616dd2cf60d',
  85. 'info_dict': {
  86. 'id': 'sekXqyTVnmN3',
  87. 'ext': 'mp4',
  88. 'title': 'Nightly News with Brian Williams Full Broadcast (February 4)',
  89. 'description': 'md5:1c10c1eccbe84a26e5debb4381e2d3c5',
  90. },
  91. },
  92. ]
  93. def _real_extract(self, url):
  94. mobj = re.match(self._VALID_URL, url)
  95. video_id = mobj.group('id')
  96. if video_id is not None:
  97. all_info = self._download_xml('http://www.nbcnews.com/id/%s/displaymode/1219' % video_id, video_id)
  98. info = all_info.find('video')
  99. return {
  100. 'id': video_id,
  101. 'title': info.find('headline').text,
  102. 'ext': 'flv',
  103. 'url': find_xpath_attr(info, 'media', 'type', 'flashVideo').text,
  104. 'description': compat_str(info.find('caption').text),
  105. 'thumbnail': find_xpath_attr(info, 'media', 'type', 'thumbnail').text,
  106. }
  107. else:
  108. # "feature" and "nightly-news" pages use theplatform.com
  109. title = mobj.group('title')
  110. webpage = self._download_webpage(url, title)
  111. bootstrap_json = self._search_regex(
  112. r'var\s+(?:bootstrapJson|playlistData)\s*=\s*({.+});?\s*$',
  113. webpage, 'bootstrap json', flags=re.MULTILINE)
  114. bootstrap = self._parse_json(bootstrap_json, video_id)
  115. info = bootstrap['results'][0]['video']
  116. mpxid = info['mpxId']
  117. base_urls = [
  118. info['fallbackPlaylistUrl'],
  119. info['associatedPlaylistUrl'],
  120. ]
  121. for base_url in base_urls:
  122. if not base_url:
  123. continue
  124. playlist_url = base_url + '?form=MPXNBCNewsAPI'
  125. try:
  126. all_videos = self._download_json(playlist_url, title)
  127. except ExtractorError as ee:
  128. if isinstance(ee.cause, compat_HTTPError):
  129. continue
  130. raise
  131. if not all_videos or 'videos' not in all_videos:
  132. continue
  133. try:
  134. info = next(v for v in all_videos['videos'] if v['mpxId'] == mpxid)
  135. break
  136. except StopIteration:
  137. continue
  138. if info is None:
  139. raise ExtractorError('Could not find video in playlists')
  140. return {
  141. '_type': 'url',
  142. # We get the best quality video
  143. 'url': info['videoAssets'][-1]['publicUrl'],
  144. 'ie_key': 'ThePlatform',
  145. }