hketv.py 6.6 KB

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