]>
Commit | Line | Data |
---|---|---|
5196b988 NJ |
1 | import re |
2 | ||
3 | from .common import InfoExtractor | |
3d2623a8 | 4 | from ..networking import Request |
5196b988 NJ |
5 | from ..utils import ( |
6 | ExtractorError, | |
7 | int_or_none, | |
b5be6dd5 | 8 | js_to_json, |
2dc4970e | 9 | traverse_obj, |
bef9a9e5 | 10 | url_or_none, |
3d2623a8 | 11 | urlencode_postdata, |
5196b988 NJ |
12 | ) |
13 | ||
14 | ||
15 | class TubiTvIE(InfoExtractor): | |
d7d86181 | 16 | IE_NAME = 'tubitv' |
bef9a9e5 | 17 | _VALID_URL = r'https?://(?:www\.)?tubitv\.com/(?P<type>video|movies|tv-shows)/(?P<id>\d+)' |
5196b988 NJ |
18 | _LOGIN_URL = 'http://tubitv.com/login' |
19 | _NETRC_MACHINE = 'tubitv' | |
4fe4bda2 | 20 | _TESTS = [{ |
bef9a9e5 | 21 | 'url': 'https://tubitv.com/movies/100004539/the-39-steps', |
3b55aaac | 22 | 'info_dict': { |
bef9a9e5 | 23 | 'id': '100004539', |
3b55aaac | 24 | 'ext': 'mp4', |
bef9a9e5 BG |
25 | 'title': 'The 39 Steps', |
26 | 'description': 'md5:bb2f2dd337f0dc58c06cb509943f54c8', | |
27 | 'uploader_id': 'abc2558d54505d4f0f32be94f2e7108c', | |
28 | 'release_year': 1935, | |
3b55aaac | 29 | 'thumbnail': r're:^https?://.+\.(jpe?g|png)$', |
bef9a9e5 | 30 | 'duration': 5187, |
3b55aaac | 31 | }, |
bef9a9e5 BG |
32 | 'params': {'skip_download': 'm3u8'}, |
33 | }, { | |
34 | 'url': 'https://tubitv.com/tv-shows/554628/s01-e01-rise-of-the-snakes', | |
35 | 'info_dict': { | |
36 | 'id': '554628', | |
37 | 'ext': 'mp4', | |
38 | 'title': 'S01:E01 - Rise of the Snakes', | |
39 | 'description': 'md5:ba136f586de53af0372811e783a3f57d', | |
40 | 'episode': 'Rise of the Snakes', | |
41 | 'episode_number': 1, | |
42 | 'season': 'Season 1', | |
43 | 'season_number': 1, | |
44 | 'uploader_id': '2a9273e728c510d22aa5c57d0646810b', | |
45 | 'release_year': 2011, | |
46 | 'thumbnail': r're:^https?://.+\.(jpe?g|png)$', | |
47 | 'duration': 1376, | |
48 | }, | |
49 | 'params': {'skip_download': 'm3u8'}, | |
3b55aaac | 50 | }, { |
9260cf1d | 51 | 'url': 'http://tubitv.com/video/283829/the_comedian_at_the_friday', |
f4dfa9a5 | 52 | 'md5': '43ac06be9326f41912dc64ccf7a80320', |
5196b988 | 53 | 'info_dict': { |
9260cf1d | 54 | 'id': '283829', |
5196b988 | 55 | 'ext': 'mp4', |
9260cf1d | 56 | 'title': 'The Comedian at The Friday', |
57 | 'description': 'A stand up comedian is forced to look at the decisions in his life while on a one week trip to the west coast.', | |
f4dfa9a5 | 58 | 'uploader_id': 'bc168bee0d18dd1cb3b86c68706ab434', |
5196b988 | 59 | }, |
add96eb9 | 60 | 'skip': 'Content Unavailable', |
4fe4bda2 RA |
61 | }, { |
62 | 'url': 'http://tubitv.com/tv-shows/321886/s01_e01_on_nom_stories', | |
63 | 'only_matching': True, | |
29f7c58a | 64 | }, { |
65 | 'url': 'https://tubitv.com/movies/560057/penitentiary?start=true', | |
66 | 'info_dict': { | |
67 | 'id': '560057', | |
68 | 'ext': 'mp4', | |
69 | 'title': 'Penitentiary', | |
70 | 'description': 'md5:8d2fc793a93cc1575ff426fdcb8dd3f9', | |
71 | 'uploader_id': 'd8fed30d4f24fcb22ec294421b9defc2', | |
72 | 'release_year': 1979, | |
73 | }, | |
add96eb9 | 74 | 'skip': 'Content Unavailable', |
4fe4bda2 | 75 | }] |
5196b988 | 76 | |
3b55aaac | 77 | # DRM formats are included only to raise appropriate error |
78 | _UNPLAYABLE_FORMATS = ('hlsv6_widevine', 'hlsv6_widevine_nonclearlead', 'hlsv6_playready_psshv0', | |
79 | 'hlsv6_fairplay', 'dash_widevine', 'dash_widevine_nonclearlead') | |
80 | ||
52efa4b3 | 81 | def _perform_login(self, username, password): |
5196b988 NJ |
82 | self.report_login() |
83 | form_data = { | |
84 | 'username': username, | |
85 | 'password': password, | |
86 | } | |
6e6bc8da | 87 | payload = urlencode_postdata(form_data) |
3d2623a8 | 88 | request = Request(self._LOGIN_URL, payload) |
89 | request.headers['Content-Type'] = 'application/x-www-form-urlencoded' | |
5196b988 NJ |
90 | login_page = self._download_webpage( |
91 | request, None, False, 'Wrong login info') | |
92 | if not re.search(r'id="tubi-logout"', login_page): | |
93 | raise ExtractorError( | |
94 | 'Login failed (invalid username/password)', expected=True) | |
95 | ||
5196b988 | 96 | def _real_extract(self, url): |
bef9a9e5 BG |
97 | video_id, video_type = self._match_valid_url(url).group('id', 'type') |
98 | webpage = self._download_webpage(f'https://tubitv.com/{video_type}/{video_id}/', video_id) | |
99 | video_data = self._search_json( | |
100 | r'window\.__data\s*=', webpage, 'data', video_id, | |
101 | transform_source=js_to_json)['video']['byId'][video_id] | |
5196b988 | 102 | |
cf9d6cfb | 103 | formats = [] |
3b55aaac | 104 | drm_formats = False |
ffcd62c2 | 105 | |
bef9a9e5 BG |
106 | for resource in traverse_obj(video_data, ('video_resources', lambda _, v: url_or_none(v['manifest']['url']))): |
107 | resource_type = resource.get('type') | |
108 | manifest_url = resource['manifest']['url'] | |
109 | if resource_type == 'dash': | |
110 | formats.extend(self._extract_mpd_formats(manifest_url, video_id, mpd_id=resource_type, fatal=False)) | |
111 | elif resource_type in ('hlsv3', 'hlsv6'): | |
112 | formats.extend(self._extract_m3u8_formats(manifest_url, video_id, 'mp4', m3u8_id=resource_type, fatal=False)) | |
113 | elif resource_type in self._UNPLAYABLE_FORMATS: | |
3b55aaac | 114 | drm_formats = True |
bef9a9e5 BG |
115 | else: |
116 | self.report_warning(f'Skipping unknown resource type "{resource_type}"') | |
3b55aaac | 117 | |
118 | if not formats and drm_formats: | |
119 | self.report_drm(video_id) | |
120 | elif not formats and not video_data.get('policy_match'): # policy_match is False if content was removed | |
121 | raise ExtractorError('This content is currently unavailable', expected=True) | |
ffcd62c2 | 122 | |
9260cf1d | 123 | subtitles = {} |
bef9a9e5 | 124 | for sub in traverse_obj(video_data, ('subtitles', lambda _, v: url_or_none(v['url']))): |
f4dfa9a5 | 125 | subtitles.setdefault(sub.get('lang', 'English'), []).append({ |
bef9a9e5 | 126 | 'url': self._proto_relative_url(sub['url']), |
9260cf1d | 127 | }) |
128 | ||
bef9a9e5 | 129 | title = traverse_obj(video_data, ('title', {str})) |
febff4c1 B |
130 | season_number, episode_number, episode_title = self._search_regex( |
131 | r'^S(\d+):E(\d+) - (.+)', title, 'episode info', fatal=False, group=(1, 2, 3), default=(None, None, None)) | |
132 | ||
5196b988 NJ |
133 | return { |
134 | 'id': video_id, | |
135 | 'title': title, | |
136 | 'formats': formats, | |
9260cf1d | 137 | 'subtitles': subtitles, |
febff4c1 B |
138 | 'season_number': int_or_none(season_number), |
139 | 'episode_number': int_or_none(episode_number), | |
bef9a9e5 BG |
140 | 'episode': episode_title, |
141 | **traverse_obj(video_data, { | |
142 | 'description': ('description', {str}), | |
143 | 'duration': ('duration', {int_or_none}), | |
144 | 'uploader_id': ('publisher_id', {str}), | |
145 | 'release_year': ('year', {int_or_none}), | |
146 | 'thumbnails': ('thumbnails', ..., {url_or_none}, {'url': {self._proto_relative_url}}), | |
147 | }), | |
5196b988 | 148 | } |
b5be6dd5 A |
149 | |
150 | ||
151 | class TubiTvShowIE(InfoExtractor): | |
d7d86181 | 152 | IE_NAME = 'tubitv:series' |
153 | _VALID_URL = r'https?://(?:www\.)?tubitv\.com/series/\d+/(?P<show_name>[^/?#]+)(?:/season-(?P<season>\d+))?' | |
b5be6dd5 A |
154 | _TESTS = [{ |
155 | 'url': 'https://tubitv.com/series/3936/the-joy-of-painting-with-bob-ross?start=true', | |
d7d86181 | 156 | 'playlist_mincount': 389, |
b5be6dd5 A |
157 | 'info_dict': { |
158 | 'id': 'the-joy-of-painting-with-bob-ross', | |
add96eb9 | 159 | }, |
d7d86181 | 160 | }, { |
161 | 'url': 'https://tubitv.com/series/2311/the-saddle-club/season-1', | |
162 | 'playlist_count': 26, | |
163 | 'info_dict': { | |
164 | 'id': 'the-saddle-club-season-1', | |
165 | }, | |
166 | }, { | |
167 | 'url': 'https://tubitv.com/series/2311/the-saddle-club/season-3', | |
168 | 'playlist_count': 19, | |
169 | 'info_dict': { | |
170 | 'id': 'the-saddle-club-season-3', | |
171 | }, | |
172 | }, { | |
173 | 'url': 'https://tubitv.com/series/2311/the-saddle-club/', | |
174 | 'playlist_mincount': 71, | |
175 | 'info_dict': { | |
176 | 'id': 'the-saddle-club', | |
177 | }, | |
b5be6dd5 A |
178 | }] |
179 | ||
d7d86181 | 180 | def _entries(self, show_url, playlist_id, selected_season): |
181 | webpage = self._download_webpage(show_url, playlist_id) | |
182 | ||
183 | data = self._search_json( | |
184 | r'window\.__data\s*=', webpage, 'data', playlist_id, | |
185 | transform_source=js_to_json)['video'] | |
febff4c1 | 186 | |
d7d86181 | 187 | # v['number'] is already a decimal string, but stringify to protect against API changes |
188 | path = [lambda _, v: str(v['number']) == selected_season] if selected_season else [..., {dict}] | |
febff4c1 | 189 | |
d7d86181 | 190 | for season in traverse_obj(data, ('byId', lambda _, v: v['type'] == 's', 'seasons', *path)): |
191 | season_number = int_or_none(season.get('number')) | |
192 | for episode in traverse_obj(season, ('episodes', lambda _, v: v['id'])): | |
193 | episode_id = episode['id'] | |
194 | yield self.url_result( | |
195 | f'https://tubitv.com/tv-shows/{episode_id}/', TubiTvIE, episode_id, | |
196 | season_number=season_number, episode_number=int_or_none(episode.get('num'))) | |
b5be6dd5 A |
197 | |
198 | def _real_extract(self, url): | |
d7d86181 | 199 | playlist_id, selected_season = self._match_valid_url(url).group('show_name', 'season') |
200 | if selected_season: | |
201 | playlist_id = f'{playlist_id}-season-{selected_season}' | |
202 | return self.playlist_result(self._entries(url, playlist_id, selected_season), playlist_id) |