Pyfy is a Sync + Async Pythonic Spotify Client that focuses on ease of use in personal projects and API stability and security for production grade codebases.
$ pip install pyfy
Sync
from pyfy import Spotify
spt = Spotify('your_access_token')
spt.play()
spt.volume(85)
spt.next()
spt.pause()
Async
import asyncio
from pyfy import AsyncSpotify
spt = AsyncSpotify('your_access_token')
async def search():
return await spt.search('A tout le monde')
search_result = asyncio.run(search())
You should start by creating client credentials from Spotify's Developer's console.
Next, edit your application's settings and set a Redirect URL. If it's for personal use then set it as:
http://localhost:9000 Port can be any port of choice, not necessarily 9000
Next, copy your:
- Client ID
- Client Secret
- Redirect URL (That you just set)
Next, figure out the scopes that you think you'll need from here: https://developer.spotify.com/documentation/general/guides/scopes/
e.g. ["user-library-modify", "app-remote-control"]
Next, follow the first authentication scheme from below (it's the one you'll most likely need, unless you're sure otherwise)
1. Authorization Code Flow (OAuth2) (recommended)
Suitable if you want to access user-related resources. e.g. user-playlists, user-tracks etc.
Click here for full working examples with Sanic(async) and Flask(sync).
from pyfy import Spotify, ClientCreds, UserCreds, AuthError, ApiError
client = ClientCreds(
client_id='clientid',
client_secret='client_secret',
redirect_uri='https://localhost:9000",
scopes=["user-library-modify", "app-remote-control"]
)
spt = Spotify(client_creds=client)
def authorize():
# Fist step of OAuth, Redirect user to spotify's authorization endpoint
if spt.is_oauth_ready:
return redirect(spt.auth_uri())
# Authorization callback
def callback(grant):
try:
user_creds = spt.build_credentials(grant=grant)
except AuthError as e:
abort(401)
logging.info(e.msg)
logging.info(e.http_response)
else:
db.insert(user_creds)
return redirect(url_for_home)
def get_user_tracks():
try:
return json.dumps(spt.user_tracks())
except ApiError:
abort(500)
2. User's Access Token: get from here
Same as the Authorization Code Flow above but without a refresh token. Suitable for quick runs.
from pyfy import Spotify
spt = Spotify('your access token')
3. Client Credentials Flow (OAuth2): get from here
Suitable for when you want to access public information quickly. (Accessing user information is porhibited using this method)
from pyfy import ClientCreds, Spotify
client = ClientCreds(client_id=client_id, client_secret=client_secret)
spt = Spotify(client_creds=client)
spt.authorize_client_creds()
- Get an album:
- Get an album's tracks:
- Get several albums:
- Get an artist:
- Artist albums:
- Artist top tracks:
- Artist related artists:
- Get several artists:
- Get a category:
- Get a category's playlists:
- Get list of categories:
- Get a list of featured playlists:
- Get a list of new releases:
- Get recommendations based on seeds:
- Get an episode:
- Pyfy: TODO
- Web API reference: https://developer.spotify.com/documentation/web-api/reference/episodes/get-an-episode/
- Get several episodes:
- Pyfy: TODO
- Web API reference: https://developer.spotify.com/documentation/web-api/reference/episodes/get-several-episodes/
- Check if Current User Follows Artists or Users:
- Check if Users Follow a Playlist:
- Follow Artists:
- Follow Users:
- Follow a playlist:
- Get User's Followed Artists:
- Unfollow Artists:
- Unfollow Users:
- Unfollow Playlist:
- Check User's Saved Albums:
- Check User's Saved Shows:
- Pyfy: TODO
- Web API reference: https://developer.spotify.com/documentation/web-api/reference/library/check-users-saved-shows/
- Check User's Saved Tracks:
- Get Current User's Saved Albums:
- Get User's Saved Shows:
- Pyfy: TODO
- Web API reference: https://developer.spotify.com/documentation/web-api/reference/library/get-users-saved-shows/
- Get a User's Saved Tracks:
- Remove Albums for Current User:
- Remove User's Saved Shows:
- Pyfy: TODO
- Web API reference: https://developer.spotify.com/documentation/web-api/reference/library/remove-shows-user/
- Remove User's Saved Tracks:
- Save Albums for Current User:
- Save Shows for Current User:
- Pyfy: TODO
- Web API reference: https://developer.spotify.com/documentation/web-api/reference/library/save-shows-user/
- Save Tracks for User:
- Get a User's Top Artists:
- Get a User's Top Tracks:
- Add an Item to the User's Playback Queue:
- Get a User's Available Devices:
- Get Information About The User's Current Playback:
- Get Current User's Recently Played Tracks:
- Get the User's Currently Playing Track:
- Pause a User's Playback:
- Seek To Position In Currently Playing Track:
- Set Repeat Mode On User’s Playback:
- Set Volume For User's Playback:
- Skip User’s Playback To Next Track:
- Skip User’s Playback To Previous Track:
- Start/Resume a User's Playback:
- Toggle Shuffle For User’s Playback:
- Transfer a User's Playback:
- Add playlist items:
- Edit playlist:
- Create playlist:
- List a user's playlists:
- Playlist cover:
- Pyfy: TODO
- Web API reference: https://developer.spotify.com/documentation/web-api/reference/playlists/get-playlist-cover/
- List a playlist:
- List a playlist items:
- Remove playlist items:
- Reorder playlist items:
- Replace playlist items:
- Upload custom playlist cover image:
- Pyfy: TODO
- Web API reference: https://developer.spotify.com/documentation/web-api/reference/playlists/upload-custom-playlist-cover/
- List current user playlists:
- Search for an item:
- Get a Show:
- Pyfy: TODO
- Web API reference: https://developer.spotify.com/documentation/web-api/reference/shows/get-a-show/
- Get Several Shows:
- Pyfy: TODO
- Web API reference: https://developer.spotify.com/documentation/web-api/reference/shows/get-several-shows/
- Get a Show's Episodes:
- Pyfy: TODO
- Web API reference: https://developer.spotify.com/documentation/web-api/reference/shows/get-shows-episodes/
- Get Audio Analysis for a Track:
- Get Audio Features for a Track:
- Get Audio Features for Several Tracks:
- Get Several Tracks:
- Get a Track:
- Get Current User's Profile:
- Get a User's Profile:
from pyfy import Spotify
user_creds = {'access_token': '...', 'refresh_token': '....'}
spt = Spotify(user_creds=user_creds)
user_top_tracks = spt.user_top_tracks(limit=5)
next_page_1 = spt.next_page(user_top_tracks)
next_page_2 = spt.next_page(next_page_1)
previous_page_1 = spt.previous_page(next_page_2)
previous_page_1 === next_page_1 # True
pyfy.sync_client.Spotify
pyfy.sync_client.Spotify
pyfy.excs
pyfy.creds
$ tox
Open tox.ini and change thoee values to:
SPOTIFY_CLIENT_ID
Create an appSPOTIFY_CLIENT_SECRET
Create an appSPOTIFY_ACCESS_TOKEN
Get one or perform OAuth2 Auth Code Flow.Note
Check all scopes when getting an access token.
SPOTIFY_REFRESH_TOKEN
Note
To avoid manually refreshing your access token from the dev console, run the Oauth2 example in the examples dir. Then copy and paste the refresh token returned to your tox file.
SPOTIFY_REDIRECT_URI = 'http://localhost:5000/callback/spotify'
Note
You have to register this callback in your Application's dashboard https://developer.spotify.com/dashboard/applications
PYFY_TEST_INTEGRATION_SYNC` = true
PYFY_TEST_INTEGRATION_ASYNC = true
Run:
Note
- This will run some tests using your client ID, client secret and access token.
- Unfortunately Spotify does not have a sandbox API, so we have to test it against the live API
- Tests will carefully teardown all resources created and/or modified
- Integration tests will not be abusive to the API and should only test for successful integration with minimum API calls
- OAuth2 flow isn't tested in the tests folder (yet). Instead you can manually test it in the examples folder by running:
pip install flask pyfy && python examples/oauth2.py
$ tox
genindex
modindex
search