[ie/wistia] Support password-protected videos (#11100)
[yt-dlp3.git] / yt_dlp / extractor / hketv.py
blob3998abc121cd09a5e3a62bdac030ea2efb0a9490
1 from .common import InfoExtractor
2 from ..utils import (
3 ExtractorError,
4 clean_html,
5 int_or_none,
6 merge_dicts,
7 parse_count,
8 str_or_none,
9 try_get,
10 unified_strdate,
11 urlencode_postdata,
12 urljoin,
16 class HKETVIE(InfoExtractor):
17 IE_NAME = 'hketv'
18 IE_DESC = '香港教育局教育電視 (HKETV) Educational Television, Hong Kong Educational Bureau'
19 _GEO_BYPASS = False
20 _GEO_COUNTRIES = ['HK']
21 _VALID_URL = r'https?://(?:www\.)?hkedcity\.net/etv/resource/(?P<id>[0-9]+)'
22 _TESTS = [{
23 'url': 'https://www.hkedcity.net/etv/resource/2932360618',
24 'md5': 'f193712f5f7abb208ddef3c5ea6ed0b7',
25 'info_dict': {
26 'id': '2932360618',
27 'ext': 'mp4',
28 'title': '喜閱一生(共享閱讀樂) (中、英文字幕可供選擇)',
29 'description': 'md5:d5286d05219ef50e0613311cbe96e560',
30 'upload_date': '20181024',
31 'duration': 900,
32 'subtitles': 'count:2',
34 'skip': 'Geo restricted to HK',
35 }, {
36 'url': 'https://www.hkedcity.net/etv/resource/972641418',
37 'md5': '1ed494c1c6cf7866a8290edad9b07dc9',
38 'info_dict': {
39 'id': '972641418',
40 'ext': 'mp4',
41 'title': '衣冠楚楚 (天使系列之一)',
42 'description': 'md5:10bb3d659421e74f58e5db5691627b0f',
43 'upload_date': '20070109',
44 'duration': 907,
45 'subtitles': {},
47 'skip': 'Geo restricted to HK',
50 _CC_LANGS = {
51 '中文(繁體中文)': 'zh-Hant',
52 '中文(简体中文)': 'zh-Hans',
53 'English': 'en',
54 'Bahasa Indonesia': 'id',
55 '\u0939\u093f\u0928\u094d\u0926\u0940': 'hi',
56 '\u0928\u0947\u092a\u093e\u0932\u0940': 'ne',
57 'Tagalog': 'tl',
58 '\u0e44\u0e17\u0e22': 'th',
59 '\u0627\u0631\u062f\u0648': 'ur',
61 _FORMAT_HEIGHTS = {
62 'SD': 360,
63 'HD': 720,
65 _APPS_BASE_URL = 'https://apps.hkedcity.net'
67 def _real_extract(self, url):
68 video_id = self._match_id(url)
69 webpage = self._download_webpage(url, video_id)
71 title = (
72 self._html_search_meta(
73 ('ed_title', 'search.ed_title'), webpage, default=None)
74 or self._search_regex(
75 r'data-favorite_title_(?:eng|chi)=(["\'])(?P<id>(?:(?!\1).)+)\1',
76 webpage, 'title', default=None, group='url')
77 or self._html_search_regex(
78 r'<h1>([^<]+)</h1>', webpage, 'title', default=None)
79 or self._og_search_title(webpage)
82 file_id = self._search_regex(
83 r'post_var\[["\']file_id["\']\s*\]\s*=\s*(.+?);',
84 webpage, 'file ID')
85 curr_url = self._search_regex(
86 r'post_var\[["\']curr_url["\']\s*\]\s*=\s*"(.+?)";',
87 webpage, 'curr URL')
88 data = {
89 'action': 'get_info',
90 'curr_url': curr_url,
91 'file_id': file_id,
92 'video_url': file_id,
95 response = self._download_json(
96 self._APPS_BASE_URL + '/media/play/handler.php', video_id,
97 data=urlencode_postdata(data),
98 headers=merge_dicts({
99 'Content-Type': 'application/x-www-form-urlencoded'},
100 self.geo_verification_headers()))
102 result = response['result']
104 if not response.get('success') or not response.get('access'):
105 error = clean_html(response.get('access_err_msg'))
106 if 'Video streaming is not available in your country' in error:
107 self.raise_geo_restricted(
108 msg=error, countries=self._GEO_COUNTRIES)
109 else:
110 raise ExtractorError(error, expected=True)
112 formats = []
114 width = int_or_none(result.get('width'))
115 height = int_or_none(result.get('height'))
117 playlist0 = result['playlist'][0]
118 for fmt in playlist0['sources']:
119 file_url = urljoin(self._APPS_BASE_URL, fmt.get('file'))
120 if not file_url:
121 continue
122 # If we ever wanted to provide the final resolved URL that
123 # does not require cookies, albeit with a shorter lifespan:
124 # urlh = self._downloader.urlopen(file_url)
125 # resolved_url = urlh.url
126 label = fmt.get('label')
127 h = self._FORMAT_HEIGHTS.get(label)
128 w = h * width // height if h and width and height else None
129 formats.append({
130 'format_id': label,
131 'ext': fmt.get('type'),
132 'url': file_url,
133 'width': w,
134 'height': h,
137 subtitles = {}
138 tracks = try_get(playlist0, lambda x: x['tracks'], list) or []
139 for track in tracks:
140 if not isinstance(track, dict):
141 continue
142 track_kind = str_or_none(track.get('kind'))
143 if not track_kind or not isinstance(track_kind, str):
144 continue
145 if track_kind.lower() not in ('captions', 'subtitles'):
146 continue
147 track_url = urljoin(self._APPS_BASE_URL, track.get('file'))
148 if not track_url:
149 continue
150 track_label = track.get('label')
151 subtitles.setdefault(self._CC_LANGS.get(
152 track_label, track_label), []).append({
153 'url': self._proto_relative_url(track_url),
154 'ext': 'srt',
157 # Likes
158 emotion = self._download_json(
159 'https://emocounter.hkedcity.net/handler.php', video_id,
160 data=urlencode_postdata({
161 'action': 'get_emotion',
162 'data[bucket_id]': 'etv',
163 'data[identifier]': video_id,
165 headers={'Content-Type': 'application/x-www-form-urlencoded'},
166 fatal=False) or {}
167 like_count = int_or_none(try_get(
168 emotion, lambda x: x['data']['emotion_data'][0]['count']))
170 return {
171 'id': video_id,
172 'title': title,
173 'description': self._html_search_meta(
174 'description', webpage, fatal=False),
175 'upload_date': unified_strdate(self._html_search_meta(
176 'ed_date', webpage, fatal=False), day_first=False),
177 'duration': int_or_none(result.get('length')),
178 'formats': formats,
179 'subtitles': subtitles,
180 'thumbnail': urljoin(self._APPS_BASE_URL, result.get('image')),
181 'view_count': parse_count(result.get('view_count')),
182 'like_count': like_count,