nbc.py 4.7 KB

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