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