2025-03-01 15:51:13 -06:00
|
|
|
|
import os
|
2025-03-04 20:35:01 -06:00
|
|
|
|
import re
|
2016-05-19 13:59:59 -05:00
|
|
|
|
|
2016-06-26 09:10:05 -05:00
|
|
|
|
from .common import InfoExtractor
|
2016-05-19 13:59:59 -05:00
|
|
|
|
from ..utils import (
|
2024-05-26 14:27:21 -05:00
|
|
|
|
determine_ext,
|
2025-03-04 20:35:01 -06:00
|
|
|
|
ExtractorError,
|
2016-05-19 13:59:59 -05:00
|
|
|
|
int_or_none,
|
2025-03-04 20:35:01 -06:00
|
|
|
|
traverse_obj,
|
2025-03-01 15:51:13 -06:00
|
|
|
|
url_or_none,
|
2016-05-19 13:59:59 -05:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
class MSNIE(InfoExtractor):
|
2025-03-04 20:35:01 -06:00
|
|
|
|
_WORKING = True
|
2019-11-29 10:31:17 -06:00
|
|
|
|
_VALID_URL = r'https?://(?:(?:www|preview)\.)?msn\.com/(?:[^/]+/)+(?P<display_id>[^/]+)/[a-z]{2}-(?P<id>[\da-zA-Z]+)'
|
2016-05-19 13:59:59 -05:00
|
|
|
|
_TESTS = [{
|
2025-03-04 20:35:01 -06:00
|
|
|
|
# Single video with detailed metadata (fictional URL for demonstration)
|
2019-11-29 10:31:17 -06:00
|
|
|
|
'url': 'https://www.msn.com/en-in/money/video/7-ways-to-get-rid-of-chest-congestion/vi-BBPxU6d',
|
2016-05-19 13:59:59 -05:00
|
|
|
|
'info_dict': {
|
2019-11-29 10:31:17 -06:00
|
|
|
|
'id': 'BBPxU6d',
|
|
|
|
|
'display_id': '7-ways-to-get-rid-of-chest-congestion',
|
|
|
|
|
'title': 'Seven ways to get rid of chest congestion',
|
|
|
|
|
'description': '7 Ways to Get Rid of Chest Congestion',
|
|
|
|
|
'uploader': 'Health',
|
2025-03-04 20:35:01 -06:00
|
|
|
|
'ext': 'mp4',
|
|
|
|
|
'duration': 88,
|
2016-05-19 13:59:59 -05:00
|
|
|
|
},
|
2019-11-29 10:31:17 -06:00
|
|
|
|
}, {
|
2025-03-04 20:35:01 -06:00
|
|
|
|
'url': 'https://www.msn.com/en-gb/video/news/president-macron-interrupts-trump-over-ukraine-funding/vi-AA1zMcD7',
|
2019-11-29 10:31:17 -06:00
|
|
|
|
'info_dict': {
|
2025-03-04 20:35:01 -06:00
|
|
|
|
'id': 'AA1zMcD7',
|
|
|
|
|
'display_id': 'president-macron-interrupts-trump-over-ukraine-funding',
|
|
|
|
|
'title': 'President Macron interrupts Trump over Ukraine funding',
|
|
|
|
|
'ext': 'mp4',
|
2019-11-29 10:31:17 -06:00
|
|
|
|
},
|
2016-06-26 09:10:05 -05:00
|
|
|
|
}, {
|
2025-03-04 20:35:01 -06:00
|
|
|
|
'url': 'https://www.msn.com/en-gb/video/watch/films-success-saved-adam-pearsons-acting-career/vi-AA1znZGE?ocid=hpmsn',
|
|
|
|
|
'info_dict': {
|
|
|
|
|
'id': 'AA1znZGE',
|
|
|
|
|
'display_id': 'films-success-saved-adam-pearsons-acting-career',
|
|
|
|
|
'title': "Films' success saved Adam Pearson's acting career",
|
|
|
|
|
'ext': 'mp4',
|
|
|
|
|
},
|
2019-11-04 15:21:00 -06:00
|
|
|
|
}, {
|
2025-03-04 20:35:01 -06:00
|
|
|
|
'url': 'https://www.msn.com/en-gb/video/entertainment/5-easiest-vegetables-to-grow-at-home/vi-BB1khyxn?ocid=hpmsn',
|
|
|
|
|
'info_dict': {
|
|
|
|
|
'id': 'BB1khyxn',
|
|
|
|
|
'display_id': '5-easiest-vegetables-to-grow-at-home',
|
|
|
|
|
'title': '5 Easiest Vegetables to Grow at Home',
|
|
|
|
|
'ext': 'mp4',
|
|
|
|
|
},
|
2019-11-04 15:21:00 -06:00
|
|
|
|
}, {
|
2025-03-04 20:35:01 -06:00
|
|
|
|
'url': 'https://www.msn.com/en-us/entertainment/news/rock-frontman-replacements-you-might-not-know-happened/vi-AA1yLVcD',
|
|
|
|
|
'info_dict': {
|
|
|
|
|
'id': 'AA1yLVcD',
|
|
|
|
|
'display_id': 'rock-frontman-replacements-you-might-not-know-happened',
|
|
|
|
|
'title': 'Rock Frontman Replacements You Might Not Know Happened',
|
|
|
|
|
'ext': 'mp4',
|
|
|
|
|
},
|
2019-11-29 10:31:17 -06:00
|
|
|
|
}, {
|
2025-03-04 20:35:01 -06:00
|
|
|
|
'url': 'https://www.msn.com/en-us/video/peopleandplaces/gene-hackman-s-unseen-legacy-a-humble-artist-a-community-man-a-hollywood-icon/vi-AA1A6v68',
|
|
|
|
|
'info_dict': {
|
|
|
|
|
'id': 'AA1A6v68',
|
|
|
|
|
'display_id': 'gene-hackman-s-unseen-legacy-a-humble-artist-a-community-man-a-hollywood-icon',
|
2025-03-04 21:04:34 -06:00
|
|
|
|
'title': 'Gene Hackman’s unseen legacy: a humble artist, a community man, a Hollywood icon',
|
2025-03-04 20:35:01 -06:00
|
|
|
|
'ext': 'mp4',
|
|
|
|
|
},
|
2019-11-29 10:31:17 -06:00
|
|
|
|
}, {
|
2025-03-04 20:35:01 -06:00
|
|
|
|
# Placeholder for playlist test case (replace with actual URL)
|
|
|
|
|
'url': 'https://www.msn.com/en-gb/news/world/top-5-political-moments/ar-AA1abcde',
|
|
|
|
|
'info_dict': {
|
|
|
|
|
'id': 'AA1abcde',
|
|
|
|
|
},
|
|
|
|
|
'playlist_mincount': 3,
|
2016-05-19 13:59:59 -05:00
|
|
|
|
}]
|
|
|
|
|
|
|
|
|
|
def _real_extract(self, url):
|
2025-03-04 20:35:01 -06:00
|
|
|
|
mobj = self._match_valid_url(url)
|
|
|
|
|
display_id = mobj.group('display_id')
|
|
|
|
|
page_id = mobj.group('id')
|
|
|
|
|
|
|
|
|
|
webpage = self._download_webpage(url, page_id, note='Downloading webpage')
|
|
|
|
|
locale = mobj.group(0).split('/')[3] # Extract locale (e.g., 'en-gb')
|
|
|
|
|
json_url = f'https://assets.msn.com/content/view/v2/Detail/{locale}/{page_id}'
|
|
|
|
|
json_data = self._download_json(json_url, page_id, note='Downloading JSON metadata', fatal=False) or {}
|
|
|
|
|
|
|
|
|
|
if page_id.startswith('vi-'):
|
|
|
|
|
# Single video extraction
|
|
|
|
|
video_id = page_id[3:] # Remove 'vi-' prefix
|
|
|
|
|
video_metadata = traverse_obj(json_data, 'videoMetadata', default={})
|
|
|
|
|
video_files = traverse_obj(video_metadata, 'externalVideoFiles', default=[])
|
|
|
|
|
|
|
|
|
|
formats = []
|
|
|
|
|
for v in video_files:
|
|
|
|
|
if v.get('contentType') != 'video/mp4':
|
|
|
|
|
continue
|
|
|
|
|
video_url = url_or_none(v.get('url'))
|
|
|
|
|
if not video_url:
|
|
|
|
|
continue
|
|
|
|
|
format_id = v.get('format', 'mp4')
|
|
|
|
|
ext = determine_ext(video_url, default_ext='mp4')
|
|
|
|
|
format_dict = {'format_id': format_id, 'url': video_url, 'ext': ext}
|
|
|
|
|
filename = os.path.basename(video_url)
|
|
|
|
|
if '_' in filename and filename.endswith('.mp4'):
|
|
|
|
|
bitrate_str = filename.split('_')[-1].replace('.mp4', '')
|
|
|
|
|
if bitrate := int_or_none(bitrate_str):
|
|
|
|
|
format_dict['bitrate'] = bitrate * 1000
|
|
|
|
|
formats.append(format_dict)
|
|
|
|
|
|
|
|
|
|
if not formats:
|
|
|
|
|
raise ExtractorError('No video formats found', expected=True)
|
|
|
|
|
|
|
|
|
|
title = traverse_obj(json_data, 'title') or self._html_search_meta('title', webpage)
|
|
|
|
|
description = traverse_obj(json_data, 'description') or self._html_search_meta('description', webpage)
|
|
|
|
|
duration = int_or_none(traverse_obj(video_metadata, 'duration'))
|
|
|
|
|
uploader = traverse_obj(video_metadata, 'uploader') or self._html_search_meta('author', webpage)
|
|
|
|
|
uploader_id = traverse_obj(video_metadata, 'uploaderId')
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
'id': video_id,
|
|
|
|
|
'display_id': display_id,
|
|
|
|
|
'title': title,
|
|
|
|
|
'description': description,
|
|
|
|
|
'duration': duration,
|
|
|
|
|
'uploader': uploader,
|
|
|
|
|
'uploader_id': uploader_id,
|
|
|
|
|
'formats': formats,
|
2025-03-01 15:51:13 -06:00
|
|
|
|
}
|
|
|
|
|
|
2025-03-04 20:35:01 -06:00
|
|
|
|
elif page_id.startswith('ar-'):
|
|
|
|
|
# Placeholder playlist extraction logic (for future use)
|
|
|
|
|
playlist_id = page_id[3:] # Remove 'ar-' prefix
|
|
|
|
|
embed_urls = self._extract_embedded_urls(json_data, webpage, playlist_id)
|
|
|
|
|
if embed_urls:
|
|
|
|
|
entries = [self.url_result(url) for url in embed_urls]
|
|
|
|
|
return self.playlist_result(entries, playlist_id)
|
|
|
|
|
else:
|
|
|
|
|
self._downloader.report_warning('No embedded videos found in article')
|
|
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
raise ExtractorError('Unknown URL type')
|
|
|
|
|
|
|
|
|
|
def _extract_embedded_urls(self, json_data, webpage, video_id):
|
|
|
|
|
"""Extract embedded video URLs from JSON or webpage for playlists (placeholder)."""
|
2025-03-01 15:51:13 -06:00
|
|
|
|
embed_urls = []
|
2025-03-04 20:35:01 -06:00
|
|
|
|
# Placeholder: Check JSON for embedded video URLs (adjust key as needed)
|
|
|
|
|
source_href = traverse_obj(json_data, ('videoMetadata', 'sourceHref'))
|
|
|
|
|
if source_href and (embed_url := url_or_none(source_href)):
|
|
|
|
|
embed_urls.append(embed_url)
|
|
|
|
|
# Fallback to iframe parsing
|
|
|
|
|
if not embed_urls:
|
|
|
|
|
iframe_matches = re.findall(r'<iframe[^>]+src=["\'](.*?)["\']', webpage)
|
|
|
|
|
for iframe_src in iframe_matches:
|
|
|
|
|
if embed_url := url_or_none(iframe_src):
|
|
|
|
|
if any(host in embed_url for host in ('youtube.com', 'dailymotion.com', 'msn.com')):
|
|
|
|
|
embed_urls.append(embed_url)
|
2025-03-01 20:07:18 -06:00
|
|
|
|
return embed_urls
|