]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/hketv.py
fix motherless
[yt-dlp.git] / yt_dlp / extractor / hketv.py
CommitLineData
289ef490 1from .common import InfoExtractor
289ef490 2from ..utils import (
289ef490 3 ExtractorError,
e897bd82 4 clean_html,
289ef490
AF
5 int_or_none,
6 merge_dicts,
73c19aaa 7 parse_count,
289ef490 8 str_or_none,
289ef490
AF
9 try_get,
10 unified_strdate,
11 urlencode_postdata,
12 urljoin,
13)
14
15
16class 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': '喜閱一生(共享閱讀樂) (中、英文字幕可供選擇)',
73c19aaa 29 'description': 'md5:d5286d05219ef50e0613311cbe96e560',
289ef490
AF
30 'upload_date': '20181024',
31 'duration': 900,
73c19aaa 32 'subtitles': 'count:2',
289ef490 33 },
73c19aaa 34 'skip': 'Geo restricted to HK',
289ef490
AF
35 }, {
36 'url': 'https://www.hkedcity.net/etv/resource/972641418',
37 'md5': '1ed494c1c6cf7866a8290edad9b07dc9',
38 'info_dict': {
39 'id': '972641418',
40 'ext': 'mp4',
41 'title': '衣冠楚楚 (天使系列之一)',
73c19aaa 42 'description': 'md5:10bb3d659421e74f58e5db5691627b0f',
289ef490
AF
43 'upload_date': '20070109',
44 'duration': 907,
45 'subtitles': {},
46 },
73c19aaa 47 'skip': 'Geo restricted to HK',
289ef490
AF
48 }]
49
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',
60 }
73c19aaa
S
61 _FORMAT_HEIGHTS = {
62 'SD': 360,
63 'HD': 720,
64 }
65 _APPS_BASE_URL = 'https://apps.hkedcity.net'
289ef490
AF
66
67 def _real_extract(self, url):
68 video_id = self._match_id(url)
69 webpage = self._download_webpage(url, video_id)
289ef490 70
73c19aaa
S
71 title = (
72 self._html_search_meta(
3089bc74
S
73 ('ed_title', 'search.ed_title'), webpage, default=None)
74 or self._search_regex(
73c19aaa 75 r'data-favorite_title_(?:eng|chi)=(["\'])(?P<id>(?:(?!\1).)+)\1',
3089bc74
S
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)
73c19aaa
S
80 )
81
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')
289ef490
AF
88 data = {
89 'action': 'get_info',
90 'curr_url': curr_url,
91 'file_id': file_id,
92 'video_url': file_id,
93 }
289ef490
AF
94
95 response = self._download_json(
73c19aaa 96 self._APPS_BASE_URL + '/media/play/handler.php', video_id,
289ef490 97 data=urlencode_postdata(data),
73c19aaa
S
98 headers=merge_dicts({
99 'Content-Type': 'application/x-www-form-urlencoded'},
100 self.geo_verification_headers()))
289ef490
AF
101
102 result = response['result']
103
73c19aaa
S
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)
111
289ef490 112 formats = []
289ef490 113
73c19aaa
S
114 width = int_or_none(result.get('width'))
115 height = int_or_none(result.get('height'))
116
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)
3d2623a8 125 # resolved_url = urlh.url
73c19aaa
S
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,
135 })
73c19aaa
S
136
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'))
add96eb9 143 if not track_kind or not isinstance(track_kind, str):
73c19aaa
S
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({
289ef490
AF
153 'url': self._proto_relative_url(track_url),
154 'ext': 'srt',
155 })
156
289ef490
AF
157 # Likes
158 emotion = self._download_json(
73c19aaa 159 'https://emocounter.hkedcity.net/handler.php', video_id,
289ef490
AF
160 data=urlencode_postdata({
161 'action': 'get_emotion',
162 'data[bucket_id]': 'etv',
163 'data[identifier]': video_id,
164 }),
165 headers={'Content-Type': 'application/x-www-form-urlencoded'},
73c19aaa
S
166 fatal=False) or {}
167 like_count = int_or_none(try_get(
168 emotion, lambda x: x['data']['emotion_data'][0]['count']))
289ef490
AF
169
170 return {
171 'id': video_id,
172 'title': title,
73c19aaa
S
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),
289ef490
AF
177 'duration': int_or_none(result.get('length')),
178 'formats': formats,
179 'subtitles': subtitles,
73c19aaa
S
180 'thumbnail': urljoin(self._APPS_BASE_URL, result.get('image')),
181 'view_count': parse_count(result.get('view_count')),
289ef490
AF
182 'like_count': like_count,
183 }