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 Developers 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)
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')
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()
Albums:
-
Get an album
-
Get an album's tracks
-
Get several albums
Artists:
-
Get an artist
-
Artist albums
-
Artist top tracks
-
Artist related artists
-
Get several artists
Browse:
-
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
Episodes:
-
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/
Follow:
-
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
User Library:
-
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
Personalization:
-
Get a User's Top Artists
-
Get a User's Top Tracks
Player:
-
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
Playlists:
-
Add playlist items:
-
Edit playlist:
-
Create playlist:
-
List a user's playlists:
-
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:
- Search for an item
Shows:
-
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/
Tracks:
-
Get Audio Analysis for a Track
-
Get Audio Features for a Track
-
Get Audio Features for Several Tracks
-
Get Several Tracks
-
Get a Track
Users Profile:
-
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
For a detailed documentation of Pyfy's API, please visit: https://pyfy.readthedocs.io/en/latest where you'll find:
-
Sync client API 🎸: https://pyfy.readthedocs.io/en/latest/#sync-client-api
-
Async client API 🎼: https://pyfy.readthedocs.io/en/latest/#async-client-api
-
Exceptions API
⚠️ : https://pyfy.readthedocs.io/en/latest/#module-pyfy.excs -
Credentials API 📇: https://pyfy.readthedocs.io/en/latest/#module-pyfy.creds
V2:
-
Removed
Spotify.oauth_uri
property in favor ofSpotify.auth_uri
method. -
Spotify.play()
now accepts,track_ids
,artist_ids
etc. instead ofresource_ids
+resource_names
-
Oauth2 state handling:
-
Removed deprecated
enforce_state_check
functionality -
Removed state attribute from
user_creds
-
Oauth2 state checking is no longer done by Pyfy's client and should be handled manually
-
Please visit: https://pyfy.readthedocs.io/en/latest/#testing
Big thank you to our amazing contributors: