]>
Commit | Line | Data |
---|---|---|
289ef490 AF |
1 | from .common import InfoExtractor |
2 | from ..compat import compat_str | |
3 | from ..utils import ( | |
4 | clean_html, | |
5 | ExtractorError, | |
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 | ||
17 | class 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) | |
129 | # resolved_url = urlh.geturl() | |
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 | }) | |
140 | self._sort_formats(formats) | |
141 | ||
142 | subtitles = {} | |
143 | tracks = try_get(playlist0, lambda x: x['tracks'], list) or [] | |
144 | for track in tracks: | |
145 | if not isinstance(track, dict): | |
146 | continue | |
147 | track_kind = str_or_none(track.get('kind')) | |
148 | if not track_kind or not isinstance(track_kind, compat_str): | |
149 | continue | |
150 | if track_kind.lower() not in ('captions', 'subtitles'): | |
151 | continue | |
152 | track_url = urljoin(self._APPS_BASE_URL, track.get('file')) | |
153 | if not track_url: | |
154 | continue | |
155 | track_label = track.get('label') | |
156 | subtitles.setdefault(self._CC_LANGS.get( | |
157 | track_label, track_label), []).append({ | |
289ef490 AF |
158 | 'url': self._proto_relative_url(track_url), |
159 | 'ext': 'srt', | |
160 | }) | |
161 | ||
289ef490 AF |
162 | # Likes |
163 | emotion = self._download_json( | |
73c19aaa | 164 | 'https://emocounter.hkedcity.net/handler.php', video_id, |
289ef490 AF |
165 | data=urlencode_postdata({ |
166 | 'action': 'get_emotion', | |
167 | 'data[bucket_id]': 'etv', | |
168 | 'data[identifier]': video_id, | |
169 | }), | |
170 | headers={'Content-Type': 'application/x-www-form-urlencoded'}, | |
73c19aaa S |
171 | fatal=False) or {} |
172 | like_count = int_or_none(try_get( | |
173 | emotion, lambda x: x['data']['emotion_data'][0]['count'])) | |
289ef490 AF |
174 | |
175 | return { | |
176 | 'id': video_id, | |
177 | 'title': title, | |
73c19aaa S |
178 | 'description': self._html_search_meta( |
179 | 'description', webpage, fatal=False), | |
180 | 'upload_date': unified_strdate(self._html_search_meta( | |
181 | 'ed_date', webpage, fatal=False), day_first=False), | |
289ef490 AF |
182 | 'duration': int_or_none(result.get('length')), |
183 | 'formats': formats, | |
184 | 'subtitles': subtitles, | |
73c19aaa S |
185 | 'thumbnail': urljoin(self._APPS_BASE_URL, result.get('image')), |
186 | 'view_count': parse_count(result.get('view_count')), | |
289ef490 AF |
187 | 'like_count': like_count, |
188 | } |