]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/plutotv.py
Ensure `mergeall` selects best format when multistreams are disabled
[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):
22 _VALID_URL = r'https?://(?:www\.)?pluto\.tv/on-demand/(?P<video_type>movies|series)/(?P<slug>.*)/?$'
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 }
51 },
52 {
53 'url': 'https://pluto.tv/on-demand/series/i-love-money/season/1/',
54 'playlist_count': 11,
55 'info_dict': {
56 'id': '5de6c582e9379ae4912dedbd',
57 'title': 'I Love Money - Season 1',
58 }
59 },
60 {
61 'url': 'https://pluto.tv/on-demand/series/i-love-money/',
62 'playlist_count': 26,
63 'info_dict': {
64 'id': '5de6c582e9379ae4912dedbd',
65 'title': 'I Love Money',
66 }
67 },
68 {
69 'url': 'https://pluto.tv/on-demand/movies/arrival-2015-1-1',
70 'md5': '3cead001d317a018bf856a896dee1762',
71 'info_dict': {
72 'id': '5e83ac701fa6a9001bb9df24',
73 'ext': 'mp4',
74 'title': 'Arrival',
75 '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.',
76 'duration': 9000,
77 }
78 },
79 ]
80
81 def _to_ad_free_formats(self, video_id, formats):
82 ad_free_formats = []
83 m3u8_urls = set()
84 for format in formats:
85 res = self._download_webpage(
86 format.get('url'), video_id, note='Downloading m3u8 playlist',
87 fatal=False)
88 if not res:
89 continue
90 first_segment_url = re.search(
91 r'^(https?://.*/)0\-(end|[0-9]+)/[^/]+\.ts$', res,
92 re.MULTILINE)
93 if not first_segment_url:
94 continue
95 m3u8_urls.add(
96 compat_urlparse.urljoin(first_segment_url.group(1), '0-end/master.m3u8'))
97
98 for m3u8_url in m3u8_urls:
99 ad_free_formats.extend(
100 self._extract_m3u8_formats(
101 m3u8_url, video_id, 'mp4', 'm3u8_native',
102 m3u8_id='hls', fatal=False))
103 self._sort_formats(ad_free_formats)
104 return ad_free_formats
105
106 def _get_video_info(self, video_json, slug, series_name=None):
107 video_id = video_json.get('_id', slug)
108 formats = []
109 for video_url in try_get(video_json, lambda x: x['stitched']['urls'], list) or []:
110 if video_url.get('type') != 'hls':
111 continue
112 url = url_or_none(video_url.get('url'))
113 formats.extend(
114 self._extract_m3u8_formats(
115 url, video_id, 'mp4', 'm3u8_native',
116 m3u8_id='hls', fatal=False))
117 info = {
118 'id': video_id,
119 'formats': self._to_ad_free_formats(video_id, formats),
120 'title': video_json.get('name'),
121 'description': video_json.get('description'),
122 'duration': float_or_none(video_json.get('duration'), scale=1000),
123 }
124 if series_name:
125 info.update({
126 'series': series_name,
127 'episode': video_json.get('name'),
128 'season_number': int_or_none(video_json.get('season')),
129 'episode_number': int_or_none(video_json.get('number')),
130 })
131 return info
132
133 def _real_extract(self, url):
134 path = compat_urlparse.urlparse(url).path
135 path_components = path.split('/')
136 video_type = path_components[2]
137 info_slug = path_components[3]
138 video_json = self._download_json(self._INFO_URL + info_slug, info_slug,
139 query=self._INFO_QUERY_PARAMS)
140
141 if video_type == 'series':
142 series_name = video_json.get('name', info_slug)
143 season_number = int_or_none(try_get(path_components, lambda x: x[5]))
144 episode_slug = try_get(path_components, lambda x: x[7])
145
146 videos = []
147 for season in video_json['seasons']:
148 if season_number is not None and season_number != int_or_none(season.get('number')):
149 continue
150 for episode in season['episodes']:
151 if episode_slug is not None and episode_slug != episode.get('slug'):
152 continue
153 videos.append(self._get_video_info(episode, episode_slug, series_name))
154 if not videos:
155 raise ExtractorError('Failed to find any videos to extract')
156 if episode_slug is not None and len(videos) == 1:
157 return videos[0]
158 playlist_title = series_name
159 if season_number is not None:
160 playlist_title += ' - Season %d' % season_number
161 return self.playlist_result(videos,
162 playlist_id=video_json.get('_id', info_slug),
163 playlist_title=playlist_title)
164 return self._get_video_info(video_json, info_slug)