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.

118 lines
4.1 KiB

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