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