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.

117 lines
4.1 KiB

11 years ago
  1. from __future__ import unicode_literals
  2. import re
  3. import json
  4. from .common import InfoExtractor
  5. from ..utils import (
  6. compat_str,
  7. ExtractorError,
  8. find_xpath_attr,
  9. )
  10. class NBCIE(InfoExtractor):
  11. _VALID_URL = r'http://www\.nbc\.com/[^/]+/video/[^/]+/(?P<id>n?\d+)'
  12. _TEST = {
  13. 'url': 'http://www.nbc.com/chicago-fire/video/i-am-a-firefighter/2734188',
  14. 'md5': '54d0fbc33e0b853a65d7b4de5c06d64e',
  15. 'info_dict': {
  16. 'id': 'u1RInQZRN7QJ',
  17. 'ext': 'flv',
  18. 'title': 'I Am a Firefighter',
  19. 'description': 'An emergency puts Dawson\'sf irefighter skills to the ultimate test in this four-part digital series.',
  20. },
  21. }
  22. def _real_extract(self, url):
  23. mobj = re.match(self._VALID_URL, url)
  24. video_id = mobj.group('id')
  25. webpage = self._download_webpage(url, video_id)
  26. theplatform_url = self._search_regex('class="video-player video-player-full" data-mpx-url="(.*?)"', webpage, 'theplatform url')
  27. if theplatform_url.startswith('//'):
  28. theplatform_url = 'http:' + theplatform_url
  29. return self.url_result(theplatform_url)
  30. class NBCNewsIE(InfoExtractor):
  31. _VALID_URL = r'''(?x)https?://www\.nbcnews\.com/
  32. ((video/.+?/(?P<id>\d+))|
  33. (feature/[^/]+/(?P<title>.+)))
  34. '''
  35. _TESTS = [
  36. {
  37. 'url': 'http://www.nbcnews.com/video/nbc-news/52753292',
  38. 'md5': '47abaac93c6eaf9ad37ee6c4463a5179',
  39. 'info_dict': {
  40. 'id': '52753292',
  41. 'ext': 'flv',
  42. 'title': 'Crew emerges after four-month Mars food study',
  43. 'description': 'md5:24e632ffac72b35f8b67a12d1b6ddfc1',
  44. },
  45. },
  46. {
  47. 'url': 'http://www.nbcnews.com/feature/edward-snowden-interview/how-twitter-reacted-snowden-interview-n117236',
  48. 'md5': 'b2421750c9f260783721d898f4c42063',
  49. 'info_dict': {
  50. 'id': 'I1wpAI_zmhsQ',
  51. 'ext': 'flv',
  52. 'title': 'How Twitter Reacted To The Snowden Interview',
  53. 'description': 'md5:65a0bd5d76fe114f3c2727aa3a81fe64',
  54. },
  55. 'add_ie': ['ThePlatform'],
  56. },
  57. ]
  58. def _real_extract(self, url):
  59. mobj = re.match(self._VALID_URL, url)
  60. video_id = mobj.group('id')
  61. if video_id is not None:
  62. all_info = self._download_xml('http://www.nbcnews.com/id/%s/displaymode/1219' % video_id, video_id)
  63. info = all_info.find('video')
  64. return {
  65. 'id': video_id,
  66. 'title': info.find('headline').text,
  67. 'ext': 'flv',
  68. 'url': find_xpath_attr(info, 'media', 'type', 'flashVideo').text,
  69. 'description': compat_str(info.find('caption').text),
  70. 'thumbnail': find_xpath_attr(info, 'media', 'type', 'thumbnail').text,
  71. }
  72. else:
  73. # "feature" pages use theplatform.com
  74. title = mobj.group('title')
  75. webpage = self._download_webpage(url, title)
  76. bootstrap_json = self._search_regex(
  77. r'var bootstrapJson = ({.+})\s*$', webpage, 'bootstrap json',
  78. flags=re.MULTILINE)
  79. bootstrap = json.loads(bootstrap_json)
  80. info = bootstrap['results'][0]['video']
  81. mpxid = info['mpxId']
  82. base_urls = [
  83. info['fallbackPlaylistUrl'],
  84. info['associatedPlaylistUrl'],
  85. ]
  86. for base_url in base_urls:
  87. playlist_url = base_url + '?form=MPXNBCNewsAPI'
  88. all_videos = self._download_json(playlist_url, title)['videos']
  89. try:
  90. info = next(v for v in all_videos if v['mpxId'] == mpxid)
  91. break
  92. except StopIteration:
  93. continue
  94. if info is None:
  95. raise ExtractorError('Could not find video in playlists')
  96. return {
  97. '_type': 'url',
  98. # We get the best quality video
  99. 'url': info['videoAssets'][-1]['publicUrl'],
  100. 'ie_key': 'ThePlatform',
  101. }