Product
Introducing SSO
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
Readme
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:
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)
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
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
Check User's Saved Tracks
Get Current User's Saved Albums
Get User's Saved Shows
Get a User's Saved Tracks
Remove Albums for Current User
Remove User's Saved Shows
Remove User's Saved Tracks
Save Albums for Current User
Save Shows for Current 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:
List current user playlists:
Search:
Shows:
Get a Show
Get Several Shows
Get a Show's 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 of Spotify.auth_uri
method.
Spotify.play()
now accepts, track_ids
, artist_ids
etc. instead of resource_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:
FAQs
Sync/Async API wrapper for Spotify's web API
We found that pyfy demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.