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