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.

154 lines
5.4 KiB

11 years ago
  1. from __future__ import unicode_literals
  2. import re
  3. import json
  4. from .common import InfoExtractor
  5. from ..compat import (
  6. compat_str,
  7. compat_HTTPError,
  8. )
  9. from ..utils import (
  10. ExtractorError,
  11. find_xpath_attr,
  12. )
  13. class NBCIE(InfoExtractor):
  14. _VALID_URL = r'http://www\.nbc\.com/(?:[^/]+/)+(?P<id>n?\d+)'
  15. _TESTS = [
  16. {
  17. 'url': 'http://www.nbc.com/chicago-fire/video/i-am-a-firefighter/2734188',
  18. # md5 checksum is not stable
  19. 'info_dict': {
  20. 'id': 'bTmnLCvIbaaH',
  21. 'ext': 'flv',
  22. 'title': 'I Am a Firefighter',
  23. 'description': 'An emergency puts Dawson\'sf irefighter skills to the ultimate test in this four-part digital series.',
  24. },
  25. },
  26. {
  27. 'url': 'http://www.nbc.com/the-tonight-show/episodes/176',
  28. 'info_dict': {
  29. 'id': 'XwU9KZkp98TH',
  30. 'ext': 'flv',
  31. 'title': 'Ricky Gervais, Steven Van Zandt, ILoveMakonnen',
  32. 'description': 'A brand new episode of The Tonight Show welcomes Ricky Gervais, Steven Van Zandt and ILoveMakonnen.',
  33. },
  34. 'skip': 'Only works from US',
  35. },
  36. ]
  37. def _real_extract(self, url):
  38. video_id = self._match_id(url)
  39. webpage = self._download_webpage(url, video_id)
  40. theplatform_url = self._search_regex(
  41. '(?:class="video-player video-player-full" data-mpx-url|class="player" src)="(.*?)"',
  42. webpage, 'theplatform url').replace('_no_endcard', '')
  43. if theplatform_url.startswith('//'):
  44. theplatform_url = 'http:' + theplatform_url
  45. return self.url_result(theplatform_url)
  46. class NBCNewsIE(InfoExtractor):
  47. _VALID_URL = r'''(?x)https?://www\.nbcnews\.com/
  48. ((video/.+?/(?P<id>\d+))|
  49. (feature/[^/]+/(?P<title>.+)))
  50. '''
  51. _TESTS = [
  52. {
  53. 'url': 'http://www.nbcnews.com/video/nbc-news/52753292',
  54. 'md5': '47abaac93c6eaf9ad37ee6c4463a5179',
  55. 'info_dict': {
  56. 'id': '52753292',
  57. 'ext': 'flv',
  58. 'title': 'Crew emerges after four-month Mars food study',
  59. 'description': 'md5:24e632ffac72b35f8b67a12d1b6ddfc1',
  60. },
  61. },
  62. {
  63. 'url': 'http://www.nbcnews.com/feature/edward-snowden-interview/how-twitter-reacted-snowden-interview-n117236',
  64. 'md5': 'b2421750c9f260783721d898f4c42063',
  65. 'info_dict': {
  66. 'id': 'I1wpAI_zmhsQ',
  67. 'ext': 'mp4',
  68. 'title': 'How Twitter Reacted To The Snowden Interview',
  69. 'description': 'md5:65a0bd5d76fe114f3c2727aa3a81fe64',
  70. },
  71. 'add_ie': ['ThePlatform'],
  72. },
  73. {
  74. 'url': 'http://www.nbcnews.com/feature/dateline-full-episodes/full-episode-family-business-n285156',
  75. 'md5': 'fdbf39ab73a72df5896b6234ff98518a',
  76. 'info_dict': {
  77. 'id': 'Wjf9EDR3A_60',
  78. 'ext': 'mp4',
  79. 'title': 'FULL EPISODE: Family Business',
  80. 'description': 'md5:757988edbaae9d7be1d585eb5d55cc04',
  81. },
  82. },
  83. ]
  84. def _real_extract(self, url):
  85. mobj = re.match(self._VALID_URL, url)
  86. video_id = mobj.group('id')
  87. if video_id is not None:
  88. all_info = self._download_xml('http://www.nbcnews.com/id/%s/displaymode/1219' % video_id, video_id)
  89. info = all_info.find('video')
  90. return {
  91. 'id': video_id,
  92. 'title': info.find('headline').text,
  93. 'ext': 'flv',
  94. 'url': find_xpath_attr(info, 'media', 'type', 'flashVideo').text,
  95. 'description': compat_str(info.find('caption').text),
  96. 'thumbnail': find_xpath_attr(info, 'media', 'type', 'thumbnail').text,
  97. }
  98. else:
  99. # "feature" pages use theplatform.com
  100. title = mobj.group('title')
  101. webpage = self._download_webpage(url, title)
  102. bootstrap_json = self._search_regex(
  103. r'var bootstrapJson = ({.+})\s*$', webpage, 'bootstrap json',
  104. flags=re.MULTILINE)
  105. bootstrap = json.loads(bootstrap_json)
  106. info = bootstrap['results'][0]['video']
  107. mpxid = info['mpxId']
  108. base_urls = [
  109. info['fallbackPlaylistUrl'],
  110. info['associatedPlaylistUrl'],
  111. ]
  112. for base_url in base_urls:
  113. if not base_url:
  114. continue
  115. playlist_url = base_url + '?form=MPXNBCNewsAPI'
  116. try:
  117. all_videos = self._download_json(playlist_url, title)
  118. except ExtractorError as ee:
  119. if isinstance(ee.cause, compat_HTTPError):
  120. continue
  121. raise
  122. if not all_videos or 'videos' not in all_videos:
  123. continue
  124. try:
  125. info = next(v for v in all_videos['videos'] if v['mpxId'] == mpxid)
  126. break
  127. except StopIteration:
  128. continue
  129. if info is None:
  130. raise ExtractorError('Could not find video in playlists')
  131. return {
  132. '_type': 'url',
  133. # We get the best quality video
  134. 'url': info['videoAssets'][-1]['publicUrl'],
  135. 'ie_key': 'ThePlatform',
  136. }