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.

202 lines
6.8 KiB

  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import json
  4. import re
  5. from .common import InfoExtractor
  6. from ..compat import compat_HTTPError
  7. from ..utils import (
  8. clean_html,
  9. ExtractorError,
  10. int_or_none,
  11. parse_age_limit,
  12. sanitized_Request,
  13. try_get,
  14. )
  15. class HRTiBaseIE(InfoExtractor):
  16. """
  17. Base Information Extractor for Croatian Radiotelevision
  18. video on demand site https://hrti.hrt.hr
  19. Reverse engineered from the JavaScript app in app.min.js
  20. """
  21. _NETRC_MACHINE = 'hrti'
  22. _APP_LANGUAGE = 'hr'
  23. _APP_VERSION = '1.1'
  24. _APP_PUBLICATION_ID = 'all_in_one'
  25. _API_URL = 'http://clientapi.hrt.hr/client_api.php/config/identify/format/json'
  26. def _initialize_api(self):
  27. init_data = {
  28. 'application_publication_id': self._APP_PUBLICATION_ID
  29. }
  30. uuid = self._download_json(
  31. self._API_URL, None, note='Downloading uuid',
  32. errnote='Unable to download uuid',
  33. data=json.dumps(init_data).encode('utf-8'))['uuid']
  34. app_data = {
  35. 'uuid': uuid,
  36. 'application_publication_id': self._APP_PUBLICATION_ID,
  37. 'application_version': self._APP_VERSION
  38. }
  39. req = sanitized_Request(self._API_URL, data=json.dumps(app_data).encode('utf-8'))
  40. req.get_method = lambda: 'PUT'
  41. resources = self._download_json(
  42. req, None, note='Downloading session information',
  43. errnote='Unable to download session information')
  44. self._session_id = resources['session_id']
  45. modules = resources['modules']
  46. self._search_url = modules['vod_catalog']['resources']['search']['uri'].format(
  47. language=self._APP_LANGUAGE,
  48. application_id=self._APP_PUBLICATION_ID)
  49. self._login_url = (modules['user']['resources']['login']['uri'] +
  50. '/format/json').format(session_id=self._session_id)
  51. self._logout_url = modules['user']['resources']['logout']['uri']
  52. def _login(self):
  53. (username, password) = self._get_login_info()
  54. # TODO: figure out authentication with cookies
  55. if username is None or password is None:
  56. self.raise_login_required()
  57. auth_data = {
  58. 'username': username,
  59. 'password': password,
  60. }
  61. try:
  62. auth_info = self._download_json(
  63. self._login_url, None, note='Logging in', errnote='Unable to log in',
  64. data=json.dumps(auth_data).encode('utf-8'))
  65. except ExtractorError as e:
  66. if isinstance(e.cause, compat_HTTPError) and e.cause.code == 406:
  67. auth_info = self._parse_json(e.cause.read().encode('utf-8'), None)
  68. else:
  69. raise
  70. error_message = auth_info.get('error', {}).get('message')
  71. if error_message:
  72. raise ExtractorError(
  73. '%s said: %s' % (self.IE_NAME, error_message),
  74. expected=True)
  75. self._token = auth_info['secure_streaming_token']
  76. def _real_initialize(self):
  77. self._initialize_api()
  78. self._login()
  79. class HRTiIE(HRTiBaseIE):
  80. _VALID_URL = r'''(?x)
  81. (?:
  82. hrti:(?P<short_id>[0-9]+)|
  83. https?://
  84. hrti\.hrt\.hr/\#/video/show/(?P<id>[0-9]+)/(?P<display_id>[^/]+)?
  85. )
  86. '''
  87. _TESTS = [{
  88. 'url': 'https://hrti.hrt.hr/#/video/show/2181385/republika-dokumentarna-serija-16-hd',
  89. 'info_dict': {
  90. 'id': '2181385',
  91. 'display_id': 'republika-dokumentarna-serija-16-hd',
  92. 'ext': 'mp4',
  93. 'title': 'REPUBLIKA, dokumentarna serija (1/6) (HD)',
  94. 'description': 'md5:48af85f620e8e0e1df4096270568544f',
  95. 'duration': 2922,
  96. 'view_count': int,
  97. 'average_rating': int,
  98. 'episode_number': int,
  99. 'season_number': int,
  100. 'age_limit': 12,
  101. },
  102. 'skip': 'Requires account credentials',
  103. }, {
  104. 'url': 'https://hrti.hrt.hr/#/video/show/2181385/',
  105. 'only_matching': True,
  106. }, {
  107. 'url': 'hrti:2181385',
  108. 'only_matching': True,
  109. }]
  110. def _real_extract(self, url):
  111. mobj = re.match(self._VALID_URL, url)
  112. video_id = mobj.group('short_id') or mobj.group('id')
  113. display_id = mobj.group('display_id') or video_id
  114. video = self._download_json(
  115. '%s/video_id/%s/format/json' % (self._search_url, video_id),
  116. display_id, 'Downloading video metadata JSON')['video'][0]
  117. title_info = video['title']
  118. title = title_info['title_long']
  119. movie = video['video_assets']['movie'][0]
  120. m3u8_url = movie['url'].format(TOKEN=self._token)
  121. formats = self._extract_m3u8_formats(
  122. m3u8_url, display_id, 'mp4', entry_protocol='m3u8_native',
  123. m3u8_id='hls')
  124. self._sort_formats(formats)
  125. description = clean_html(title_info.get('summary_long'))
  126. age_limit = parse_age_limit(video.get('parental_control', {}).get('rating'))
  127. view_count = int_or_none(video.get('views'))
  128. average_rating = int_or_none(video.get('user_rating'))
  129. duration = int_or_none(movie.get('duration'))
  130. return {
  131. 'id': video_id,
  132. 'display_id': display_id,
  133. 'title': title,
  134. 'description': description,
  135. 'duration': duration,
  136. 'view_count': view_count,
  137. 'average_rating': average_rating,
  138. 'age_limit': age_limit,
  139. 'formats': formats,
  140. }
  141. class HRTiPlaylistIE(HRTiBaseIE):
  142. _VALID_URL = r'https?://hrti.hrt.hr/#/video/list/category/(?P<id>[0-9]+)/(?P<display_id>[^/]+)?'
  143. _TESTS = [{
  144. 'url': 'https://hrti.hrt.hr/#/video/list/category/212/ekumena',
  145. 'info_dict': {
  146. 'id': '212',
  147. 'title': 'ekumena',
  148. },
  149. 'playlist_mincount': 8,
  150. 'skip': 'Requires account credentials',
  151. }, {
  152. 'url': 'https://hrti.hrt.hr/#/video/list/category/212/',
  153. 'only_matching': True,
  154. }]
  155. def _real_extract(self, url):
  156. mobj = re.match(self._VALID_URL, url)
  157. category_id = mobj.group('id')
  158. display_id = mobj.group('display_id') or category_id
  159. response = self._download_json(
  160. '%s/category_id/%s/format/json' % (self._search_url, category_id),
  161. display_id, 'Downloading video metadata JSON')
  162. video_ids = try_get(
  163. response, lambda x: x['video_listings'][0]['alternatives'][0]['list'],
  164. list) or [video['id'] for video in response.get('videos', []) if video.get('id')]
  165. entries = [self.url_result('hrti:%s' % video_id) for video_id in video_ids]
  166. return self.playlist_result(entries, category_id, display_id)