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.

121 lines
4.8 KiB

  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. def _real_extract(self, url):
  42. mobj = re.match(self._VALID_URL, url)
  43. video_id, display_id = mobj.group('id', 'display_id')
  44. webpage = self._download_webpage(url, display_id)
  45. video = self._parse_json(
  46. self._search_regex(
  47. r'data-metadata\s*=\s*(["\'])(?P<data>.+?)\1',
  48. webpage, 'video data', default='{}', group='data'),
  49. display_id, transform_source=unescapeHTML)
  50. if not video:
  51. error = unescapeHTML(self._search_regex(
  52. r'data-error=(["\'])(?P<error>.+?)\1',
  53. webpage, 'error', group='error'))
  54. raise ExtractorError('%s said: %s' % (self.IE_NAME, error), expected=True)
  55. title = video['title']
  56. formats = []
  57. for file_ in video.get('videoFiles', []):
  58. format_url = file_.get('url')
  59. if not format_url:
  60. continue
  61. ext = determine_ext(format_url)
  62. if ext == 'ism':
  63. formats.extend(self._extract_ism_formats(
  64. format_url + '/Manifest', display_id, 'mss', fatal=False))
  65. if 'm3u8' in format_url:
  66. # m3u8_native should not be used here until
  67. # https://github.com/rg3/youtube-dl/issues/9913 is fixed
  68. m3u8_formats = self._extract_m3u8_formats(
  69. format_url, display_id, 'mp4',
  70. m3u8_id='hls', fatal=False)
  71. # Despite metadata in m3u8 all video+audio formats are
  72. # actually video-only (no audio)
  73. for f in m3u8_formats:
  74. if f.get('acodec') != 'none' and f.get('vcodec') != 'none':
  75. f['acodec'] = 'none'
  76. formats.extend(m3u8_formats)
  77. else:
  78. formats.append({
  79. 'url': format_url,
  80. 'ext': 'mp4',
  81. 'format_id': 'http',
  82. 'width': int_or_none(file_.get('width')),
  83. 'height': int_or_none(file_.get('height')),
  84. })
  85. self._sort_formats(formats)
  86. subtitles = {}
  87. for file_ in video.get('files', []):
  88. format_url = file_.get('url')
  89. format_code = file_.get('formatCode')
  90. if not format_url or not format_code:
  91. continue
  92. if compat_str(format_code) == '3100':
  93. subtitles.setdefault(file_.get('culture', 'en'), []).append({
  94. 'ext': determine_ext(format_url, 'ttml'),
  95. 'url': format_url,
  96. })
  97. return {
  98. 'id': video_id,
  99. 'display_id': display_id,
  100. 'title': title,
  101. 'description': video.get('description'),
  102. 'thumbnail': video.get('headlineImage', {}).get('url'),
  103. 'duration': int_or_none(video.get('durationSecs')),
  104. 'uploader': video.get('sourceFriendly'),
  105. 'uploader_id': video.get('providerId'),
  106. 'creator': video.get('creator'),
  107. 'subtitles': subtitles,
  108. 'formats': formats,
  109. }