]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/plutotv.py
[ie/orf:on] Improve extraction (#9677)
[yt-dlp.git] / yt_dlp / extractor / plutotv.py
CommitLineData
ea3a012d
C
1import re
2import uuid
3
4from .common import InfoExtractor
5from ..compat import (
6 compat_str,
7 compat_urlparse,
8)
9from ..utils import (
10 ExtractorError,
11 float_or_none,
12 int_or_none,
13 try_get,
14 url_or_none,
15)
16
17
18class PlutoTVIE(InfoExtractor):
df773c3d 19 _WORKING = False
826446bd 20 _VALID_URL = r'''(?x)
aab41cdd 21 https?://(?:www\.)?pluto\.tv(?:/[^/]+)?/on-demand
826446bd 22 /(?P<video_type>movies|series)
23 /(?P<series_or_movie_slug>[^/]+)
24 (?:
aab41cdd 25 (?:/seasons?/(?P<season_no>\d+))?
826446bd 26 (?:/episode/(?P<episode_slug>[^/]+))?
27 )?
28 /?(?:$|[#?])'''
29
ea3a012d
C
30 _INFO_URL = 'https://service-vod.clusters.pluto.tv/v3/vod/slugs/'
31 _INFO_QUERY_PARAMS = {
32 'appName': 'web',
33 'appVersion': 'na',
34 'clientID': compat_str(uuid.uuid1()),
35 'clientModelNumber': 'na',
36 'serverSideAds': 'false',
37 'deviceMake': 'unknown',
38 'deviceModel': 'web',
39 'deviceType': 'web',
40 'deviceVersion': 'unknown',
41 'sid': compat_str(uuid.uuid1()),
42 }
43 _TESTS = [
44 {
45 'url': 'https://pluto.tv/on-demand/series/i-love-money/season/2/episode/its-in-the-cards-2009-2-3',
46 'md5': 'ebcdd8ed89aaace9df37924f722fd9bd',
47 'info_dict': {
48 'id': '5de6c598e9379ae4912df0a8',
49 'ext': 'mp4',
50 'title': 'It\'s In The Cards',
51 'episode': 'It\'s In The Cards',
52 'description': 'The teams face off against each other in a 3-on-2 soccer showdown. Strategy comes into play, though, as each team gets to select their opposing teams’ two defenders.',
53 'series': 'I Love Money',
54 'season_number': 2,
55 'episode_number': 3,
56 'duration': 3600,
57 }
2b18a8c5 58 }, {
ea3a012d
C
59 'url': 'https://pluto.tv/on-demand/series/i-love-money/season/1/',
60 'playlist_count': 11,
61 'info_dict': {
62 'id': '5de6c582e9379ae4912dedbd',
63 'title': 'I Love Money - Season 1',
64 }
2b18a8c5 65 }, {
ea3a012d
C
66 'url': 'https://pluto.tv/on-demand/series/i-love-money/',
67 'playlist_count': 26,
68 'info_dict': {
69 'id': '5de6c582e9379ae4912dedbd',
70 'title': 'I Love Money',
71 }
2b18a8c5 72 }, {
ea3a012d
C
73 'url': 'https://pluto.tv/on-demand/movies/arrival-2015-1-1',
74 'md5': '3cead001d317a018bf856a896dee1762',
75 'info_dict': {
76 'id': '5e83ac701fa6a9001bb9df24',
77 'ext': 'mp4',
78 'title': 'Arrival',
79 'description': 'When mysterious spacecraft touch down across the globe, an elite team - led by expert translator Louise Banks (Academy Award® nominee Amy Adams) – races against time to decipher their intent.',
80 'duration': 9000,
81 }
2b18a8c5 82 }, {
83 'url': 'https://pluto.tv/en/on-demand/series/manhunters-fugitive-task-force/seasons/1/episode/third-times-the-charm-1-1',
84 'only_matching': True,
aab41cdd 85 }, {
86 'url': 'https://pluto.tv/it/on-demand/series/csi-vegas/episode/legacy-2021-1-1',
87 'only_matching': True,
42ec478f
DT
88 },
89 {
90 'url': 'https://pluto.tv/en/on-demand/movies/attack-of-the-killer-tomatoes-1977-1-1-ptv1',
91 'md5': '7db56369c0da626a32d505ec6eb3f89f',
92 'info_dict': {
93 'id': '5b190c7bb0875c36c90c29c4',
94 'ext': 'mp4',
95 'title': 'Attack of the Killer Tomatoes',
96 'description': 'A group of scientists band together to save the world from mutated tomatoes that KILL! (1978)',
97 'duration': 5700,
98 }
2b18a8c5 99 }
ea3a012d
C
100 ]
101
7700b37f 102 def _to_ad_free_formats(self, video_id, formats, subtitles):
103 ad_free_formats, ad_free_subtitles, m3u8_urls = [], {}, set()
12e73423 104 for fmt in formats:
ea3a012d 105 res = self._download_webpage(
12e73423 106 fmt.get('url'), video_id, note='Downloading m3u8 playlist',
ea3a012d
C
107 fatal=False)
108 if not res:
109 continue
110 first_segment_url = re.search(
111 r'^(https?://.*/)0\-(end|[0-9]+)/[^/]+\.ts$', res,
112 re.MULTILINE)
12e73423 113 if first_segment_url:
114 m3u8_urls.add(
115 compat_urlparse.urljoin(first_segment_url.group(1), '0-end/master.m3u8'))
116 continue
117 first_segment_url = re.search(
42ec478f 118 r'^(https?://.*/).+\-0+[0-1]0\.ts$', res,
12e73423 119 re.MULTILINE)
120 if first_segment_url:
121 m3u8_urls.add(
122 compat_urlparse.urljoin(first_segment_url.group(1), 'master.m3u8'))
ea3a012d 123 continue
ea3a012d
C
124
125 for m3u8_url in m3u8_urls:
7700b37f 126 fmts, subs = self._extract_m3u8_formats_and_subtitles(
127 m3u8_url, video_id, 'mp4', 'm3u8_native', m3u8_id='hls', fatal=False)
128 ad_free_formats.extend(fmts)
129 ad_free_subtitles = self._merge_subtitles(ad_free_subtitles, subs)
12e73423 130 if ad_free_formats:
131 formats, subtitles = ad_free_formats, ad_free_subtitles
132 else:
a06916d9 133 self.report_warning('Unable to find ad-free formats')
12e73423 134 return formats, subtitles
ea3a012d
C
135
136 def _get_video_info(self, video_json, slug, series_name=None):
137 video_id = video_json.get('_id', slug)
7700b37f 138 formats, subtitles = [], {}
ea3a012d
C
139 for video_url in try_get(video_json, lambda x: x['stitched']['urls'], list) or []:
140 if video_url.get('type') != 'hls':
141 continue
142 url = url_or_none(video_url.get('url'))
7700b37f 143
144 fmts, subs = self._extract_m3u8_formats_and_subtitles(
145 url, video_id, 'mp4', 'm3u8_native', m3u8_id='hls', fatal=False)
146 formats.extend(fmts)
147 subtitles = self._merge_subtitles(subtitles, subs)
148
149 formats, subtitles = self._to_ad_free_formats(video_id, formats, subtitles)
7700b37f 150
ea3a012d
C
151 info = {
152 'id': video_id,
7700b37f 153 'formats': formats,
154 'subtitles': subtitles,
ea3a012d
C
155 'title': video_json.get('name'),
156 'description': video_json.get('description'),
157 'duration': float_or_none(video_json.get('duration'), scale=1000),
158 }
159 if series_name:
160 info.update({
161 'series': series_name,
162 'episode': video_json.get('name'),
163 'season_number': int_or_none(video_json.get('season')),
164 'episode_number': int_or_none(video_json.get('number')),
165 })
166 return info
167
168 def _real_extract(self, url):
826446bd 169 mobj = self._match_valid_url(url).groupdict()
170 info_slug = mobj['series_or_movie_slug']
171 video_json = self._download_json(self._INFO_URL + info_slug, info_slug, query=self._INFO_QUERY_PARAMS)
ea3a012d 172
826446bd 173 if mobj['video_type'] == 'series':
ea3a012d 174 series_name = video_json.get('name', info_slug)
826446bd 175 season_number, episode_slug = mobj.get('season_number'), mobj.get('episode_slug')
ea3a012d
C
176
177 videos = []
178 for season in video_json['seasons']:
179 if season_number is not None and season_number != int_or_none(season.get('number')):
180 continue
181 for episode in season['episodes']:
182 if episode_slug is not None and episode_slug != episode.get('slug'):
183 continue
184 videos.append(self._get_video_info(episode, episode_slug, series_name))
185 if not videos:
186 raise ExtractorError('Failed to find any videos to extract')
187 if episode_slug is not None and len(videos) == 1:
188 return videos[0]
189 playlist_title = series_name
190 if season_number is not None:
191 playlist_title += ' - Season %d' % season_number
192 return self.playlist_result(videos,
193 playlist_id=video_json.get('_id', info_slug),
194 playlist_title=playlist_title)
195 return self._get_video_info(video_json, info_slug)