Welcome 👋
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.
Setup ⚙️
$ pip install pyfy
Quick Start 🎛️
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())
Getting Started 👩
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)
Authentication Schemes 👩🎤
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 (OAauth2):
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()
API endpoints 🌐
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
Pagination 📖
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
Documentation 📑
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
Backward Incompatibility Notices
V2:
-
Removed
Spotify.oauth_uriproperty in favor ofSpotify.auth_urimethod. -
Spotify.play()now accepts,track_ids,artist_idsetc. instead ofresource_ids+resource_names -
Oauth2 state handling:
-
Removed deprecated
enforce_state_checkfunctionality -
Removed state attribute from
user_creds -
Oauth2 state checking is no longer done by Pyfy's client and should be handled manually
-
Testing 👩🔬:
Please visit: https://pyfy.readthedocs.io/en/latest/#testing
Contributors
Big thank you to our amazing contributors:
Metadata
Release files for pyfy 2.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| pyfy-2.2.0.tar.gz | 49.6 kB | Without provenance | Details for pyfy-2.2.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| pyfy-2.2.0-py3-none-any.whl | Python 3 | none | any | Details for pyfy-2.2.0-py3-none-any.whl |
Total release size: 107.2 kB
Release files / pyfy-2.2.0.tar.gz
| Download URL | pyfy-2.2.0.tar.gz |
|---|---|
| Size | 49.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d61c7426c76a458179b01ed2d4a905dc7c26c07afb67dd2a656057eea7dd6e59
|
|
BLAKE2b-256 checksum How to use checksums |
9cdbb1e5647faecd5943d78b5db9a1d4e9e0103c3f025f46e2d539ea11c1609b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.0
|
Release files / pyfy-2.2.0-py3-none-any.whl
| Download URL | pyfy-2.2.0-py3-none-any.whl |
|---|---|
| Size | 57.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
e1ebdf3aaef20169e2520da1ac8e42cb013e14afbba846dc44ecca21e2e52299
|
|
BLAKE2b-256 checksum How to use checksums |
a530dd50f42f61a1ac00c30806c2a5fab80c1b162eefa5423e8b924d1f93ac4d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.0
|
Security for pyfy 2.2.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.