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.

133 lines
4.7 KiB

  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import json
  4. from .common import InfoExtractor
  5. from ..compat import compat_urllib_request
  6. from ..utils import (
  7. ExtractorError,
  8. parse_iso8601,
  9. )
  10. class VesselIE(InfoExtractor):
  11. _VALID_URL = r'https?://(?:www\.)?vessel\.com/videos/(?P<id>[0-9a-zA-Z]+)'
  12. _API_URL_TEMPLATE = 'https://www.vessel.com/api/view/items/%s'
  13. _LOGIN_URL = 'https://www.vessel.com/api/account/login'
  14. _NETRC_MACHINE = 'vessel'
  15. _TEST = {
  16. 'url': 'https://www.vessel.com/videos/HDN7G5UMs',
  17. 'md5': '455cdf8beb71c6dd797fd2f3818d05c4',
  18. 'info_dict': {
  19. 'id': 'HDN7G5UMs',
  20. 'ext': 'mp4',
  21. 'title': 'Nvidia GeForce GTX Titan X - The Best Video Card on the Market?',
  22. 'thumbnail': 're:^https?://.*\.jpg$',
  23. 'upload_date': '20150317',
  24. 'description': 'Did Nvidia pull out all the stops on the Titan X, or does its performance leave something to be desired?',
  25. 'timestamp': int,
  26. },
  27. }
  28. @staticmethod
  29. def make_json_request(url, data):
  30. payload = json.dumps(data).encode('utf-8')
  31. req = compat_urllib_request.Request(url, payload)
  32. req.add_header('Content-Type', 'application/json; charset=utf-8')
  33. return req
  34. @staticmethod
  35. def find_assets(data, asset_type, asset_id=None):
  36. for asset in data.get('assets', []):
  37. if not asset.get('type') == asset_type:
  38. continue
  39. elif asset_id is not None and not asset.get('id') == asset_id:
  40. continue
  41. else:
  42. yield asset
  43. def _check_access_rights(self, data):
  44. access_info = data.get('__view', {})
  45. if not access_info.get('allow_access', True):
  46. err_code = access_info.get('error_code') or ''
  47. if err_code == 'ITEM_PAID_ONLY':
  48. raise ExtractorError(
  49. 'This video requires subscription.', expected=True)
  50. else:
  51. raise ExtractorError(
  52. 'Access to this content is restricted. (%s said: %s)' % (self.IE_NAME, err_code), expected=True)
  53. def _login(self):
  54. (username, password) = self._get_login_info()
  55. if username is None:
  56. return
  57. self.report_login()
  58. data = {
  59. 'client_id': 'web',
  60. 'type': 'password',
  61. 'user_key': username,
  62. 'password': password,
  63. }
  64. login_request = VesselIE.make_json_request(self._LOGIN_URL, data)
  65. self._download_webpage(login_request, None, False, 'Wrong login info')
  66. def _real_initialize(self):
  67. self._login()
  68. def _real_extract(self, url):
  69. video_id = self._match_id(url)
  70. webpage = self._download_webpage(url, video_id)
  71. data = self._parse_json(self._search_regex(
  72. r'App\.bootstrapData\((.*?)\);', webpage, 'data'), video_id)
  73. asset_id = data['model']['data']['id']
  74. req = VesselIE.make_json_request(
  75. self._API_URL_TEMPLATE % asset_id, {'client': 'web'})
  76. data = self._download_json(req, video_id)
  77. video_asset_id = data.get('main_video_asset')
  78. self._check_access_rights(data)
  79. try:
  80. video_asset = next(
  81. VesselIE.find_assets(data, 'video', asset_id=video_asset_id))
  82. except StopIteration:
  83. raise ExtractorError('No video assets found')
  84. formats = []
  85. for f in video_asset.get('sources', []):
  86. if f['name'] == 'hls-index':
  87. formats.extend(self._extract_m3u8_formats(
  88. f['location'], video_id, ext='mp4', m3u8_id='m3u8'))
  89. else:
  90. formats.append({
  91. 'format_id': f['name'],
  92. 'tbr': f.get('bitrate'),
  93. 'height': f.get('height'),
  94. 'width': f.get('width'),
  95. 'url': f['location'],
  96. })
  97. self._sort_formats(formats)
  98. thumbnails = []
  99. for im_asset in VesselIE.find_assets(data, 'image'):
  100. thumbnails.append({
  101. 'url': im_asset['location'],
  102. 'width': im_asset.get('width', 0),
  103. 'height': im_asset.get('height', 0),
  104. })
  105. return {
  106. 'id': video_id,
  107. 'title': data['title'],
  108. 'formats': formats,
  109. 'thumbnails': thumbnails,
  110. 'description': data.get('short_description'),
  111. 'duration': data.get('duration'),
  112. 'comment_count': data.get('comment_count'),
  113. 'like_count': data.get('like_count'),
  114. 'view_count': data.get('view_count'),
  115. 'timestamp': parse_iso8601(data.get('released_at')),
  116. }