logo

youtube-dl

[mirror] Download/Watch videos from video hostersgit clone https://hacktivis.me/git/mirror/youtube-dl.git

hrti.py (7202B)


  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. 'url': 'https://hrti.hrt.hr/video/show/3873068/cuvar-dvorca-dramska-serija-14',
  111. 'only_matching': True,
  112. }]
  113. def _real_extract(self, url):
  114. mobj = re.match(self._VALID_URL, url)
  115. video_id = mobj.group('short_id') or mobj.group('id')
  116. display_id = mobj.group('display_id') or video_id
  117. video = self._download_json(
  118. '%s/video_id/%s/format/json' % (self._search_url, video_id),
  119. display_id, 'Downloading video metadata JSON')['video'][0]
  120. title_info = video['title']
  121. title = title_info['title_long']
  122. movie = video['video_assets']['movie'][0]
  123. m3u8_url = movie['url'].format(TOKEN=self._token)
  124. formats = self._extract_m3u8_formats(
  125. m3u8_url, display_id, 'mp4', entry_protocol='m3u8_native',
  126. m3u8_id='hls')
  127. self._sort_formats(formats)
  128. description = clean_html(title_info.get('summary_long'))
  129. age_limit = parse_age_limit(video.get('parental_control', {}).get('rating'))
  130. view_count = int_or_none(video.get('views'))
  131. average_rating = int_or_none(video.get('user_rating'))
  132. duration = int_or_none(movie.get('duration'))
  133. return {
  134. 'id': video_id,
  135. 'display_id': display_id,
  136. 'title': title,
  137. 'description': description,
  138. 'duration': duration,
  139. 'view_count': view_count,
  140. 'average_rating': average_rating,
  141. 'age_limit': age_limit,
  142. 'formats': formats,
  143. }
  144. class HRTiPlaylistIE(HRTiBaseIE):
  145. _VALID_URL = r'https?://hrti\.hrt\.hr/(?:#/)?video/list/category/(?P<id>[0-9]+)/(?P<display_id>[^/]+)?'
  146. _TESTS = [{
  147. 'url': 'https://hrti.hrt.hr/#/video/list/category/212/ekumena',
  148. 'info_dict': {
  149. 'id': '212',
  150. 'title': 'ekumena',
  151. },
  152. 'playlist_mincount': 8,
  153. 'skip': 'Requires account credentials',
  154. }, {
  155. 'url': 'https://hrti.hrt.hr/#/video/list/category/212/',
  156. 'only_matching': True,
  157. }, {
  158. 'url': 'https://hrti.hrt.hr/video/list/category/212/ekumena',
  159. 'only_matching': True,
  160. }]
  161. def _real_extract(self, url):
  162. mobj = re.match(self._VALID_URL, url)
  163. category_id = mobj.group('id')
  164. display_id = mobj.group('display_id') or category_id
  165. response = self._download_json(
  166. '%s/category_id/%s/format/json' % (self._search_url, category_id),
  167. display_id, 'Downloading video metadata JSON')
  168. video_ids = try_get(
  169. response, lambda x: x['video_listings'][0]['alternatives'][0]['list'],
  170. list) or [video['id'] for video in response.get('videos', []) if video.get('id')]
  171. entries = [self.url_result('hrti:%s' % video_id) for video_id in video_ids]
  172. return self.playlist_result(entries, category_id, display_id)