You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

191 lines
6.8 KiB

  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. from .common import InfoExtractor
  4. from ..compat import compat_str
  5. from ..utils import (
  6. clean_html,
  7. ExtractorError,
  8. int_or_none,
  9. merge_dicts,
  10. parse_count,
  11. str_or_none,
  12. try_get,
  13. unified_strdate,
  14. urlencode_postdata,
  15. urljoin,
  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': '喜閱一生(共享閱讀樂) (中、英文字幕可供選擇)',
  30. 'description': 'md5:d5286d05219ef50e0613311cbe96e560',
  31. 'upload_date': '20181024',
  32. 'duration': 900,
  33. 'subtitles': 'count:2',
  34. },
  35. 'skip': 'Geo restricted to HK',
  36. }, {
  37. 'url': 'https://www.hkedcity.net/etv/resource/972641418',
  38. 'md5': '1ed494c1c6cf7866a8290edad9b07dc9',
  39. 'info_dict': {
  40. 'id': '972641418',
  41. 'ext': 'mp4',
  42. 'title': '衣冠楚楚 (天使系列之一)',
  43. 'description': 'md5:10bb3d659421e74f58e5db5691627b0f',
  44. 'upload_date': '20070109',
  45. 'duration': 907,
  46. 'subtitles': {},
  47. },
  48. 'params': {
  49. 'geo_verification_proxy': '<HK proxy here>',
  50. },
  51. 'skip': 'Geo restricted to HK',
  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. def _real_extract(self, url):
  70. video_id = self._match_id(url)
  71. webpage = self._download_webpage(url, video_id)
  72. title = (
  73. self._html_search_meta(
  74. ('ed_title', 'search.ed_title'), webpage, default=None) or
  75. self._search_regex(
  76. r'data-favorite_title_(?:eng|chi)=(["\'])(?P<id>(?:(?!\1).)+)\1',
  77. webpage, 'title', default=None, group='url') or
  78. self._html_search_regex(
  79. r'<h1>([^<]+)</h1>', webpage, 'title', default=None) or
  80. self._og_search_title(webpage)
  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')
  88. data = {
  89. 'action': 'get_info',
  90. 'curr_url': curr_url,
  91. 'file_id': file_id,
  92. 'video_url': file_id,
  93. }
  94. response = self._download_json(
  95. self._APPS_BASE_URL + '/media/play/handler.php', video_id,
  96. data=urlencode_postdata(data),
  97. headers=merge_dicts({
  98. 'Content-Type': 'application/x-www-form-urlencoded'},
  99. self.geo_verification_headers()))
  100. result = response['result']
  101. if not response.get('success') or not response.get('access'):
  102. error = clean_html(response.get('access_err_msg'))
  103. if 'Video streaming is not available in your country' in error:
  104. self.raise_geo_restricted(
  105. msg=error, countries=self._GEO_COUNTRIES)
  106. else:
  107. raise ExtractorError(error, expected=True)
  108. formats = []
  109. width = int_or_none(result.get('width'))
  110. height = int_or_none(result.get('height'))
  111. playlist0 = result['playlist'][0]
  112. for fmt in playlist0['sources']:
  113. file_url = urljoin(self._APPS_BASE_URL, fmt.get('file'))
  114. if not file_url:
  115. continue
  116. # If we ever wanted to provide the final resolved URL that
  117. # does not require cookies, albeit with a shorter lifespan:
  118. # urlh = self._downloader.urlopen(file_url)
  119. # resolved_url = urlh.geturl()
  120. label = fmt.get('label')
  121. h = self._FORMAT_HEIGHTS.get(label)
  122. w = h * width // height if h and width and height else None
  123. formats.append({
  124. 'format_id': label,
  125. 'ext': fmt.get('type'),
  126. 'url': file_url,
  127. 'width': w,
  128. 'height': h,
  129. })
  130. self._sort_formats(formats)
  131. subtitles = {}
  132. tracks = try_get(playlist0, lambda x: x['tracks'], list) or []
  133. for track in tracks:
  134. if not isinstance(track, dict):
  135. continue
  136. track_kind = str_or_none(track.get('kind'))
  137. if not track_kind or not isinstance(track_kind, compat_str):
  138. continue
  139. if track_kind.lower() not in ('captions', 'subtitles'):
  140. continue
  141. track_url = urljoin(self._APPS_BASE_URL, track.get('file'))
  142. if not track_url:
  143. continue
  144. track_label = track.get('label')
  145. subtitles.setdefault(self._CC_LANGS.get(
  146. track_label, track_label), []).append({
  147. 'url': self._proto_relative_url(track_url),
  148. 'ext': 'srt',
  149. })
  150. # Likes
  151. emotion = self._download_json(
  152. 'https://emocounter.hkedcity.net/handler.php', video_id,
  153. data=urlencode_postdata({
  154. 'action': 'get_emotion',
  155. 'data[bucket_id]': 'etv',
  156. 'data[identifier]': video_id,
  157. }),
  158. headers={'Content-Type': 'application/x-www-form-urlencoded'},
  159. fatal=False) or {}
  160. like_count = int_or_none(try_get(
  161. emotion, lambda x: x['data']['emotion_data'][0]['count']))
  162. return {
  163. 'id': video_id,
  164. 'title': title,
  165. 'description': self._html_search_meta(
  166. 'description', webpage, fatal=False),
  167. 'upload_date': unified_strdate(self._html_search_meta(
  168. 'ed_date', webpage, fatal=False), day_first=False),
  169. 'duration': int_or_none(result.get('length')),
  170. 'formats': formats,
  171. 'subtitles': subtitles,
  172. 'thumbnail': urljoin(self._APPS_BASE_URL, result.get('image')),
  173. 'view_count': parse_count(result.get('view_count')),
  174. 'like_count': like_count,
  175. }