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