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.

127 lines
4.4 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):
  36. for asset in data.get('assets', []):
  37. if asset.get('type') == asset_type:
  38. yield asset
  39. def _check_access_rights(self, data):
  40. access_info = data.get('__view', {})
  41. if not access_info.get('allow_access', True):
  42. err_code = access_info.get('error_code') or ''
  43. if err_code == 'ITEM_PAID_ONLY':
  44. raise ExtractorError(
  45. 'This video requires subscription.', expected=True)
  46. else:
  47. raise ExtractorError(
  48. 'Access to this content is restricted. (%s said: %s)' % (self.IE_NAME, err_code), expected=True)
  49. def _login(self):
  50. (username, password) = self._get_login_info()
  51. if username is None:
  52. return
  53. self.report_login()
  54. data = {
  55. 'client_id': 'web',
  56. 'type': 'password',
  57. 'user_key': username,
  58. 'password': password,
  59. }
  60. login_request = VesselIE.make_json_request(self._LOGIN_URL, data)
  61. self._download_webpage(login_request, None, False, 'Wrong login info')
  62. def _real_initialize(self):
  63. self._login()
  64. def _real_extract(self, url):
  65. video_id = self._match_id(url)
  66. webpage = self._download_webpage(url, video_id)
  67. data = self._parse_json(self._search_regex(
  68. r'App\.bootstrapData\((.*?)\);', webpage, 'data'), video_id)
  69. asset_id = data['model']['data']['id']
  70. req = VesselIE.make_json_request(
  71. self._API_URL_TEMPLATE % asset_id, {'client': 'web'})
  72. data = self._download_json(req, video_id)
  73. self._check_access_rights(data)
  74. try:
  75. video_asset = next(VesselIE.find_assets(data, 'video'))
  76. except StopIteration:
  77. raise ExtractorError('No video assets found')
  78. formats = []
  79. for f in video_asset.get('sources', []):
  80. if f['name'] == 'hls-index':
  81. formats.extend(self._extract_m3u8_formats(
  82. f['location'], video_id, ext='mp4', m3u8_id='m3u8'))
  83. else:
  84. formats.append({
  85. 'format_id': f['name'],
  86. 'tbr': f.get('bitrate'),
  87. 'height': f.get('height'),
  88. 'width': f.get('width'),
  89. 'url': f['location'],
  90. })
  91. self._sort_formats(formats)
  92. thumbnails = []
  93. for im_asset in VesselIE.find_assets(data, 'image'):
  94. thumbnails.append({
  95. 'url': im_asset['location'],
  96. 'width': im_asset.get('width', 0),
  97. 'height': im_asset.get('height', 0),
  98. })
  99. return {
  100. 'id': video_id,
  101. 'title': data['title'],
  102. 'formats': formats,
  103. 'thumbnails': thumbnails,
  104. 'description': data.get('short_description'),
  105. 'duration': data.get('duration'),
  106. 'comment_count': data.get('comment_count'),
  107. 'like_count': data.get('like_count'),
  108. 'view_count': data.get('view_count'),
  109. 'timestamp': parse_iso8601(data.get('released_at')),
  110. }