msn.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from .common import InfoExtractor
  5. from ..compat import compat_str
  6. from ..utils import (
  7. determine_ext,
  8. ExtractorError,
  9. int_or_none,
  10. unescapeHTML,
  11. )
  12. class MSNIE(InfoExtractor):
  13. _VALID_URL = r'https?://(?:www\.)?msn\.com/(?:[^/]+/)+(?P<display_id>[^/]+)/[a-z]{2}-(?P<id>[\da-zA-Z]+)'
  14. _TESTS = [{
  15. 'url': 'http://www.msn.com/en-ae/foodanddrink/joinourtable/criminal-minds-shemar-moore-shares-a-touching-goodbye-message/vp-BBqQYNE',
  16. 'md5': '8442f66c116cbab1ff7098f986983458',
  17. 'info_dict': {
  18. 'id': 'BBqQYNE',
  19. 'display_id': 'criminal-minds-shemar-moore-shares-a-touching-goodbye-message',
  20. 'ext': 'mp4',
  21. 'title': 'Criminal Minds - Shemar Moore Shares A Touching Goodbye Message',
  22. 'description': 'md5:e8e89b897b222eb33a6b5067a8f1bc25',
  23. 'duration': 104,
  24. 'uploader': 'CBS Entertainment',
  25. 'uploader_id': 'IT0X5aoJ6bJgYerJXSDCgFmYPB1__54v',
  26. },
  27. }, {
  28. 'url': 'http://www.msn.com/en-ae/news/offbeat/meet-the-nine-year-old-self-made-millionaire/ar-BBt6ZKf',
  29. 'only_matching': True,
  30. }, {
  31. 'url': 'http://www.msn.com/en-ae/video/watch/obama-a-lot-of-people-will-be-disappointed/vi-AAhxUMH',
  32. 'only_matching': True,
  33. }, {
  34. # geo restricted
  35. 'url': 'http://www.msn.com/en-ae/foodanddrink/joinourtable/the-first-fart-makes-you-laugh-the-last-fart-makes-you-cry/vp-AAhzIBU',
  36. 'only_matching': True,
  37. }, {
  38. 'url': 'http://www.msn.com/en-ae/entertainment/bollywood/watch-how-salman-khan-reacted-when-asked-if-he-would-apologize-for-his-‘raped-woman’-comment/vi-AAhvzW6',
  39. 'only_matching': True,
  40. }, {
  41. # Vidible(AOL) Embed
  42. 'url': 'https://www.msn.com/en-us/video/animals/yellowstone-park-staffers-catch-deer-engaged-in-behavior-they-cant-explain/vi-AAGfdg1',
  43. 'only_matching': True,
  44. }, {
  45. # Dailymotion Embed
  46. 'url': 'https://www.msn.com/es-ve/entretenimiento/watch/winston-salem-paire-refait-des-siennes-en-perdant-sa-raquette-au-service/vp-AAG704L',
  47. 'only_matching': True,
  48. }]
  49. def _real_extract(self, url):
  50. mobj = re.match(self._VALID_URL, url)
  51. video_id, display_id = mobj.group('id', 'display_id')
  52. webpage = self._download_webpage(url, display_id)
  53. video = self._parse_json(
  54. self._search_regex(
  55. r'data-metadata\s*=\s*(["\'])(?P<data>.+?)\1',
  56. webpage, 'video data', default='{}', group='data'),
  57. display_id, transform_source=unescapeHTML)
  58. if not video:
  59. error = unescapeHTML(self._search_regex(
  60. r'data-error=(["\'])(?P<error>.+?)\1',
  61. webpage, 'error', group='error'))
  62. raise ExtractorError('%s said: %s' % (self.IE_NAME, error), expected=True)
  63. player_name = video.get('playerName')
  64. if player_name:
  65. provider_id = video.get('providerId')
  66. if provider_id:
  67. if player_name == 'AOL':
  68. return self.url_result(
  69. 'aol-video:' + provider_id, 'Aol', provider_id)
  70. elif player_name == 'Dailymotion':
  71. return self.url_result(
  72. 'https://www.dailymotion.com/video/' + provider_id,
  73. 'Dailymotion', provider_id)
  74. title = video['title']
  75. formats = []
  76. for file_ in video.get('videoFiles', []):
  77. format_url = file_.get('url')
  78. if not format_url:
  79. continue
  80. if 'm3u8' in format_url:
  81. # m3u8_native should not be used here until
  82. # https://github.com/ytdl-org/youtube-dl/issues/9913 is fixed
  83. m3u8_formats = self._extract_m3u8_formats(
  84. format_url, display_id, 'mp4',
  85. m3u8_id='hls', fatal=False)
  86. formats.extend(m3u8_formats)
  87. elif determine_ext(format_url) == 'ism':
  88. formats.extend(self._extract_ism_formats(
  89. format_url + '/Manifest', display_id, 'mss', fatal=False))
  90. else:
  91. formats.append({
  92. 'url': format_url,
  93. 'ext': 'mp4',
  94. 'format_id': 'http',
  95. 'width': int_or_none(file_.get('width')),
  96. 'height': int_or_none(file_.get('height')),
  97. })
  98. self._sort_formats(formats)
  99. subtitles = {}
  100. for file_ in video.get('files', []):
  101. format_url = file_.get('url')
  102. format_code = file_.get('formatCode')
  103. if not format_url or not format_code:
  104. continue
  105. if compat_str(format_code) == '3100':
  106. subtitles.setdefault(file_.get('culture', 'en'), []).append({
  107. 'ext': determine_ext(format_url, 'ttml'),
  108. 'url': format_url,
  109. })
  110. return {
  111. 'id': video_id,
  112. 'display_id': display_id,
  113. 'title': title,
  114. 'description': video.get('description'),
  115. 'thumbnail': video.get('headlineImage', {}).get('url'),
  116. 'duration': int_or_none(video.get('durationSecs')),
  117. 'uploader': video.get('sourceFriendly'),
  118. 'uploader_id': video.get('providerId'),
  119. 'creator': video.get('creator'),
  120. 'subtitles': subtitles,
  121. 'formats': formats,
  122. }