]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/plutotv.py
[extractor] Deprecate `_sort_formats`
[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):
826446bd 19 _VALID_URL = r'''(?x)
aab41cdd 20 https?://(?:www\.)?pluto\.tv(?:/[^/]+)?/on-demand
826446bd 21 /(?P<video_type>movies|series)
22 /(?P<series_or_movie_slug>[^/]+)
23 (?:
aab41cdd 24 (?:/seasons?/(?P<season_no>\d+))?
826446bd 25 (?:/episode/(?P<episode_slug>[^/]+))?
26 )?
27 /?(?:$|[#?])'''
28
ea3a012d
C
29 _INFO_URL = 'https://service-vod.clusters.pluto.tv/v3/vod/slugs/'
30 _INFO_QUERY_PARAMS = {
31 'appName': 'web',
32 'appVersion': 'na',
33 'clientID': compat_str(uuid.uuid1()),
34 'clientModelNumber': 'na',
35 'serverSideAds': 'false',
36 'deviceMake': 'unknown',
37 'deviceModel': 'web',
38 'deviceType': 'web',
39 'deviceVersion': 'unknown',
40 'sid': compat_str(uuid.uuid1()),
41 }
42 _TESTS = [
43 {
44 'url': 'https://pluto.tv/on-demand/series/i-love-money/season/2/episode/its-in-the-cards-2009-2-3',
45 'md5': 'ebcdd8ed89aaace9df37924f722fd9bd',
46 'info_dict': {
47 'id': '5de6c598e9379ae4912df0a8',
48 'ext': 'mp4',
49 'title': 'It\'s In The Cards',
50 'episode': 'It\'s In The Cards',
51 '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.',
52 'series': 'I Love Money',
53 'season_number': 2,
54 'episode_number': 3,
55 'duration': 3600,
56 }
2b18a8c5 57 }, {
ea3a012d
C
58 'url': 'https://pluto.tv/on-demand/series/i-love-money/season/1/',
59 'playlist_count': 11,
60 'info_dict': {
61 'id': '5de6c582e9379ae4912dedbd',
62 'title': 'I Love Money - Season 1',
63 }
2b18a8c5 64 }, {
ea3a012d
C
65 'url': 'https://pluto.tv/on-demand/series/i-love-money/',
66 'playlist_count': 26,
67 'info_dict': {
68 'id': '5de6c582e9379ae4912dedbd',
69 'title': 'I Love Money',
70 }
2b18a8c5 71 }, {
ea3a012d
C
72 'url': 'https://pluto.tv/on-demand/movies/arrival-2015-1-1',
73 'md5': '3cead001d317a018bf856a896dee1762',
74 'info_dict': {
75 'id': '5e83ac701fa6a9001bb9df24',
76 'ext': 'mp4',
77 'title': 'Arrival',
78 '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.',
79 'duration': 9000,
80 }
2b18a8c5 81 }, {
82 'url': 'https://pluto.tv/en/on-demand/series/manhunters-fugitive-task-force/seasons/1/episode/third-times-the-charm-1-1',
83 'only_matching': True,
aab41cdd 84 }, {
85 'url': 'https://pluto.tv/it/on-demand/series/csi-vegas/episode/legacy-2021-1-1',
86 'only_matching': True,
2b18a8c5 87 }
ea3a012d
C
88 ]
89
7700b37f 90 def _to_ad_free_formats(self, video_id, formats, subtitles):
91 ad_free_formats, ad_free_subtitles, m3u8_urls = [], {}, set()
12e73423 92 for fmt in formats:
ea3a012d 93 res = self._download_webpage(
12e73423 94 fmt.get('url'), video_id, note='Downloading m3u8 playlist',
ea3a012d
C
95 fatal=False)
96 if not res:
97 continue
98 first_segment_url = re.search(
99 r'^(https?://.*/)0\-(end|[0-9]+)/[^/]+\.ts$', res,
100 re.MULTILINE)
12e73423 101 if first_segment_url:
102 m3u8_urls.add(
103 compat_urlparse.urljoin(first_segment_url.group(1), '0-end/master.m3u8'))
104 continue
105 first_segment_url = re.search(
106 r'^(https?://.*/).+\-0+\.ts$', res,
107 re.MULTILINE)
108 if first_segment_url:
109 m3u8_urls.add(
110 compat_urlparse.urljoin(first_segment_url.group(1), 'master.m3u8'))
ea3a012d 111 continue
ea3a012d
C
112
113 for m3u8_url in m3u8_urls:
7700b37f 114 fmts, subs = self._extract_m3u8_formats_and_subtitles(
115 m3u8_url, video_id, 'mp4', 'm3u8_native', m3u8_id='hls', fatal=False)
116 ad_free_formats.extend(fmts)
117 ad_free_subtitles = self._merge_subtitles(ad_free_subtitles, subs)
12e73423 118 if ad_free_formats:
119 formats, subtitles = ad_free_formats, ad_free_subtitles
120 else:
a06916d9 121 self.report_warning('Unable to find ad-free formats')
12e73423 122 return formats, subtitles
ea3a012d
C
123
124 def _get_video_info(self, video_json, slug, series_name=None):
125 video_id = video_json.get('_id', slug)
7700b37f 126 formats, subtitles = [], {}
ea3a012d
C
127 for video_url in try_get(video_json, lambda x: x['stitched']['urls'], list) or []:
128 if video_url.get('type') != 'hls':
129 continue
130 url = url_or_none(video_url.get('url'))
7700b37f 131
132 fmts, subs = self._extract_m3u8_formats_and_subtitles(
133 url, video_id, 'mp4', 'm3u8_native', m3u8_id='hls', fatal=False)
134 formats.extend(fmts)
135 subtitles = self._merge_subtitles(subtitles, subs)
136
137 formats, subtitles = self._to_ad_free_formats(video_id, formats, subtitles)
7700b37f 138
ea3a012d
C
139 info = {
140 'id': video_id,
7700b37f 141 'formats': formats,
142 'subtitles': subtitles,
ea3a012d
C
143 'title': video_json.get('name'),
144 'description': video_json.get('description'),
145 'duration': float_or_none(video_json.get('duration'), scale=1000),
146 }
147 if series_name:
148 info.update({
149 'series': series_name,
150 'episode': video_json.get('name'),
151 'season_number': int_or_none(video_json.get('season')),
152 'episode_number': int_or_none(video_json.get('number')),
153 })
154 return info
155
156 def _real_extract(self, url):
826446bd 157 mobj = self._match_valid_url(url).groupdict()
158 info_slug = mobj['series_or_movie_slug']
159 video_json = self._download_json(self._INFO_URL + info_slug, info_slug, query=self._INFO_QUERY_PARAMS)
ea3a012d 160
826446bd 161 if mobj['video_type'] == 'series':
ea3a012d 162 series_name = video_json.get('name', info_slug)
826446bd 163 season_number, episode_slug = mobj.get('season_number'), mobj.get('episode_slug')
ea3a012d
C
164
165 videos = []
166 for season in video_json['seasons']:
167 if season_number is not None and season_number != int_or_none(season.get('number')):
168 continue
169 for episode in season['episodes']:
170 if episode_slug is not None and episode_slug != episode.get('slug'):
171 continue
172 videos.append(self._get_video_info(episode, episode_slug, series_name))
173 if not videos:
174 raise ExtractorError('Failed to find any videos to extract')
175 if episode_slug is not None and len(videos) == 1:
176 return videos[0]
177 playlist_title = series_name
178 if season_number is not None:
179 playlist_title += ' - Season %d' % season_number
180 return self.playlist_result(videos,
181 playlist_id=video_json.get('_id', info_slug),
182 playlist_title=playlist_title)
183 return self._get_video_info(video_json, info_slug)