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.

134 lines
4.7 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. )
  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/chicago-fire/video/i-am-a-firefighter/2734188',
  17. # md5 checksum is not stable
  18. 'info_dict': {
  19. 'id': 'bTmnLCvIbaaH',
  20. 'ext': 'flv',
  21. 'title': 'I Am a Firefighter',
  22. 'description': 'An emergency puts Dawson\'sf irefighter skills to the ultimate test in this four-part digital series.',
  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/[^/]+/(?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. def _real_extract(self, url):
  74. mobj = re.match(self._VALID_URL, url)
  75. video_id = mobj.group('id')
  76. if video_id is not None:
  77. all_info = self._download_xml('http://www.nbcnews.com/id/%s/displaymode/1219' % video_id, video_id)
  78. info = all_info.find('video')
  79. return {
  80. 'id': video_id,
  81. 'title': info.find('headline').text,
  82. 'ext': 'flv',
  83. 'url': find_xpath_attr(info, 'media', 'type', 'flashVideo').text,
  84. 'description': compat_str(info.find('caption').text),
  85. 'thumbnail': find_xpath_attr(info, 'media', 'type', 'thumbnail').text,
  86. }
  87. else:
  88. # "feature" pages use theplatform.com
  89. title = mobj.group('title')
  90. webpage = self._download_webpage(url, title)
  91. bootstrap_json = self._search_regex(
  92. r'var bootstrapJson = ({.+})\s*$', webpage, 'bootstrap json',
  93. flags=re.MULTILINE)
  94. bootstrap = json.loads(bootstrap_json)
  95. info = bootstrap['results'][0]['video']
  96. mpxid = info['mpxId']
  97. base_urls = [
  98. info['fallbackPlaylistUrl'],
  99. info['associatedPlaylistUrl'],
  100. ]
  101. for base_url in base_urls:
  102. if not base_url:
  103. continue
  104. playlist_url = base_url + '?form=MPXNBCNewsAPI'
  105. all_videos = self._download_json(playlist_url, title)['videos']
  106. try:
  107. info = next(v for v in all_videos if v['mpxId'] == mpxid)
  108. break
  109. except StopIteration:
  110. continue
  111. if info is None:
  112. raise ExtractorError('Could not find video in playlists')
  113. return {
  114. '_type': 'url',
  115. # We get the best quality video
  116. 'url': info['videoAssets'][-1]['publicUrl'],
  117. 'ie_key': 'ThePlatform',
  118. }