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.

269 lines
10 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_HTTPError,
  13. compat_urllib_parse_urlencode,
  14. compat_urllib_parse,
  15. )
  16. from ..utils import (
  17. ExtractorError,
  18. float_or_none,
  19. int_or_none,
  20. )
  21. class VRVBaseIE(InfoExtractor):
  22. _API_DOMAIN = None
  23. _API_PARAMS = {}
  24. _CMS_SIGNING = {}
  25. _TOKEN = None
  26. _TOKEN_SECRET = ''
  27. def _call_api(self, path, video_id, note, data=None):
  28. # https://tools.ietf.org/html/rfc5849#section-3
  29. base_url = self._API_DOMAIN + '/core/' + path
  30. query = [
  31. ('oauth_consumer_key', self._API_PARAMS['oAuthKey']),
  32. ('oauth_nonce', ''.join([random.choice(string.ascii_letters) for _ in range(32)])),
  33. ('oauth_signature_method', 'HMAC-SHA1'),
  34. ('oauth_timestamp', int(time.time())),
  35. ]
  36. if self._TOKEN:
  37. query.append(('oauth_token', self._TOKEN))
  38. encoded_query = compat_urllib_parse_urlencode(query)
  39. headers = self.geo_verification_headers()
  40. if data:
  41. data = json.dumps(data).encode()
  42. headers['Content-Type'] = 'application/json'
  43. base_string = '&'.join([
  44. 'POST' if data else 'GET',
  45. compat_urllib_parse.quote(base_url, ''),
  46. compat_urllib_parse.quote(encoded_query, '')])
  47. oauth_signature = base64.b64encode(hmac.new(
  48. (self._API_PARAMS['oAuthSecret'] + '&' + self._TOKEN_SECRET).encode('ascii'),
  49. base_string.encode(), hashlib.sha1).digest()).decode()
  50. encoded_query += '&oauth_signature=' + compat_urllib_parse.quote(oauth_signature, '')
  51. try:
  52. return self._download_json(
  53. '?'.join([base_url, encoded_query]), video_id,
  54. note='Downloading %s JSON metadata' % note, headers=headers, data=data)
  55. except ExtractorError as e:
  56. if isinstance(e.cause, compat_HTTPError) and e.cause.code == 401:
  57. raise ExtractorError(json.loads(e.cause.read().decode())['message'], expected=True)
  58. raise
  59. def _call_cms(self, path, video_id, note):
  60. if not self._CMS_SIGNING:
  61. self._CMS_SIGNING = self._call_api('index', video_id, 'CMS Signing')['cms_signing']
  62. return self._download_json(
  63. self._API_DOMAIN + path, video_id, query=self._CMS_SIGNING,
  64. note='Downloading %s JSON metadata' % note, headers=self.geo_verification_headers())
  65. def _get_cms_resource(self, resource_key, video_id):
  66. return self._call_api(
  67. 'cms_resource', video_id, 'resource path', data={
  68. 'resource_key': resource_key,
  69. })['__links__']['cms_resource']['href']
  70. def _real_initialize(self):
  71. webpage = self._download_webpage(
  72. 'https://vrv.co/', None, headers=self.geo_verification_headers())
  73. self._API_PARAMS = self._parse_json(self._search_regex(
  74. [
  75. r'window\.__APP_CONFIG__\s*=\s*({.+?})(?:</script>|;)',
  76. r'window\.__APP_CONFIG__\s*=\s*({.+})'
  77. ], webpage, 'app config'), None)['cxApiParams']
  78. self._API_DOMAIN = self._API_PARAMS.get('apiDomain', 'https://api.vrv.co')
  79. class VRVIE(VRVBaseIE):
  80. IE_NAME = 'vrv'
  81. _VALID_URL = r'https?://(?:www\.)?vrv\.co/watch/(?P<id>[A-Z0-9]+)'
  82. _TESTS = [{
  83. 'url': 'https://vrv.co/watch/GR9PNZ396/Hidden-America-with-Jonah-Ray:BOSTON-WHERE-THE-PAST-IS-THE-PRESENT',
  84. 'info_dict': {
  85. 'id': 'GR9PNZ396',
  86. 'ext': 'mp4',
  87. 'title': 'BOSTON: WHERE THE PAST IS THE PRESENT',
  88. 'description': 'md5:4ec8844ac262ca2df9e67c0983c6b83f',
  89. 'uploader_id': 'seeso',
  90. },
  91. 'params': {
  92. # m3u8 download
  93. 'skip_download': True,
  94. },
  95. }, {
  96. # movie listing
  97. 'url': 'https://vrv.co/watch/G6NQXZ1J6/Lily-CAT',
  98. 'info_dict': {
  99. 'id': 'G6NQXZ1J6',
  100. 'title': 'Lily C.A.T',
  101. 'description': 'md5:988b031e7809a6aeb60968be4af7db07',
  102. },
  103. 'playlist_count': 2,
  104. }]
  105. _NETRC_MACHINE = 'vrv'
  106. def _real_initialize(self):
  107. super(VRVIE, self)._real_initialize()
  108. email, password = self._get_login_info()
  109. if email is None:
  110. return
  111. token_credentials = self._call_api(
  112. 'authenticate/by:credentials', None, 'Token Credentials', data={
  113. 'email': email,
  114. 'password': password,
  115. })
  116. self._TOKEN = token_credentials['oauth_token']
  117. self._TOKEN_SECRET = token_credentials['oauth_token_secret']
  118. def _extract_vrv_formats(self, url, video_id, stream_format, audio_lang, hardsub_lang):
  119. if not url or stream_format not in ('hls', 'dash', 'adaptive_hls'):
  120. return []
  121. stream_id_list = []
  122. if audio_lang:
  123. stream_id_list.append('audio-%s' % audio_lang)
  124. if hardsub_lang:
  125. stream_id_list.append('hardsub-%s' % hardsub_lang)
  126. format_id = stream_format
  127. if stream_id_list:
  128. format_id += '-' + '-'.join(stream_id_list)
  129. if 'hls' in stream_format:
  130. adaptive_formats = self._extract_m3u8_formats(
  131. url, video_id, 'mp4', m3u8_id=format_id,
  132. note='Downloading %s information' % format_id,
  133. fatal=False)
  134. elif stream_format == 'dash':
  135. adaptive_formats = self._extract_mpd_formats(
  136. url, video_id, mpd_id=format_id,
  137. note='Downloading %s information' % format_id,
  138. fatal=False)
  139. if audio_lang:
  140. for f in adaptive_formats:
  141. if f.get('acodec') != 'none':
  142. f['language'] = audio_lang
  143. return adaptive_formats
  144. def _real_extract(self, url):
  145. video_id = self._match_id(url)
  146. object_data = self._call_cms(self._get_cms_resource(
  147. 'cms:/objects/' + video_id, video_id), video_id, 'object')['items'][0]
  148. resource_path = object_data['__links__']['resource']['href']
  149. video_data = self._call_cms(resource_path, video_id, 'video')
  150. title = video_data['title']
  151. description = video_data.get('description')
  152. if video_data.get('__class__') == 'movie_listing':
  153. items = self._call_cms(
  154. video_data['__links__']['movie_listing/movies']['href'],
  155. video_id, 'movie listing').get('items') or []
  156. if len(items) != 1:
  157. entries = []
  158. for item in items:
  159. item_id = item.get('id')
  160. if not item_id:
  161. continue
  162. entries.append(self.url_result(
  163. 'https://vrv.co/watch/' + item_id,
  164. self.ie_key(), item_id, item.get('title')))
  165. return self.playlist_result(entries, video_id, title, description)
  166. video_data = items[0]
  167. streams_path = video_data['__links__'].get('streams', {}).get('href')
  168. if not streams_path:
  169. self.raise_login_required()
  170. streams_json = self._call_cms(streams_path, video_id, 'streams')
  171. audio_locale = streams_json.get('audio_locale')
  172. formats = []
  173. for stream_type, streams in streams_json.get('streams', {}).items():
  174. if stream_type in ('adaptive_hls', 'adaptive_dash'):
  175. for stream in streams.values():
  176. formats.extend(self._extract_vrv_formats(
  177. stream.get('url'), video_id, stream_type.split('_')[1],
  178. audio_locale, stream.get('hardsub_locale')))
  179. self._sort_formats(formats)
  180. subtitles = {}
  181. for k in ('captions', 'subtitles'):
  182. for subtitle in streams_json.get(k, {}).values():
  183. subtitle_url = subtitle.get('url')
  184. if not subtitle_url:
  185. continue
  186. subtitles.setdefault(subtitle.get('locale', 'en-US'), []).append({
  187. 'url': subtitle_url,
  188. 'ext': subtitle.get('format', 'ass'),
  189. })
  190. thumbnails = []
  191. for thumbnail in video_data.get('images', {}).get('thumbnails', []):
  192. thumbnail_url = thumbnail.get('source')
  193. if not thumbnail_url:
  194. continue
  195. thumbnails.append({
  196. 'url': thumbnail_url,
  197. 'width': int_or_none(thumbnail.get('width')),
  198. 'height': int_or_none(thumbnail.get('height')),
  199. })
  200. return {
  201. 'id': video_id,
  202. 'title': title,
  203. 'formats': formats,
  204. 'subtitles': subtitles,
  205. 'thumbnails': thumbnails,
  206. 'description': description,
  207. 'duration': float_or_none(video_data.get('duration_ms'), 1000),
  208. 'uploader_id': video_data.get('channel_id'),
  209. 'series': video_data.get('series_title'),
  210. 'season': video_data.get('season_title'),
  211. 'season_number': int_or_none(video_data.get('season_number')),
  212. 'season_id': video_data.get('season_id'),
  213. 'episode': title,
  214. 'episode_number': int_or_none(video_data.get('episode_number')),
  215. 'episode_id': video_data.get('production_episode_id'),
  216. }
  217. class VRVSeriesIE(VRVBaseIE):
  218. IE_NAME = 'vrv:series'
  219. _VALID_URL = r'https?://(?:www\.)?vrv\.co/series/(?P<id>[A-Z0-9]+)'
  220. _TEST = {
  221. 'url': 'https://vrv.co/series/G68VXG3G6/The-Perfect-Insider',
  222. 'info_dict': {
  223. 'id': 'G68VXG3G6',
  224. },
  225. 'playlist_mincount': 11,
  226. }
  227. def _real_extract(self, url):
  228. series_id = self._match_id(url)
  229. seasons_path = self._get_cms_resource(
  230. 'cms:/seasons?series_id=' + series_id, series_id)
  231. seasons_data = self._call_cms(seasons_path, series_id, 'seasons')
  232. entries = []
  233. for season in seasons_data.get('items', []):
  234. episodes_path = season['__links__']['season/episodes']['href']
  235. episodes = self._call_cms(episodes_path, series_id, 'episodes')
  236. for episode in episodes.get('items', []):
  237. episode_id = episode['id']
  238. entries.append(self.url_result(
  239. 'https://vrv.co/watch/' + episode_id,
  240. 'VRV', episode_id, episode.get('title')))
  241. return self.playlist_result(entries, series_id)