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