Skip to content

Commit

Permalink
Merge pull request #4 from Scientific-Guy/master
Browse files Browse the repository at this point in the history
v0.0.2 Update
  • Loading branch information
scientific-dev authored Sep 26, 2020
2 parents ab1f7a0 + ecc9075 commit 35a19fb
Show file tree
Hide file tree
Showing 12 changed files with 219 additions and 254 deletions.
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Spotify-Api.py ChangeLog

---

## v0.0.2 - Stable Version

- Custom Exceptions
- Styled Source Codes
- Cleared Unwanted Codes and Shrinked to look decent

---

## v0.0.2 - Stable Dev Version

- Initial Update
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,9 @@ So, hey facing troubles? We have some ways you to support you!

- [Discord Support Server](https://discord.gg/FrduEZd)
- [GitHub Repo](https://github.com/spotify-api/spotify-api.py/)

## Things to be updated

- Make Errors when Spotify Api responses with bad response

Thing to be updated will be updated in next version...
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
from setuptools import setup

def readme():
with open('README.md') as f:
README = f.read()
return README

setup(
name="spotify-api.py",
version="0.0.1",
version="0.0.2",
url="https://github.com/spotify-api/spotify-api.py",
description="A simple wrapper for spotify api written in python!",
long_description=readme(),
Expand Down
92 changes: 41 additions & 51 deletions spotifyapi/Album.py
Original file line number Diff line number Diff line change
@@ -1,58 +1,48 @@
# Import Packages
import requests
from .Util import encodeURIComponent
from .exceptions import LimitOutOfRangeError

from .Exception import *

# Album Class
class Album():

def __init__(self, token: str):
self.token = token

def search(self, query: str, limit: int = 1):
link = 'https://api.spotify.com/v1/search'
header = {'Authorization': 'Bearer ' + self.token}

if not 0 < limit < 50:
raise LimitOutOfRangeError('Limit must be under 50.')

return requests.request(
'GET',
link,
headers=header,
params={
'q': encodeURIComponent(query),
'type': 'album',
'limit': limit,
'market': 'US'
}
).json()

def get(self, albumID: str):
link = 'https://api.spotify.com/v1/albums/' + albumID
header = {'Authorization': 'Bearer ' + self.token}

return requests.request(
'GET',
link,
headers=header
).json()

def get_tracks(self, albumID: str, limit: int = 1):
link = 'https://api.spotify.com/v1/albums/' + albumID + '/tracks'
header = {'Authorization': 'Bearer ' + self.token}

if not 0 < limit < 50:
raise LimitOutOfRangeError('Limit must be under 50.')

return requests.request(
'GET',
link,
headers=header,
params={
'offset': 0,
'limit': limit,
'market': 'US'
}
).json()
def __init__(self, token: str):
self.token = token

def search(self, query: str, limit: int = 1):
if not 0 < limit < 50:
raise LimitOutOfRangeError('limit must be under 50')

return requests.request(
'GET',
'https://api.spotify.com/v1/search',
headers={'Authorization': 'Bearer ' + self.token},
params={
'q': encodeURIComponent(query),
'type': 'album',
'limit': limit,
'market': 'US'
}
).json()

def get(self, albumID: str):
return requests.request(
'GET',
'https://api.spotify.com/v1/albums/' + albumID,
headers={'Authorization': 'Bearer ' + self.token}
).json()

def get_tracks(self, albumID: str, limit: int = 1):
if not 0 < limit < 50:
raise LimitOutOfRangeError('limit must be under 50')

return requests.request(
'GET',
'https://api.spotify.com/v1/albums/' + albumID + '/tracks',
headers={'Authorization': 'Bearer ' + self.token},
params={
'offset': 0,
'limit': limit,
'market': 'US'
}
).json()
129 changes: 56 additions & 73 deletions spotifyapi/Artist.py
Original file line number Diff line number Diff line change
@@ -1,80 +1,63 @@
# Import Packages
import requests
from .Util import encodeURIComponent
from .exceptions import LimitOutOfRangeError
from .Exception import *

# Artist Class
class Artist():

def __init__(self, token: str):
self.token = token

def search(self, query: str, limit: int = 1):
link = 'https://api.spotify.com/v1/search'
header = {'Authorization': 'Bearer ' + self.token}

if not 0 < limit < 50:
raise LimitOutOfRangeError('Limit must be under 50.')

return requests.request(
'GET',
link,
headers=header,
params={
'q': encodeURIComponent(query),
'type': 'artist',
'limit': limit,
'market': 'US'
}
).json()

def get(self, artistID: str):
link = 'https://api.spotify.com/v1/artists/' + artistID
header = {'Authorization': 'Bearer ' + self.token}

return requests.request(
'GET',
link,
headers=header
).json()

def albums(self, artistID: str, limit: int = 1):
link = 'https://api.spotify.com/v1/artists/' + artistID + '/albums'
header = {'Authorization': 'Bearer ' + self.token}

if not 0 < limit < 50:
raise LimitOutOfRangeError('Limit must be under 50.')

return requests.request(
'GET',
link,
headers=header,
params={
'include_groups': 'single',
'limit': limit,
'market': 'US'
}
).json()

def top_tracks(self, artistID: str):
link = 'https://api.spotify.com/v1/artists/' + artistID + '/top-tracks'
header = {'Authorization': 'Bearer ' + self.token}

return requests.request(
'GET',
link,
headers=header,
params={
'country': 'US'
}
).json()

def related_artists(self, artistID: str):
link = 'https://api.spotify.com/v1/artists/' + artistID + '/related-artists'
header = {'Authorization': 'Bearer ' + self.token}

return requests.request(
'GET',
link,
headers=header
).json()
def __init__(self, token: str):
self.token = token

def search(self, query: str, limit: int = 1):
if not 0 < limit < 50:
raise LimitOutOfRangeError('limit must be under 50')

return requests.request(
'GET',
'https://api.spotify.com/v1/search',
headers={'Authorization': 'Bearer ' + self.token},
params={
'q': encodeURIComponent(query),
'type': 'artist',
'limit': limit,
'market': 'US'
}
).json()

def get(self, artistID: str):
return requests.request(
'GET',
'https://api.spotify.com/v1/artists/' + artistID,
headers={'Authorization': 'Bearer ' + self.token}
).json()

def albums(self, artistID: str, limit: int = 1):
if not 0 < limit < 50:
raise LimitOutOfRangeError('limit must be under 50')

return requests.request(
'GET',
'https://api.spotify.com/v1/artists/' + artistID + '/albums',
headers={'Authorization': 'Bearer ' + self.token},
params={
'include_groups': 'single',
'limit': limit,
'market': 'US'
}
).json()

def top_tracks(self, artistID: str):
return requests.request(
'GET',
'https://api.spotify.com/v1/artists/' + artistID + '/top-tracks',
headers={'Authorization': 'Bearer ' + self.token},
params={'country': 'US'}
).json()

def related_artists(self, artistID: str):
return requests.request(
'GET',
'https://api.spotify.com/v1/artists/' + artistID + '/related-artists',
headers={'Authorization': 'Bearer ' + self.token}
).json()
14 changes: 14 additions & 0 deletions spotifyapi/Exception.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Base Error Class
class SpotifyApiError(Exception):

def __init__(self, message):
self.message = message

def __str__(self):
return self.message

# Limit out of range error
class LimitOutOfRangeError(SpotifyApiError):

def __init__(self, message):
super().__init__(message)
27 changes: 11 additions & 16 deletions spotifyapi/Oauth.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,15 @@
# Read Class
class Auth():

def __init__(self, oauth: str):
self.token = oauth
def __init__(self, oauth: str):
self.token = oauth

def get(self, client_id: str, client_secret: str):
header_data = b64(str(client_id) + ':' + str(client_secret))
header = {
'Authorization': 'Basic {}'.format(header_data)
}

return requests.request(
'POST',
'https://accounts.spotify.com/api/token',
data={
'grant_type': 'client_credentials'
},
headers=header
).json()
def get(self, client_id: str, client_secret: str):
return requests.request(
'POST',
'https://accounts.spotify.com/api/token',
data={
'grant_type': 'client_credentials'
},
headers={'Authorization': 'Basic ' + b64(str(client_id) + ':' + str(client_secret))}
).json()
Loading

0 comments on commit 35a19fb

Please sign in to comment.