[ie/wistia] Support password-protected videos (#11100)
[yt-dlp3.git] / yt_dlp / extractor / toggle.py
blobfbef7cc0f2b426d158c34aba3ea1073c0946f393
1 import json
2 import re
4 from .common import InfoExtractor
5 from ..utils import (
6 determine_ext,
7 float_or_none,
8 int_or_none,
9 parse_iso8601,
10 strip_or_none,
14 class ToggleIE(InfoExtractor):
15 IE_NAME = 'toggle'
16 _VALID_URL = r'(?:https?://(?:(?:www\.)?mewatch|video\.toggle)\.sg/(?:en|zh)/(?:[^/]+/){2,}|toggle:)(?P<id>[0-9]+)'
17 _TESTS = [{
18 'url': 'http://www.mewatch.sg/en/series/lion-moms-tif/trailers/lion-moms-premier/343115',
19 'info_dict': {
20 'id': '343115',
21 'ext': 'mp4',
22 'title': 'Lion Moms Premiere',
23 'description': 'md5:aea1149404bff4d7f7b6da11fafd8e6b',
24 'upload_date': '20150910',
25 'timestamp': 1441858274,
27 'params': {
28 'skip_download': 'm3u8 download',
30 }, {
31 'url': 'http://www.mewatch.sg/en/movies/dug-s-special-mission/341413',
32 'only_matching': True,
33 }, {
34 'url': 'http://www.mewatch.sg/en/series/28th-sea-games-5-show/28th-sea-games-5-show-ep11/332861',
35 'only_matching': True,
36 }, {
37 'url': 'http://video.toggle.sg/en/clips/seraph-sun-aloysius-will-suddenly-sing-some-old-songs-in-high-pitch-on-set/343331',
38 'only_matching': True,
39 }, {
40 'url': 'http://www.mewatch.sg/en/clips/seraph-sun-aloysius-will-suddenly-sing-some-old-songs-in-high-pitch-on-set/343331',
41 'only_matching': True,
42 }, {
43 'url': 'http://www.mewatch.sg/zh/series/zero-calling-s2-hd/ep13/336367',
44 'only_matching': True,
45 }, {
46 'url': 'http://www.mewatch.sg/en/series/vetri-s2/webisodes/jeeva-is-an-orphan-vetri-s2-webisode-7/342302',
47 'only_matching': True,
48 }, {
49 'url': 'http://www.mewatch.sg/en/movies/seven-days/321936',
50 'only_matching': True,
51 }, {
52 'url': 'https://www.mewatch.sg/en/tv-show/news/may-2017-cna-singapore-tonight/fri-19-may-2017/512456',
53 'only_matching': True,
54 }, {
55 'url': 'http://www.mewatch.sg/en/channels/eleven-plus/401585',
56 'only_matching': True,
59 _API_USER = 'tvpapi_147'
60 _API_PASS = '11111'
62 def _real_extract(self, url):
63 video_id = self._match_id(url)
65 params = {
66 'initObj': {
67 'Locale': {
68 'LocaleLanguage': '',
69 'LocaleCountry': '',
70 'LocaleDevice': '',
71 'LocaleUserState': 0,
73 'Platform': 0,
74 'SiteGuid': 0,
75 'DomainID': '0',
76 'UDID': '',
77 'ApiUser': self._API_USER,
78 'ApiPass': self._API_PASS,
80 'MediaID': video_id,
81 'mediaType': 0,
84 info = self._download_json(
85 'http://tvpapi.as.tvinci.com/v2_9/gateways/jsonpostgw.aspx?m=GetMediaInfo',
86 video_id, 'Downloading video info json', data=json.dumps(params).encode())
88 title = info['MediaName']
90 formats = []
91 for video_file in info.get('Files', []):
92 video_url, vid_format = video_file.get('URL'), video_file.get('Format')
93 if not video_url or video_url == 'NA' or not vid_format:
94 continue
95 ext = determine_ext(video_url)
96 vid_format = vid_format.replace(' ', '')
97 # if geo-restricted, m3u8 is inaccessible, but mp4 is okay
98 if ext == 'm3u8':
99 m3u8_formats = self._extract_m3u8_formats(
100 video_url, video_id, ext='mp4', m3u8_id=vid_format,
101 note=f'Downloading {vid_format} m3u8 information',
102 errnote=f'Failed to download {vid_format} m3u8 information',
103 fatal=False)
104 for f in m3u8_formats:
105 # Apple FairPlay Streaming
106 if '/fpshls/' in f['url']:
107 continue
108 formats.append(f)
109 elif ext == 'mpd':
110 formats.extend(self._extract_mpd_formats(
111 video_url, video_id, mpd_id=vid_format,
112 note=f'Downloading {vid_format} MPD manifest',
113 errnote=f'Failed to download {vid_format} MPD manifest',
114 fatal=False))
115 elif ext == 'ism':
116 formats.extend(self._extract_ism_formats(
117 video_url, video_id, ism_id=vid_format,
118 note=f'Downloading {vid_format} ISM manifest',
119 errnote=f'Failed to download {vid_format} ISM manifest',
120 fatal=False))
121 elif ext == 'mp4':
122 formats.append({
123 'ext': ext,
124 'url': video_url,
125 'format_id': vid_format,
127 if not formats:
128 for meta in (info.get('Metas') or []):
129 if (not self.get_param('allow_unplayable_formats')
130 and meta.get('Key') == 'Encryption' and meta.get('Value') == '1'):
131 self.report_drm(video_id)
132 # Most likely because geo-blocked if no formats and no DRM
134 thumbnails = []
135 for picture in info.get('Pictures', []):
136 if not isinstance(picture, dict):
137 continue
138 pic_url = picture.get('URL')
139 if not pic_url:
140 continue
141 thumbnail = {
142 'url': pic_url,
144 pic_size = picture.get('PicSize', '')
145 m = re.search(r'(?P<width>\d+)[xX](?P<height>\d+)', pic_size)
146 if m:
147 thumbnail.update({
148 'width': int(m.group('width')),
149 'height': int(m.group('height')),
151 thumbnails.append(thumbnail)
153 def counter(prefix):
154 return int_or_none(
155 info.get(prefix + 'Counter') or info.get(prefix.lower() + '_counter'))
157 return {
158 'id': video_id,
159 'title': title,
160 'description': strip_or_none(info.get('Description')),
161 'duration': int_or_none(info.get('Duration')),
162 'timestamp': parse_iso8601(info.get('CreationDate') or None),
163 'average_rating': float_or_none(info.get('Rating')),
164 'view_count': counter('View'),
165 'like_count': counter('Like'),
166 'thumbnails': thumbnails,
167 'formats': formats,
171 class MeWatchIE(InfoExtractor):
172 IE_NAME = 'mewatch'
173 _VALID_URL = r'https?://(?:(?:www|live)\.)?mewatch\.sg/watch/[^/?#&]+-(?P<id>[0-9]+)'
174 _TESTS = [{
175 'url': 'https://www.mewatch.sg/watch/Recipe-Of-Life-E1-179371',
176 'info_dict': {
177 'id': '1008625',
178 'ext': 'mp4',
179 'title': 'Recipe Of Life 味之道',
180 'timestamp': 1603306526,
181 'description': 'md5:6e88cde8af2068444fc8e1bc3ebf257c',
182 'upload_date': '20201021',
184 'params': {
185 'skip_download': 'm3u8 download',
187 }, {
188 'url': 'https://www.mewatch.sg/watch/Little-Red-Dot-Detectives-S2-搜密。打卡。小红点-S2-E1-176232',
189 'only_matching': True,
190 }, {
191 'url': 'https://www.mewatch.sg/watch/Little-Red-Dot-Detectives-S2-%E6%90%9C%E5%AF%86%E3%80%82%E6%89%93%E5%8D%A1%E3%80%82%E5%B0%8F%E7%BA%A2%E7%82%B9-S2-E1-176232',
192 'only_matching': True,
193 }, {
194 'url': 'https://live.mewatch.sg/watch/Recipe-Of-Life-E41-189759',
195 'only_matching': True,
198 def _real_extract(self, url):
199 item_id = self._match_id(url)
200 custom_id = self._download_json(
201 'https://cdn.mewatch.sg/api/items/' + item_id,
202 item_id, query={'segments': 'all'})['customId']
203 return self.url_result(
204 'toggle:' + custom_id, ToggleIE.ie_key(), custom_id)