mirror of
https://github.com/c0de-archive/spotipy.git
synced 2024-11-04 23:17:48 +00:00
Merge pull request #149 from mbirtwell/python3_with_six
Use six to simplify python 2/3 compatibilty code where possible
This commit is contained in:
commit
f638b0c59b
@ -1 +1,2 @@
|
||||
requests==2.3.0
|
||||
six==1.10.0
|
5
setup.py
5
setup.py
@ -7,6 +7,9 @@ setup(
|
||||
author="@plamere",
|
||||
author_email="paul@echonest.com",
|
||||
url='http://spotipy.readthedocs.org/',
|
||||
install_requires=['requests>=1.0'],
|
||||
install_requires=[
|
||||
'requests>=1.0',
|
||||
'six>=1.10.0',
|
||||
],
|
||||
license='LICENSE.txt',
|
||||
packages=['spotipy'])
|
||||
|
@ -7,6 +7,8 @@ import requests
|
||||
import json
|
||||
import time
|
||||
|
||||
import six
|
||||
|
||||
''' A simple and thin Python library for the Spotify Web API
|
||||
'''
|
||||
|
||||
@ -418,12 +420,7 @@ class Spotify(object):
|
||||
- collaborative - optional is the playlist collaborative
|
||||
'''
|
||||
data = {}
|
||||
# Add Python2 and Python3 compatibility checking string types
|
||||
try:
|
||||
basestring
|
||||
except NameError:
|
||||
basestring = str
|
||||
if isinstance(name, basestring):
|
||||
if isinstance(name, six.string_types):
|
||||
data['name'] = name
|
||||
if isinstance(public, bool):
|
||||
data['public'] = public
|
||||
|
@ -8,18 +8,19 @@ import time
|
||||
import sys
|
||||
|
||||
# Workaround to support both python 2 & 3
|
||||
try:
|
||||
import urllib.request, urllib.error
|
||||
import urllib.parse as urllibparse
|
||||
except ImportError:
|
||||
import urllib as urllibparse
|
||||
|
||||
import six
|
||||
import six.moves.urllib.parse as urllibparse
|
||||
|
||||
|
||||
class SpotifyOauthError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
def _make_authorization_headers(client_id, client_secret):
|
||||
auth_header = base64.b64encode(six.text_type(client_id + ':' + client_secret).encode('ascii'))
|
||||
return {'Authorization': 'Basic %s' % auth_header.decode('ascii')}
|
||||
|
||||
|
||||
class SpotifyClientCredentials(object):
|
||||
OAUTH_TOKEN_URL = 'https://accounts.spotify.com/api/token'
|
||||
|
||||
@ -63,12 +64,7 @@ class SpotifyClientCredentials(object):
|
||||
"""Gets client credentials access token """
|
||||
payload = { 'grant_type': 'client_credentials'}
|
||||
|
||||
if sys.version_info[0] >= 3: # Python 3
|
||||
auth_header = base64.b64encode(str(self.client_id + ':' + self.client_secret).encode())
|
||||
headers = {'Authorization': 'Basic %s' % auth_header.decode()}
|
||||
else: # Python 2
|
||||
auth_header = base64.b64encode(self.client_id + ':' + self.client_secret)
|
||||
headers = {'Authorization': 'Basic %s' % auth_header}
|
||||
headers = _make_authorization_headers(self.client_id, self.client_secret)
|
||||
|
||||
response = requests.post(self.OAUTH_TOKEN_URL, data=payload,
|
||||
headers=headers, verify=True, proxies=self.proxies)
|
||||
@ -191,6 +187,9 @@ class SpotifyOAuth(object):
|
||||
except IndexError:
|
||||
return None
|
||||
|
||||
def _make_authorization_headers(self):
|
||||
return _make_authorization_headers(self.client_id, self.client_secret)
|
||||
|
||||
def get_access_token(self, code):
|
||||
""" Gets the access token for the app given the code
|
||||
|
||||
@ -206,12 +205,7 @@ class SpotifyOAuth(object):
|
||||
if self.state:
|
||||
payload['state'] = self.state
|
||||
|
||||
if sys.version_info[0] >= 3: # Python 3
|
||||
auth_header = base64.b64encode(str(self.client_id + ':' + self.client_secret).encode())
|
||||
headers = {'Authorization': 'Basic %s' % auth_header.decode()}
|
||||
else: # Python 2
|
||||
auth_header = base64.b64encode(self.client_id + ':' + self.client_secret)
|
||||
headers = {'Authorization': 'Basic %s' % auth_header}
|
||||
headers = self._make_authorization_headers()
|
||||
|
||||
response = requests.post(self.OAUTH_TOKEN_URL, data=payload,
|
||||
headers=headers, verify=True, proxies=self.proxies)
|
||||
@ -234,12 +228,7 @@ class SpotifyOAuth(object):
|
||||
payload = { 'refresh_token': refresh_token,
|
||||
'grant_type': 'refresh_token'}
|
||||
|
||||
if sys.version_info[0] >= 3: # Python 3
|
||||
auth_header = base64.b64encode(str(self.client_id + ':' + self.client_secret).encode())
|
||||
headers = {'Authorization': 'Basic %s' % auth_header.decode()}
|
||||
else: # Python 2
|
||||
auth_header = base64.b64encode(self.client_id + ':' + self.client_secret)
|
||||
headers = {'Authorization': 'Basic %s' % auth_header}
|
||||
headers = self._make_authorization_headers()
|
||||
|
||||
response = requests.post(self.OAUTH_TOKEN_URL, data=payload,
|
||||
headers=headers, proxies=self.proxies)
|
||||
|
Loading…
Reference in New Issue
Block a user