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.

212 lines
8.5 KiB

  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import base64
  4. import json
  5. import hashlib
  6. import hmac
  7. import random
  8. import string
  9. import time
  10. from .common import InfoExtractor
  11. from ..compat import (
  12. compat_urllib_parse_urlencode,
  13. compat_urlparse,
  14. )
  15. from ..utils import (
  16. float_or_none,
  17. int_or_none,
  18. )
  19. class VRVBaseIE(InfoExtractor):
  20. _API_DOMAIN = None
  21. _API_PARAMS = {}
  22. _CMS_SIGNING = {}
  23. def _call_api(self, path, video_id, note, data=None):
  24. base_url = self._API_DOMAIN + '/core/' + path
  25. encoded_query = compat_urllib_parse_urlencode({
  26. 'oauth_consumer_key': self._API_PARAMS['oAuthKey'],
  27. 'oauth_nonce': ''.join([random.choice(string.ascii_letters) for _ in range(32)]),
  28. 'oauth_signature_method': 'HMAC-SHA1',
  29. 'oauth_timestamp': int(time.time()),
  30. 'oauth_version': '1.0',
  31. })
  32. headers = self.geo_verification_headers()
  33. if data:
  34. data = json.dumps(data).encode()
  35. headers['Content-Type'] = 'application/json'
  36. method = 'POST' if data else 'GET'
  37. base_string = '&'.join([method, compat_urlparse.quote(base_url, ''), compat_urlparse.quote(encoded_query, '')])
  38. oauth_signature = base64.b64encode(hmac.new(
  39. (self._API_PARAMS['oAuthSecret'] + '&').encode('ascii'),
  40. base_string.encode(), hashlib.sha1).digest()).decode()
  41. encoded_query += '&oauth_signature=' + compat_urlparse.quote(oauth_signature, '')
  42. return self._download_json(
  43. '?'.join([base_url, encoded_query]), video_id,
  44. note='Downloading %s JSON metadata' % note, headers=headers, data=data)
  45. def _call_cms(self, path, video_id, note):
  46. if not self._CMS_SIGNING:
  47. self._CMS_SIGNING = self._call_api('index', video_id, 'CMS Signing')['cms_signing']
  48. return self._download_json(
  49. self._API_DOMAIN + path, video_id, query=self._CMS_SIGNING,
  50. note='Downloading %s JSON metadata' % note, headers=self.geo_verification_headers())
  51. def _set_api_params(self, webpage, video_id):
  52. if not self._API_PARAMS:
  53. self._API_PARAMS = self._parse_json(self._search_regex(
  54. r'window\.__APP_CONFIG__\s*=\s*({.+?})</script>',
  55. webpage, 'api config'), video_id)['cxApiParams']
  56. self._API_DOMAIN = self._API_PARAMS.get('apiDomain', 'https://api.vrv.co')
  57. def _get_cms_resource(self, resource_key, video_id):
  58. return self._call_api(
  59. 'cms_resource', video_id, 'resource path', data={
  60. 'resource_key': resource_key,
  61. })['__links__']['cms_resource']['href']
  62. class VRVIE(VRVBaseIE):
  63. IE_NAME = 'vrv'
  64. _VALID_URL = r'https?://(?:www\.)?vrv\.co/watch/(?P<id>[A-Z0-9]+)'
  65. _TEST = {
  66. 'url': 'https://vrv.co/watch/GR9PNZ396/Hidden-America-with-Jonah-Ray:BOSTON-WHERE-THE-PAST-IS-THE-PRESENT',
  67. 'info_dict': {
  68. 'id': 'GR9PNZ396',
  69. 'ext': 'mp4',
  70. 'title': 'BOSTON: WHERE THE PAST IS THE PRESENT',
  71. 'description': 'md5:4ec8844ac262ca2df9e67c0983c6b83f',
  72. 'uploader_id': 'seeso',
  73. },
  74. 'params': {
  75. # m3u8 download
  76. 'skip_download': True,
  77. },
  78. }
  79. def _real_extract(self, url):
  80. video_id = self._match_id(url)
  81. webpage = self._download_webpage(
  82. url, video_id,
  83. headers=self.geo_verification_headers())
  84. media_resource = self._parse_json(self._search_regex(
  85. r'window\.__INITIAL_STATE__\s*=\s*({.+?})</script>',
  86. webpage, 'inital state'), video_id).get('watch', {}).get('mediaResource') or {}
  87. video_data = media_resource.get('json')
  88. if not video_data:
  89. self._set_api_params(webpage, video_id)
  90. episode_path = self._get_cms_resource(
  91. 'cms:/episodes/' + video_id, video_id)
  92. video_data = self._call_cms(episode_path, video_id, 'video')
  93. title = video_data['title']
  94. streams_json = media_resource.get('streams', {}).get('json', {})
  95. if not streams_json:
  96. self._set_api_params(webpage, video_id)
  97. streams_path = video_data['__links__']['streams']['href']
  98. streams_json = self._call_cms(streams_path, video_id, 'streams')
  99. audio_locale = streams_json.get('audio_locale')
  100. formats = []
  101. for stream_type, streams in streams_json.get('streams', {}).items():
  102. if stream_type in ('adaptive_hls', 'adaptive_dash'):
  103. for stream in streams.values():
  104. stream_url = stream.get('url')
  105. if not stream_url:
  106. continue
  107. stream_id = stream.get('hardsub_locale') or audio_locale
  108. format_id = '%s-%s' % (stream_type.split('_')[1], stream_id)
  109. if stream_type == 'adaptive_hls':
  110. adaptive_formats = self._extract_m3u8_formats(
  111. stream_url, video_id, 'mp4', m3u8_id=format_id,
  112. note='Downloading %s m3u8 information' % stream_id,
  113. fatal=False)
  114. else:
  115. adaptive_formats = self._extract_mpd_formats(
  116. stream_url, video_id, mpd_id=format_id,
  117. note='Downloading %s MPD information' % stream_id,
  118. fatal=False)
  119. if audio_locale:
  120. for f in adaptive_formats:
  121. if f.get('acodec') != 'none':
  122. f['language'] = audio_locale
  123. formats.extend(adaptive_formats)
  124. self._sort_formats(formats)
  125. subtitles = {}
  126. for subtitle in streams_json.get('subtitles', {}).values():
  127. subtitle_url = subtitle.get('url')
  128. if not subtitle_url:
  129. continue
  130. subtitles.setdefault(subtitle.get('locale', 'en-US'), []).append({
  131. 'url': subtitle_url,
  132. 'ext': subtitle.get('format', 'ass'),
  133. })
  134. thumbnails = []
  135. for thumbnail in video_data.get('images', {}).get('thumbnails', []):
  136. thumbnail_url = thumbnail.get('source')
  137. if not thumbnail_url:
  138. continue
  139. thumbnails.append({
  140. 'url': thumbnail_url,
  141. 'width': int_or_none(thumbnail.get('width')),
  142. 'height': int_or_none(thumbnail.get('height')),
  143. })
  144. return {
  145. 'id': video_id,
  146. 'title': title,
  147. 'formats': formats,
  148. 'subtitles': subtitles,
  149. 'thumbnails': thumbnails,
  150. 'description': video_data.get('description'),
  151. 'duration': float_or_none(video_data.get('duration_ms'), 1000),
  152. 'uploader_id': video_data.get('channel_id'),
  153. 'series': video_data.get('series_title'),
  154. 'season': video_data.get('season_title'),
  155. 'season_number': int_or_none(video_data.get('season_number')),
  156. 'season_id': video_data.get('season_id'),
  157. 'episode': title,
  158. 'episode_number': int_or_none(video_data.get('episode_number')),
  159. 'episode_id': video_data.get('production_episode_id'),
  160. }
  161. class VRVSeriesIE(VRVBaseIE):
  162. IE_NAME = 'vrv:series'
  163. _VALID_URL = r'https?://(?:www\.)?vrv\.co/series/(?P<id>[A-Z0-9]+)'
  164. _TEST = {
  165. 'url': 'https://vrv.co/series/G68VXG3G6/The-Perfect-Insider',
  166. 'info_dict': {
  167. 'id': 'G68VXG3G6',
  168. },
  169. 'playlist_mincount': 11,
  170. }
  171. def _real_extract(self, url):
  172. series_id = self._match_id(url)
  173. webpage = self._download_webpage(
  174. url, series_id,
  175. headers=self.geo_verification_headers())
  176. self._set_api_params(webpage, series_id)
  177. seasons_path = self._get_cms_resource(
  178. 'cms:/seasons?series_id=' + series_id, series_id)
  179. seasons_data = self._call_cms(seasons_path, series_id, 'seasons')
  180. entries = []
  181. for season in seasons_data.get('items', []):
  182. episodes_path = season['__links__']['season/episodes']['href']
  183. episodes = self._call_cms(episodes_path, series_id, 'episodes')
  184. for episode in episodes.get('items', []):
  185. episode_id = episode['id']
  186. entries.append(self.url_result(
  187. 'https://vrv.co/watch/' + episode_id,
  188. 'VRV', episode_id, episode.get('title')))
  189. return self.playlist_result(entries, series_id)