]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/hketv.py
[ie/youtube] Suppress "Unavailable videos are hidden" warning (#10159)
[yt-dlp.git] / yt_dlp / extractor / hketv.py
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,
13 )
14
15
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',
33 },
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': {},
46 },
47 'params': {
48 'geo_verification_proxy': '<HK proxy here>',
49 },
50 'skip': 'Geo restricted to HK',
51 }]
52
53 _CC_LANGS = {
54 '中文(繁體中文)': 'zh-Hant',
55 '中文(简体中文)': 'zh-Hans',
56 'English': 'en',
57 'Bahasa Indonesia': 'id',
58 '\u0939\u093f\u0928\u094d\u0926\u0940': 'hi',
59 '\u0928\u0947\u092a\u093e\u0932\u0940': 'ne',
60 'Tagalog': 'tl',
61 '\u0e44\u0e17\u0e22': 'th',
62 '\u0627\u0631\u062f\u0648': 'ur',
63 }
64 _FORMAT_HEIGHTS = {
65 'SD': 360,
66 'HD': 720,
67 }
68 _APPS_BASE_URL = 'https://apps.hkedcity.net'
69
70 def _real_extract(self, url):
71 video_id = self._match_id(url)
72 webpage = self._download_webpage(url, video_id)
73
74 title = (
75 self._html_search_meta(
76 ('ed_title', 'search.ed_title'), webpage, default=None)
77 or self._search_regex(
78 r'data-favorite_title_(?:eng|chi)=(["\'])(?P<id>(?:(?!\1).)+)\1',
79 webpage, 'title', default=None, group='url')
80 or self._html_search_regex(
81 r'<h1>([^<]+)</h1>', webpage, 'title', default=None)
82 or self._og_search_title(webpage)
83 )
84
85 file_id = self._search_regex(
86 r'post_var\[["\']file_id["\']\s*\]\s*=\s*(.+?);',
87 webpage, 'file ID')
88 curr_url = self._search_regex(
89 r'post_var\[["\']curr_url["\']\s*\]\s*=\s*"(.+?)";',
90 webpage, 'curr URL')
91 data = {
92 'action': 'get_info',
93 'curr_url': curr_url,
94 'file_id': file_id,
95 'video_url': file_id,
96 }
97
98 response = self._download_json(
99 self._APPS_BASE_URL + '/media/play/handler.php', video_id,
100 data=urlencode_postdata(data),
101 headers=merge_dicts({
102 'Content-Type': 'application/x-www-form-urlencoded'},
103 self.geo_verification_headers()))
104
105 result = response['result']
106
107 if not response.get('success') or not response.get('access'):
108 error = clean_html(response.get('access_err_msg'))
109 if 'Video streaming is not available in your country' in error:
110 self.raise_geo_restricted(
111 msg=error, countries=self._GEO_COUNTRIES)
112 else:
113 raise ExtractorError(error, expected=True)
114
115 formats = []
116
117 width = int_or_none(result.get('width'))
118 height = int_or_none(result.get('height'))
119
120 playlist0 = result['playlist'][0]
121 for fmt in playlist0['sources']:
122 file_url = urljoin(self._APPS_BASE_URL, fmt.get('file'))
123 if not file_url:
124 continue
125 # If we ever wanted to provide the final resolved URL that
126 # does not require cookies, albeit with a shorter lifespan:
127 # urlh = self._downloader.urlopen(file_url)
128 # resolved_url = urlh.url
129 label = fmt.get('label')
130 h = self._FORMAT_HEIGHTS.get(label)
131 w = h * width // height if h and width and height else None
132 formats.append({
133 'format_id': label,
134 'ext': fmt.get('type'),
135 'url': file_url,
136 'width': w,
137 'height': h,
138 })
139
140 subtitles = {}
141 tracks = try_get(playlist0, lambda x: x['tracks'], list) or []
142 for track in tracks:
143 if not isinstance(track, dict):
144 continue
145 track_kind = str_or_none(track.get('kind'))
146 if not track_kind or not isinstance(track_kind, str):
147 continue
148 if track_kind.lower() not in ('captions', 'subtitles'):
149 continue
150 track_url = urljoin(self._APPS_BASE_URL, track.get('file'))
151 if not track_url:
152 continue
153 track_label = track.get('label')
154 subtitles.setdefault(self._CC_LANGS.get(
155 track_label, track_label), []).append({
156 'url': self._proto_relative_url(track_url),
157 'ext': 'srt',
158 })
159
160 # Likes
161 emotion = self._download_json(
162 'https://emocounter.hkedcity.net/handler.php', video_id,
163 data=urlencode_postdata({
164 'action': 'get_emotion',
165 'data[bucket_id]': 'etv',
166 'data[identifier]': video_id,
167 }),
168 headers={'Content-Type': 'application/x-www-form-urlencoded'},
169 fatal=False) or {}
170 like_count = int_or_none(try_get(
171 emotion, lambda x: x['data']['emotion_data'][0]['count']))
172
173 return {
174 'id': video_id,
175 'title': title,
176 'description': self._html_search_meta(
177 'description', webpage, fatal=False),
178 'upload_date': unified_strdate(self._html_search_meta(
179 'ed_date', webpage, fatal=False), day_first=False),
180 'duration': int_or_none(result.get('length')),
181 'formats': formats,
182 'subtitles': subtitles,
183 'thumbnail': urljoin(self._APPS_BASE_URL, result.get('image')),
184 'view_count': parse_count(result.get('view_count')),
185 'like_count': like_count,
186 }