vessel.py 5.1 KB

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