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.

122 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. # .ism is not yet supported (see
  63. # https://github.com/rg3/youtube-dl/issues/8118)
  64. if ext == 'ism':
  65. continue
  66. if 'm3u8' in format_url:
  67. # m3u8_native should not be used here until
  68. # https://github.com/rg3/youtube-dl/issues/9913 is fixed
  69. m3u8_formats = self._extract_m3u8_formats(
  70. format_url, display_id, 'mp4',
  71. m3u8_id='hls', fatal=False)
  72. # Despite metadata in m3u8 all video+audio formats are
  73. # actually video-only (no audio)
  74. for f in m3u8_formats:
  75. if f.get('acodec') != 'none' and f.get('vcodec') != 'none':
  76. f['acodec'] = 'none'
  77. formats.extend(m3u8_formats)
  78. else:
  79. formats.append({
  80. 'url': format_url,
  81. 'ext': 'mp4',
  82. 'format_id': 'http',
  83. 'width': int_or_none(file_.get('width')),
  84. 'height': int_or_none(file_.get('height')),
  85. })
  86. self._sort_formats(formats)
  87. subtitles = {}
  88. for file_ in video.get('files', []):
  89. format_url = file_.get('url')
  90. format_code = file_.get('formatCode')
  91. if not format_url or not format_code:
  92. continue
  93. if compat_str(format_code) == '3100':
  94. subtitles.setdefault(file_.get('culture', 'en'), []).append({
  95. 'ext': determine_ext(format_url, 'ttml'),
  96. 'url': format_url,
  97. })
  98. return {
  99. 'id': video_id,
  100. 'display_id': display_id,
  101. 'title': title,
  102. 'description': video.get('description'),
  103. 'thumbnail': video.get('headlineImage', {}).get('url'),
  104. 'duration': int_or_none(video.get('durationSecs')),
  105. 'uploader': video.get('sourceFriendly'),
  106. 'uploader_id': video.get('providerId'),
  107. 'creator': video.get('creator'),
  108. 'subtitles': subtitles,
  109. 'formats': formats,
  110. }