hrti.py 6.8 KB

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