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