[vevo] move authentication to VevoBase
now it can be used by playlist extractor
This commit is contained in:
parent
60de4c4410
commit
f31c8182e7
@ -24,6 +24,53 @@ class VevoBaseIE(InfoExtractor):
|
|||||||
webpage, 'initial store'),
|
webpage, 'initial store'),
|
||||||
video_id)
|
video_id)
|
||||||
|
|
||||||
|
def _initialize_api(self, refresh=False):
|
||||||
|
data = {'client_id': 'SPupX1tvqFEopQ1YS6SS'}
|
||||||
|
if refresh:
|
||||||
|
data.update({
|
||||||
|
'grant_type': 'refresh_token',
|
||||||
|
'refresh_token': self._REFRESH_TOKEN,
|
||||||
|
})
|
||||||
|
else:
|
||||||
|
data['grant_type'] = 'urn:vevo:params:oauth:grant-type:anonymous'
|
||||||
|
post_data = json.dumps(data).encode('utf-8')
|
||||||
|
|
||||||
|
webpage = self._download_webpage(
|
||||||
|
'https://accounts.vevo.com/token', 'token',
|
||||||
|
data=post_data, headers={'Content-Type': 'application/json'},
|
||||||
|
note='Retrieving oauth token',
|
||||||
|
errnote='Unable to retrieve oauth token')
|
||||||
|
|
||||||
|
if re.search(r'(?i)THIS PAGE IS CURRENTLY UNAVAILABLE IN YOUR REGION', webpage):
|
||||||
|
self.raise_geo_restricted(
|
||||||
|
'%s said: This page is currently unavailable in your region' % self.IE_NAME)
|
||||||
|
|
||||||
|
auth_info = self._parse_json(webpage, 'token')
|
||||||
|
|
||||||
|
self._ACCESS_TOKEN = auth_info['legacy_token']
|
||||||
|
self._REFRESH_TOKEN = auth_info['refresh_token']
|
||||||
|
|
||||||
|
def _call_api(self, path, *args, **kwargs):
|
||||||
|
try:
|
||||||
|
data = self._download_json(
|
||||||
|
'https://apiv2.vevo.com/%s' % path, headers={
|
||||||
|
'Authorization': 'Bearer %s' % self._ACCESS_TOKEN,
|
||||||
|
}, *args, **kwargs)
|
||||||
|
except ExtractorError as e:
|
||||||
|
if isinstance(e.cause, compat_HTTPError):
|
||||||
|
errors = self._parse_json(e.cause.read().decode(), None)['errors']
|
||||||
|
error_messages = [error['message'] for error in errors]
|
||||||
|
if 'Token is expired' in error_messages:
|
||||||
|
self._initialize_api(refresh=True)
|
||||||
|
return self._call_api(path, *args, **kwargs)
|
||||||
|
else:
|
||||||
|
error_message = ', '.join(error_messages)
|
||||||
|
raise ExtractorError(
|
||||||
|
'%s said: %s' % (self.IE_NAME, error_message),
|
||||||
|
expected=True)
|
||||||
|
raise
|
||||||
|
return data
|
||||||
|
|
||||||
|
|
||||||
class VevoIE(VevoBaseIE):
|
class VevoIE(VevoBaseIE):
|
||||||
'''
|
'''
|
||||||
@ -153,53 +200,6 @@ class VevoIE(VevoBaseIE):
|
|||||||
4: 'amazon',
|
4: 'amazon',
|
||||||
}
|
}
|
||||||
|
|
||||||
def _initialize_api(self, refresh=False):
|
|
||||||
data = {'client_id': 'SPupX1tvqFEopQ1YS6SS'}
|
|
||||||
if refresh:
|
|
||||||
data.update({
|
|
||||||
'grant_type': 'refresh_token',
|
|
||||||
'refresh_token': self._REFRESH_TOKEN,
|
|
||||||
})
|
|
||||||
else:
|
|
||||||
data['grant_type'] = 'urn:vevo:params:oauth:grant-type:anonymous'
|
|
||||||
post_data = json.dumps(data).encode('utf-8')
|
|
||||||
|
|
||||||
webpage = self._download_webpage(
|
|
||||||
'https://accounts.vevo.com/token', 'token',
|
|
||||||
data=post_data, headers={'Content-Type': 'application/json'},
|
|
||||||
note='Retrieving oauth token',
|
|
||||||
errnote='Unable to retrieve oauth token')
|
|
||||||
|
|
||||||
if re.search(r'(?i)THIS PAGE IS CURRENTLY UNAVAILABLE IN YOUR REGION', webpage):
|
|
||||||
self.raise_geo_restricted(
|
|
||||||
'%s said: This page is currently unavailable in your region' % self.IE_NAME)
|
|
||||||
|
|
||||||
auth_info = self._parse_json(webpage, 'token')
|
|
||||||
|
|
||||||
self._ACCESS_TOKEN = auth_info['legacy_token']
|
|
||||||
self._REFRESH_TOKEN = auth_info['refresh_token']
|
|
||||||
|
|
||||||
def _call_api(self, path, *args, **kwargs):
|
|
||||||
try:
|
|
||||||
data = self._download_json(
|
|
||||||
'https://apiv2.vevo.com/%s' % path, headers={
|
|
||||||
'Authorization': 'Bearer %s' % self._ACCESS_TOKEN,
|
|
||||||
}, *args, **kwargs)
|
|
||||||
except ExtractorError as e:
|
|
||||||
if isinstance(e.cause, compat_HTTPError):
|
|
||||||
errors = self._parse_json(e.cause.read().decode(), None)['errors']
|
|
||||||
error_messages = [error['message'] for error in errors]
|
|
||||||
if 'Token is expired' in error_messages:
|
|
||||||
self._initialize_api(refresh=True)
|
|
||||||
return self._call_api(path, *args, **kwargs)
|
|
||||||
else:
|
|
||||||
error_message = ', '.join(error_messages)
|
|
||||||
raise ExtractorError(
|
|
||||||
'%s said: %s' % (self.IE_NAME, error_message),
|
|
||||||
expected=True)
|
|
||||||
raise
|
|
||||||
return data
|
|
||||||
|
|
||||||
def _real_extract(self, url):
|
def _real_extract(self, url):
|
||||||
video_id = self._match_id(url)
|
video_id = self._match_id(url)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user