hketv.py 6.7 KB

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