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