[ie/wistia] Support password-protected videos (#11100)
[yt-dlp3.git] / yt_dlp / extractor / zaiko.py
blob4563b7ba07bd753b944848dd2b0d72ed1527f043
1 import base64
3 from .common import InfoExtractor
4 from ..utils import (
5 ExtractorError,
6 extract_attributes,
7 int_or_none,
8 str_or_none,
9 traverse_obj,
10 try_call,
11 unescapeHTML,
12 url_basename,
13 url_or_none,
17 class ZaikoBaseIE(InfoExtractor):
18 def _download_real_webpage(self, url, video_id):
19 webpage, urlh = self._download_webpage_handle(url, video_id)
20 final_url = urlh.url
21 if 'zaiko.io/login' in final_url:
22 self.raise_login_required()
23 elif '/_buy/' in final_url:
24 raise ExtractorError('Your account does not have tickets to this event', expected=True)
25 return webpage
27 def _parse_vue_element_attr(self, name, string, video_id):
28 page_elem = self._search_regex(rf'(<{name}[^>]+>)', string, name)
29 attrs = {}
30 for key, value in extract_attributes(page_elem).items():
31 if key.startswith(':'):
32 attrs[key[1:]] = self._parse_json(
33 value, video_id, transform_source=unescapeHTML, fatal=False)
34 return attrs
37 class ZaikoIE(ZaikoBaseIE):
38 _VALID_URL = r'https?://(?:[\w-]+\.)?zaiko\.io/event/(?P<id>\d+)/stream(?:/\d+)+'
39 _TESTS = [{
40 'url': 'https://zaiko.io/event/324868/stream/20571/20571',
41 'info_dict': {
42 'id': '324868',
43 'ext': 'mp4',
44 'title': 'ZAIKO STREAMING TEST',
45 'alt_title': '[VOD] ZAIKO STREAMING TEST_20210603(Do Not Delete)',
46 'uploader_id': '454',
47 'uploader': 'ZAIKO ZERO',
48 'release_timestamp': 1583809200,
49 'thumbnail': r're:^https://[\w.-]+/\w+/\w+',
50 'thumbnails': 'maxcount:2',
51 'release_date': '20200310',
52 'categories': ['Tech House'],
53 'live_status': 'was_live',
55 'params': {'skip_download': 'm3u8'},
56 'skip': 'Your account does not have tickets to this event',
59 def _real_extract(self, url):
60 video_id = self._match_id(url)
62 webpage = self._download_real_webpage(url, video_id)
63 stream_meta = self._parse_vue_element_attr('stream-page', webpage, video_id)
65 player_page = self._download_webpage(
66 stream_meta['stream-access']['video_source'], video_id,
67 'Downloading player page', headers={'referer': 'https://zaiko.io/'})
68 player_meta = self._parse_vue_element_attr('player', player_page, video_id)
69 initial_event_info = traverse_obj(player_meta, ('initial_event_info', {dict})) or {}
71 status = traverse_obj(initial_event_info, ('status', {str}))
72 live_status, msg, expected = {
73 'vod': ('was_live', 'No VOD stream URL was found', False),
74 'archiving': ('post_live', 'Event VOD is still being processed', True),
75 'deleting': ('post_live', 'This event has ended', True),
76 'deleted': ('post_live', 'This event has ended', True),
77 'error': ('post_live', 'This event has ended', True),
78 'disconnected': ('post_live', 'Stream has been disconnected', True),
79 'live_to_disconnected': ('post_live', 'Stream has been disconnected', True),
80 'live': ('is_live', 'No livestream URL found was found', False),
81 'waiting': ('is_upcoming', 'Live event has not yet started', True),
82 'cancelled': ('not_live', 'Event has been cancelled', True),
83 }.get(status) or ('not_live', f'Unknown event status "{status}"', False)
85 if traverse_obj(initial_event_info, ('is_jwt_protected', {bool})):
86 stream_url = self._download_json(
87 initial_event_info['jwt_token_url'], video_id, 'Downloading JWT-protected stream URL',
88 'Failed to download JWT-protected stream URL')['playback_url']
89 else:
90 stream_url = traverse_obj(initial_event_info, ('endpoint', {url_or_none}))
92 formats = self._extract_m3u8_formats(
93 stream_url, video_id, live=True, fatal=False) if stream_url else []
94 if not formats:
95 self.raise_no_formats(msg, expected=expected)
97 thumbnail_urls = [
98 traverse_obj(initial_event_info, ('poster_url', {url_or_none})),
99 self._og_search_thumbnail(self._download_webpage(
100 f'https://zaiko.io/event/{video_id}', video_id, 'Downloading event page', fatal=False) or ''),
103 return {
104 'id': video_id,
105 'formats': formats,
106 'live_status': live_status,
107 **traverse_obj(stream_meta, {
108 'title': ('event', 'name', {str}),
109 'uploader': ('profile', 'name', {str}),
110 'uploader_id': ('profile', 'id', {str_or_none}),
111 'release_timestamp': ('stream', 'start', 'timestamp', {int_or_none}),
112 'categories': ('event', 'genres', ..., {lambda x: x or None}),
114 'alt_title': traverse_obj(initial_event_info, ('title', {str})),
115 'thumbnails': [{'url': url, 'id': url_basename(url)} for url in thumbnail_urls if url_or_none(url)],
119 class ZaikoETicketIE(ZaikoBaseIE):
120 _VALID_URL = r'https?://(?:www.)?zaiko\.io/account/eticket/(?P<id>[\w=-]{49})'
121 _TESTS = [{
122 'url': 'https://zaiko.io/account/eticket/TZjMwMzQ2Y2EzMXwyMDIzMDYwNzEyMTMyNXw1MDViOWU2Mw==',
123 'playlist_count': 1,
124 'info_dict': {
125 'id': 'f30346ca31-20230607121325-505b9e63',
126 'title': 'ZAIKO STREAMING TEST',
127 'thumbnail': 'https://media.zkocdn.net/pf_1/1_3wdyjcjyupseatkwid34u',
129 'skip': 'Only available with the ticketholding account',
132 def _real_extract(self, url):
133 ticket_id = self._match_id(url)
134 ticket_id = try_call(
135 lambda: base64.urlsafe_b64decode(ticket_id[1:]).decode().replace('|', '-')) or ticket_id
137 webpage = self._download_real_webpage(url, ticket_id)
138 eticket = self._parse_vue_element_attr('eticket', webpage, ticket_id)
140 return self.playlist_result(
141 [self.url_result(stream, ZaikoIE) for stream in traverse_obj(eticket, ('streams', ..., 'url'))],
142 ticket_id, **traverse_obj(eticket, ('ticket-details', {
143 'title': 'event_name',
144 'thumbnail': 'event_img_url',
145 })))