Skip to main content

Sync/Async API wrapper for Spotify's web API

Project description

Logo

Build Status Software License

Web API Wrapper for Spotify in Python

Features

  • Async and Sync clients
  • Authenticate using:
    • OAuth2 client credentials flow
    • OAuth2 authroization code flow
    • Access token only authorization
  • Covers every parameter for every endpoint in Spotify's Web API
  • Automatically refreshes tokens for clients and users
  • Descriptive errors
  • Able to automatically default to user's locales
  • Rate limiting
  • HTTP and SOCKS proxies
  • HTTP caching (Sync only)
  • Unit and integration tested
  • Fit for both production and experimental/personal environments
  • Begginner friendly interface
  • Almost identical Async and Sync Interfaces

Documentation

Link to Readthedocs

Setup

$ pip install --upgrade --user pyfy

Quick Start

Sync

from pyfy import Spotify

spt = Spotify('your_access_token')

spt.user_playlists()
spt.play()
spt.volume(85)
spt.next()
spt.pause()
results = spt.search(q='alice in chains them bones')
print(results)

Async

1. Make a single call:

import asyncio
from pyfy import AsyncSpotify

spt = AsyncSpotify('your_access_token')

async def query():
    return await spt.search('Like a motherless child')

asyncio.run(query())

or even:

awaited_search_result = asyncio.run(spt.search('A tout le monde'))

2. Make multiple calls using a single TCP connection (no async/await syntax):

from pprint import pprint
from pyfy import AsyncSpotify

spt = AsyncSpotify('your_access_token')

gathered_results = spt.gather_now(
    spt.search('Seize the day', to_gather=True),
    spt.search('Feel good inc'', to_gather=True),
    spt.search('In your room', to_gather=True),
    spt.search('Tout Petit Moineau', to_gather=True)
)

pprint(gathered_results)

3. To manually await the results:

import asyncio
from pyfy import AsyncSpotify

spt = AsyncSpotify('your_access_token')

async def runner():
    return await spt.gather(
        spt.search('Saeed', to_gather=True),
        spt.search('Killing time', to_gather=True),
        spt.search('Project 100', to_gather=True),
        spt.search('Tout Petit Moineau', to_gather=True)
    )

results = asyncio.run(runner())

Authentication and Authorization

1. With User's Access Token: *get from here

from pyfy import Spotify

spt = Spotify('your access token')

2. With Client Credentials Flow (OAauth2): *get from here

from pyfy import ClientCreds, Spotify

client = ClientCreds(client_id=client_id, client_secret=client_secret)
spt = Spotify(client_creds=client)
spt.authorize_client_creds()

3. With Authorization Code Flow (OAuth2) *examples with Sanic(async) and Flask(sync) here

from pyfy import Spotify, ClientCreds, UserCreds, AuthError, ApiError

client = ClientCreds(client_id='clientid', client_secret='client_secret')
spt = Spotify(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)

👨 Ways to load Credentials (User & Client)

# Instantiate directly
client = ClientCreds(client_id='aclientid', client_secret='averysecrettoken')

# Load from environment
client = ClientCreds()
client.load_from_env()

# Load from json file
client = ClientCreds()
client.load_from_json(path='full/dir/path', name='name_of_the_json_file')

🎶 Resources

  • Playback:
    • devices()
    • play()
    • pause()
    • repeat()
    • seek()
    • previous()
    • shuffle()
    • recently_played_tracks()
    • currently_playing_info()
    • currently_playing()
    • playback_transfer()
    • volume()
  • Playlists:
    • playlist()
    • user_playlists()
    • follows_playlist()
    • follow_playlist()
    • create_playlist()
    • update_playlist()
    • unfollow_playlist()
    • delete_playlist()
  • Playlist Contents:
    • playlist_tracks():
    • add_playlist_tracks()
    • reorder_playlist_track()
    • delete_playlist_tracks()
  • Tracks:
    • tracks()
    • user_tracks()
    • owns_tracks()
    • save_tracks()
    • delete_tracks()
    • user_top_tracks()
  • Albums:
    • albums()
    • user_albums()
    • owns_albums()
    • save_albums()
    • delete_albums()
  • Artists:
    • artists()
    • followed_artists()
    • follows_artists()
    • follow_artists()
    • artist_related_artists()
  • Users:
    • me()
    • is_premium
    • user_profile()
    • follows_users()
    • unfollow_users()
    • follow_users()
  • Others:
    • user_top_artists()
    • artist_albums()
    • album_tracks()
    • artist_top_tracks()
    • next_page()
    • previous_page()
  • Explore and Personalization:
    • available_genre_seeds()
    • categories()
    • category_playlist()
    • featured_playlists()
    • new_releases()
    • search()
    • recommendations()
    • tracks_audio_features()

Contribute

  • All kinds of contributions are welcome :)

Contributors

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyfy-1.2.1.tar.gz (41.5 kB view details)

Uploaded Source

File details

Details for the file pyfy-1.2.1.tar.gz.

File metadata

  • Download URL: pyfy-1.2.1.tar.gz
  • Upload date:
  • Size: 41.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.7

File hashes

Hashes for pyfy-1.2.1.tar.gz
Algorithm Hash digest
SHA256 91c852becf46e6fc767d911af6a650d0b59c1ebd40952c0da3ba68af1e9e6713
MD5 32c92fbb101e13e975199767aebaa59c
BLAKE2b-256 9c3f710aa5ae3c956fb792384b9407c81b7a5d2cdbdc22e7961b38389c9f9163

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page