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