]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/plutotv.py
[plutotv] Extract subtitles from manifests
[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
7700b37f 81 def _to_ad_free_formats(self, video_id, formats, subtitles):
82 ad_free_formats, ad_free_subtitles, m3u8_urls = [], {}, set()
ea3a012d
C
83 for format in formats:
84 res = self._download_webpage(
85 format.get('url'), video_id, note='Downloading m3u8 playlist',
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)
92 if not first_segment_url:
93 continue
94 m3u8_urls.add(
95 compat_urlparse.urljoin(first_segment_url.group(1), '0-end/master.m3u8'))
96
97 for m3u8_url in m3u8_urls:
7700b37f 98 fmts, subs = self._extract_m3u8_formats_and_subtitles(
99 m3u8_url, video_id, 'mp4', 'm3u8_native', m3u8_id='hls', fatal=False)
100 ad_free_formats.extend(fmts)
101 ad_free_subtitles = self._merge_subtitles(ad_free_subtitles, subs)
102 return ad_free_formats, ad_free_subtitles
ea3a012d
C
103
104 def _get_video_info(self, video_json, slug, series_name=None):
105 video_id = video_json.get('_id', slug)
7700b37f 106 formats, subtitles = [], {}
ea3a012d
C
107 for video_url in try_get(video_json, lambda x: x['stitched']['urls'], list) or []:
108 if video_url.get('type') != 'hls':
109 continue
110 url = url_or_none(video_url.get('url'))
7700b37f 111
112 fmts, subs = self._extract_m3u8_formats_and_subtitles(
113 url, video_id, 'mp4', 'm3u8_native', m3u8_id='hls', fatal=False)
114 formats.extend(fmts)
115 subtitles = self._merge_subtitles(subtitles, subs)
116
117 formats, subtitles = self._to_ad_free_formats(video_id, formats, subtitles)
118 self._sort_formats(formats)
119
ea3a012d
C
120 info = {
121 'id': video_id,
7700b37f 122 'formats': formats,
123 'subtitles': subtitles,
ea3a012d
C
124 'title': video_json.get('name'),
125 'description': video_json.get('description'),
126 'duration': float_or_none(video_json.get('duration'), scale=1000),
127 }
128 if series_name:
129 info.update({
130 'series': series_name,
131 'episode': video_json.get('name'),
132 'season_number': int_or_none(video_json.get('season')),
133 'episode_number': int_or_none(video_json.get('number')),
134 })
135 return info
136
137 def _real_extract(self, url):
138 path = compat_urlparse.urlparse(url).path
139 path_components = path.split('/')
140 video_type = path_components[2]
141 info_slug = path_components[3]
142 video_json = self._download_json(self._INFO_URL + info_slug, info_slug,
143 query=self._INFO_QUERY_PARAMS)
144
145 if video_type == 'series':
146 series_name = video_json.get('name', info_slug)
147 season_number = int_or_none(try_get(path_components, lambda x: x[5]))
148 episode_slug = try_get(path_components, lambda x: x[7])
149
150 videos = []
151 for season in video_json['seasons']:
152 if season_number is not None and season_number != int_or_none(season.get('number')):
153 continue
154 for episode in season['episodes']:
155 if episode_slug is not None and episode_slug != episode.get('slug'):
156 continue
157 videos.append(self._get_video_info(episode, episode_slug, series_name))
158 if not videos:
159 raise ExtractorError('Failed to find any videos to extract')
160 if episode_slug is not None and len(videos) == 1:
161 return videos[0]
162 playlist_title = series_name
163 if season_number is not None:
164 playlist_title += ' - Season %d' % season_number
165 return self.playlist_result(videos,
166 playlist_id=video_json.get('_id', info_slug),
167 playlist_title=playlist_title)
168 return self._get_video_info(video_json, info_slug)