[cleanup] Make more playlist entries lazy (#11763)
[yt-dlp.git] / yt_dlp / extractor / viewlift.py
blob4a7ba9839efc0324435af9250a240c32f1f59f4e
1 import json
3 from .common import InfoExtractor
4 from ..networking.exceptions import HTTPError
5 from ..utils import (
6 ExtractorError,
7 int_or_none,
8 join_nonempty,
9 parse_age_limit,
10 traverse_obj,
14 class ViewLiftBaseIE(InfoExtractor):
15 _API_BASE = 'https://prod-api.viewlift.com/'
16 _DOMAINS_REGEX = r'(?:(?:main\.)?snagfilms|snagxtreme|funnyforfree|kiddovid|winnersview|(?:monumental|lax)sportsnetwork|vayafilm|failarmy|ftfnext|lnppass\.legapallacanestro|moviespree|app\.myoutdoortv|neoufitness|pflmma|theidentitytb|chorki)\.com|(?:hoichoi|app\.horseandcountry|kronon|marquee|supercrosslive)\.tv'
17 _SITE_MAP = {
18 'ftfnext': 'lax',
19 'funnyforfree': 'snagfilms',
20 'hoichoi': 'hoichoitv',
21 'kiddovid': 'snagfilms',
22 'laxsportsnetwork': 'lax',
23 'legapallacanestro': 'lnp',
24 'marquee': 'marquee-tv',
25 'monumentalsportsnetwork': 'monumental-network',
26 'moviespree': 'bingeflix',
27 'pflmma': 'pfl',
28 'snagxtreme': 'snagfilms',
29 'theidentitytb': 'tampabay',
30 'vayafilm': 'snagfilms',
31 'chorki': 'prothomalo',
33 _TOKENS = {}
35 def _fetch_token(self, site, url):
36 if self._TOKENS.get(site):
37 return
39 cookies = self._get_cookies(url)
40 if cookies and cookies.get('token'):
41 self._TOKENS[site] = self._search_regex(r'22authorizationToken\%22:\%22([^\%]+)\%22', cookies['token'].value, 'token')
42 if not self._TOKENS.get(site):
43 self.raise_login_required('Cookies (not necessarily logged in) are needed to download from this website', method='cookies')
45 def _call_api(self, site, path, video_id, url, query):
46 self._fetch_token(site, url)
47 try:
48 return self._download_json(
49 self._API_BASE + path, video_id, headers={'Authorization': self._TOKENS.get(site)}, query=query)
50 except ExtractorError as e:
51 if isinstance(e.cause, HTTPError) and e.cause.status == 403:
52 webpage = e.cause.response.read().decode()
53 try:
54 error_message = traverse_obj(json.loads(webpage), 'errorMessage', 'message')
55 except json.JSONDecodeError:
56 raise ExtractorError(f'{site} said: {webpage}', cause=e.cause)
57 if error_message:
58 if 'has not purchased' in error_message:
59 self.raise_login_required(method='cookies')
60 raise ExtractorError(error_message, expected=True)
61 raise
64 class ViewLiftEmbedIE(ViewLiftBaseIE):
65 IE_NAME = 'viewlift:embed'
66 _VALID_URL = rf'https?://(?:(?:www|embed)\.)?(?P<domain>{ViewLiftBaseIE._DOMAINS_REGEX})/embed/player\?.*\bfilmId=(?P<id>[\da-f]{{8}}-(?:[\da-f]{{4}}-){{3}}[\da-f]{{12}})'
67 _EMBED_REGEX = [rf'<iframe[^>]+?src=(["\'])(?P<url>(?:https?:)?//(?:embed\.)?(?:{ViewLiftBaseIE._DOMAINS_REGEX})/embed/player.+?)\1']
68 _TESTS = [{
69 'url': 'http://embed.snagfilms.com/embed/player?filmId=74849a00-85a9-11e1-9660-123139220831&w=500',
70 'md5': '2924e9215c6eff7a55ed35b72276bd93',
71 'info_dict': {
72 'id': '74849a00-85a9-11e1-9660-123139220831',
73 'ext': 'mp4',
74 'title': '#whilewewatch',
75 'description': 'md5:b542bef32a6f657dadd0df06e26fb0c8',
76 'timestamp': 1334350096,
77 'upload_date': '20120413',
79 }, {
80 # invalid labels, 360p is better that 480p
81 'url': 'http://www.snagfilms.com/embed/player?filmId=17ca0950-a74a-11e0-a92a-0026bb61d036',
82 'md5': '882fca19b9eb27ef865efeeaed376a48',
83 'info_dict': {
84 'id': '17ca0950-a74a-11e0-a92a-0026bb61d036',
85 'ext': 'mp4',
86 'title': 'Life in Limbo',
88 'skip': 'The video does not exist',
89 }, {
90 'url': 'http://www.snagfilms.com/embed/player?filmId=0000014c-de2f-d5d6-abcf-ffef58af0017',
91 'only_matching': True,
94 def _real_extract(self, url):
95 domain, film_id = self._match_valid_url(url).groups()
96 site = domain.split('.')[-2]
97 if site in self._SITE_MAP:
98 site = self._SITE_MAP[site]
100 content_data = self._call_api(
101 site, 'entitlement/video/status', film_id, url, {
102 'id': film_id,
103 })['video']
104 gist = content_data['gist']
105 title = gist['title']
106 video_assets = content_data['streamingInfo']['videoAssets']
108 hls_url = video_assets.get('hls')
109 formats, subtitles = [], {}
110 if hls_url:
111 formats, subtitles = self._extract_m3u8_formats_and_subtitles(
112 hls_url, film_id, 'mp4', 'm3u8_native', m3u8_id='hls', fatal=False)
114 for video_asset in video_assets.get('mpeg') or []:
115 video_asset_url = video_asset.get('url')
116 if not video_asset_url:
117 continue
118 bitrate = int_or_none(video_asset.get('bitrate'))
119 height = int_or_none(self._search_regex(
120 r'^_?(\d+)[pP]$', video_asset.get('renditionValue'),
121 'height', default=None))
122 formats.append({
123 'url': video_asset_url,
124 'format_id': join_nonempty('http', bitrate),
125 'tbr': bitrate,
126 'height': height,
127 'vcodec': video_asset.get('codec'),
130 subs = {}
131 for sub in traverse_obj(content_data, ('contentDetails', 'closedCaptions')) or []:
132 sub_url = sub.get('url')
133 if not sub_url:
134 continue
135 subs.setdefault(sub.get('language', 'English'), []).append({
136 'url': sub_url,
139 return {
140 'id': film_id,
141 'title': title,
142 'description': gist.get('description'),
143 'thumbnail': gist.get('videoImageUrl'),
144 'duration': int_or_none(gist.get('runtime')),
145 'age_limit': parse_age_limit(content_data.get('parentalRating')),
146 'timestamp': int_or_none(gist.get('publishDate'), 1000),
147 'formats': formats,
148 'subtitles': self._merge_subtitles(subs, subtitles),
149 'categories': traverse_obj(content_data, ('categories', ..., 'title')),
150 'tags': traverse_obj(content_data, ('tags', ..., 'title')),
154 class ViewLiftIE(ViewLiftBaseIE):
155 IE_NAME = 'viewlift'
156 _API_BASE = 'https://prod-api-cached-2.viewlift.com/'
157 _VALID_URL = rf'https?://(?:www\.)?(?P<domain>{ViewLiftBaseIE._DOMAINS_REGEX})(?P<path>(?:/(?:films/title|show|(?:news/)?videos?|watch))?/(?P<id>[^?#]+))'
158 _TESTS = [{
159 'url': 'http://www.snagfilms.com/films/title/lost_for_life',
160 'md5': '19844f897b35af219773fd63bdec2942',
161 'info_dict': {
162 'id': '0000014c-de2f-d5d6-abcf-ffef58af0017',
163 'display_id': 'lost_for_life',
164 'ext': 'mp4',
165 'title': 'Lost for Life',
166 'description': 'md5:ea10b5a50405ae1f7b5269a6ec594102',
167 'thumbnail': r're:^https?://.*\.jpg',
168 'duration': 4489,
169 'categories': 'mincount:3',
170 'age_limit': 14,
171 'upload_date': '20150421',
172 'timestamp': 1429656820,
174 }, {
175 'url': 'http://www.snagfilms.com/show/the_world_cut_project/india',
176 'md5': 'e6292e5b837642bbda82d7f8bf3fbdfd',
177 'info_dict': {
178 'id': '00000145-d75c-d96e-a9c7-ff5c67b20000',
179 'display_id': 'the_world_cut_project/india',
180 'ext': 'mp4',
181 'title': 'India',
182 'description': 'md5:5c168c5a8f4719c146aad2e0dfac6f5f',
183 'thumbnail': r're:^https?://.*\.jpg',
184 'duration': 979,
185 'timestamp': 1399478279,
186 'upload_date': '20140507',
188 }, {
189 'url': 'http://main.snagfilms.com/augie_alone/s_2_ep_12_love',
190 'info_dict': {
191 'id': '00000148-7b53-de26-a9fb-fbf306f70020',
192 'display_id': 'augie_alone/s_2_ep_12_love',
193 'ext': 'mp4',
194 'title': 'S. 2 Ep. 12 - Love',
195 'description': 'Augie finds love.',
196 'thumbnail': r're:^https?://.*\.jpg',
197 'duration': 107,
198 'upload_date': '20141012',
199 'timestamp': 1413129540,
200 'age_limit': 17,
202 'params': {
203 'skip_download': True,
205 }, {
206 'url': 'http://main.snagfilms.com/films/title/the_freebie',
207 'only_matching': True,
208 }, {
209 # Film is not playable in your area.
210 'url': 'http://www.snagfilms.com/films/title/inside_mecca',
211 'only_matching': True,
212 }, {
213 # Film is not available.
214 'url': 'http://www.snagfilms.com/show/augie_alone/flirting',
215 'only_matching': True,
216 }, {
217 'url': 'http://www.winnersview.com/videos/the-good-son',
218 'only_matching': True,
219 }, {
220 # Was once Kaltura embed
221 'url': 'https://www.monumentalsportsnetwork.com/videos/john-carlson-postgame-2-25-15',
222 'only_matching': True,
223 }, {
224 'url': 'https://www.marquee.tv/watch/sadlerswells-sacredmonsters',
225 'only_matching': True,
226 }, { # Free film with langauge code
227 'url': 'https://www.hoichoi.tv/bn/films/title/shuyopoka',
228 'info_dict': {
229 'id': '7a7a9d33-1f4c-4771-9173-ee4fb6dbf196',
230 'ext': 'mp4',
231 'title': 'Shuyopoka',
232 'description': 'md5:e28f2fb8680096a69c944d37c1fa5ffc',
233 'thumbnail': r're:^https?://.*\.jpg$',
234 'upload_date': '20211006',
236 'params': {'skip_download': True},
237 }, { # Free film
238 'url': 'https://www.hoichoi.tv/films/title/dadu-no1',
239 'info_dict': {
240 'id': '0000015b-b009-d126-a1db-b81ff3780000',
241 'ext': 'mp4',
242 'title': 'Dadu No.1',
243 'description': 'md5:605cba408e51a79dafcb824bdeded51e',
244 'thumbnail': r're:^https?://.*\.jpg$',
245 'upload_date': '20210827',
247 'params': {'skip_download': True},
248 }, { # Free episode
249 'url': 'https://www.hoichoi.tv/webseries/case-jaundice-s01-e01',
250 'info_dict': {
251 'id': 'f779e07c-30c8-459c-8612-5a834ab5e5ba',
252 'ext': 'mp4',
253 'title': 'Humans Vs. Corona',
254 'description': 'md5:ca30a682b4528d02a3eb6d0427dd0f87',
255 'thumbnail': r're:^https?://.*\.jpg$',
256 'upload_date': '20210830',
257 'series': 'Case Jaundice',
259 'params': {'skip_download': True},
260 }, { # Free video
261 'url': 'https://www.hoichoi.tv/videos/1549072415320-six-episode-02-hindi',
262 'info_dict': {
263 'id': 'b41fa1ce-aca6-47b6-b208-283ff0a2de30',
264 'ext': 'mp4',
265 'title': 'Woman in red - Hindi',
266 'description': 'md5:9d21edc1827d32f8633eb67c2054fc31',
267 'thumbnail': r're:^https?://.*\.jpg$',
268 'upload_date': '20211006',
269 'series': 'Six (Hindi)',
271 'params': {'skip_download': True},
272 }, { # Free episode
273 'url': 'https://www.hoichoi.tv/shows/watch-asian-paints-moner-thikana-online-season-1-episode-1',
274 'info_dict': {
275 'id': '1f45d185-8500-455c-b88d-13252307c3eb',
276 'ext': 'mp4',
277 'title': 'Jisshu Sengupta',
278 'description': 'md5:ef6ffae01a3d83438597367400f824ed',
279 'thumbnail': r're:^https?://.*\.jpg$',
280 'upload_date': '20211004',
281 'series': 'Asian Paints Moner Thikana',
283 'params': {'skip_download': True},
284 }, { # Free series
285 'url': 'https://www.hoichoi.tv/shows/watch-moner-thikana-bengali-web-series-online',
286 'playlist_mincount': 5,
287 'info_dict': {
288 'id': 'watch-moner-thikana-bengali-web-series-online',
290 }, { # Premium series
291 'url': 'https://www.hoichoi.tv/shows/watch-byomkesh-bengali-web-series-online',
292 'playlist_mincount': 14,
293 'info_dict': {
294 'id': 'watch-byomkesh-bengali-web-series-online',
296 }, { # Premium movie
297 'url': 'https://www.hoichoi.tv/movies/detective-2020',
298 'only_matching': True,
299 }, { # Chorki Premium series
300 'url': 'https://www.chorki.com/bn/series/sinpaat',
301 'playlist_mincount': 7,
302 'info_dict': {
303 'id': 'bn/series/sinpaat',
305 }, { # Chorki free movie
306 'url': 'https://www.chorki.com/bn/videos/bangla-movie-bikkhov',
307 'info_dict': {
308 'id': '564e755b-f5c7-4515-aee6-8959bee18c93',
309 'title': 'Bikkhov',
310 'ext': 'mp4',
311 'upload_date': '20230824',
312 'timestamp': 1692860553,
313 'categories': ['Action Movies', 'Salman Special'],
314 'tags': 'count:14',
315 'thumbnail': 'https://snagfilms-a.akamaihd.net/dd078ff5-b16e-45e4-9723-501b56b9df0a/images/2023/08/24/1692860450729_1920x1080_16x9Images.jpg',
316 'display_id': 'bn/videos/bangla-movie-bikkhov',
317 'description': 'md5:71492b086450625f4374a3eb824f27dc',
318 'duration': 8002,
320 'params': {
321 'skip_download': True,
323 }, { # Chorki Premium movie
324 'url': 'https://www.chorki.com/bn/videos/something-like-an-autobiography',
325 'only_matching': True,
328 @classmethod
329 def suitable(cls, url):
330 return False if ViewLiftEmbedIE.suitable(url) else super().suitable(url)
332 def _show_entries(self, domain, seasons):
333 for season in seasons:
334 for episode in season.get('episodes') or []:
335 path = traverse_obj(episode, ('gist', 'permalink'))
336 if path:
337 yield self.url_result(f'https://www.{domain}{path}', ie=self.ie_key())
339 def _real_extract(self, url):
340 domain, path, display_id = self._match_valid_url(url).groups()
341 site = domain.split('.')[-2]
342 if site in self._SITE_MAP:
343 site = self._SITE_MAP[site]
344 modules = self._call_api(
345 site, 'content/pages', display_id, url, {
346 'includeContent': 'true',
347 'moduleOffset': 1,
348 'path': path,
349 'site': site,
350 })['modules']
352 seasons = next((m['contentData'][0]['seasons'] for m in modules if m.get('moduleType') == 'ShowDetailModule'), None)
353 if seasons:
354 return self.playlist_result(self._show_entries(domain, seasons), display_id)
356 film_id = next(m['contentData'][0]['gist']['id'] for m in modules if m.get('moduleType') == 'VideoDetailModule')
357 return {
358 '_type': 'url_transparent',
359 'url': f'http://{domain}/embed/player?filmId={film_id}',
360 'id': film_id,
361 'display_id': display_id,
362 'ie_key': 'ViewLiftEmbed',