hrti.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import json
  4. from .common import InfoExtractor
  5. from ..compat import (
  6. compat_HTTPError
  7. )
  8. from ..utils import (
  9. sanitized_Request,
  10. ExtractorError
  11. )
  12. class HRTiIE(InfoExtractor):
  13. '''
  14. Information Extractor for Croatian Radiotelevision video on demand site
  15. 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. _VALID_URL = r'https?://hrti.hrt.hr/#/video/show/(?P<id>[0-9]+)/(?P<name>(\w|-)+)?'
  23. _TEST = {
  24. 'url': 'https://hrti.hrt.hr/#/video/show/2181385/republika-dokumentarna-serija-16-hd',
  25. 'info_dict': {
  26. 'id': '2181385',
  27. 'ext': 'mp4',
  28. 'name': 'REPUBLIKA, dokumentarna serija (4_6)-2251938',
  29. },
  30. 'skip': 'Requires login'
  31. }
  32. def _initialize_api(self):
  33. '''Initializes the API and obtains the required urls'''
  34. api_url = 'http://clientapi.hrt.hr/client_api.php/config/identify/format/json'
  35. app_data = json.dumps({
  36. 'application_publication_id': HRTiIE.APP_PUBLICATION_ID
  37. })
  38. self.uuid = self._download_json(api_url, None, note='Getting UUID',
  39. errnote='Unable to obtain an UUID',
  40. data=app_data)['uuid']
  41. app_data = json.dumps({
  42. 'uuid': self.uuid,
  43. 'application_publication_id': HRTiIE.APP_PUBLICATION_ID,
  44. 'screen_height': 1080,
  45. 'screen_width': 1920,
  46. 'os': 'Windows',
  47. 'os_version': 'NT 4.0',
  48. 'device_model_string_id': 'chrome 42.0.2311.135',
  49. 'application_version': HRTiIE.APP_VERSION
  50. })
  51. req = sanitized_Request(api_url, data=app_data)
  52. req.get_method = lambda: 'PUT'
  53. resources = self._download_json(
  54. req, None, note='Getting API endpoint and session information',
  55. errnote='Unable to get endpoint and session information',
  56. headers={'Content-type': 'application/json'})
  57. self.session_id = resources['session_id']
  58. modules = resources['modules']
  59. self.search_url = modules['vod_catalog']['resources']['search']['uri']
  60. self.search_url = self.search_url.format(
  61. language=HRTiIE.APP_LANGUAGE,
  62. application_id=HRTiIE.APP_PUBLICATION_ID)
  63. self.login_url = modules['user']['resources']['login']['uri']
  64. self.login_url = self.login_url.format(session_id=self.session_id)
  65. self.login_url += '/format/json'
  66. self.logout_url = modules['user']['resources']['logout']['uri']
  67. def _login(self):
  68. '''Performs a login to the webservice'''
  69. (username, password) = self._get_login_info()
  70. if username is None or password is None:
  71. self.raise_login_required()
  72. auth_data = json.dumps({
  73. 'username': username,
  74. 'password': password,
  75. })
  76. try:
  77. auth_info = self._download_json(
  78. self.login_url, None, note='Authenticating',
  79. errnote='Unable to log in', data=auth_data)
  80. except ExtractorError as ee:
  81. if isinstance(ee.cause, compat_HTTPError) and ee.cause.code == 406:
  82. raise ExtractorError('Unable to login, ' +
  83. 'incorrect username and/or password')
  84. raise
  85. self.token = auth_info['secure_streaming_token']
  86. self.access_token = auth_info['session_token']
  87. self.logout_url = self.logout_url.format(session_id=self.session_id,
  88. access_token=self.access_token)
  89. self.logout_url += '/format/json'
  90. def _real_initialize(self):
  91. '''Performs necessary operations so that the information extractor is
  92. ready for operation'''
  93. self._initialize_api()
  94. self._login()
  95. def _logout(self):
  96. '''Performs logout from the webservice'''
  97. self._download_json(self.logout_url, None, note='Logout',
  98. errnote='Unable to log out', fatal=False)
  99. def _real_extract(self, url):
  100. '''Extract the data necessary to download the video'''
  101. video_id = self._match_id(url)
  102. metadata_url = self.search_url + \
  103. '/video_id/{video_id}/format/json'.format(video_id=video_id)
  104. metadata = self._download_json(metadata_url, video_id,
  105. note='Getting video metadata')
  106. video = metadata['video'][0]
  107. title_info = video.get('title', {})
  108. title = title_info.get('title_long')
  109. description = title_info.get('summary_long')
  110. movie = video['video_assets']['movie'][0]
  111. url = movie['url'].format(TOKEN=self.token)
  112. formats = self._extract_m3u8_formats(url, video_id, 'mp4')
  113. self._sort_formats(formats)
  114. self._logout()
  115. return {
  116. 'id': video_id,
  117. 'title': title,
  118. 'description': description,
  119. 'formats': formats,
  120. }