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