Skip to main content

logo

Version info GitHub stars Discord

spotify.py

An API library for the spotify client and the Spotify Web API written in Python.

Spotify.py is an asyncronous API library for Spotify. While maintaining an emphasis on being purely asyncronous the library provides syncronous functionality with the spotify.sync module.

import spotify.sync as spotify  # Nothing requires async/await now!

Index

Installing

To install the library simply clone it and run setup.py

  • git clone https://github.com/mental32/spotify.py
  • python3 setup.py install

or use pypi

  • pip3 install spotify (latest stable)
  • pip3 install -U git+https://github.com/mental32/spotify.py (nightly)

Examples

Sorting a playlist by popularity

import sys

import spotify

playlist_uri =   # Playlist uri here
client_id =      # App client id here
secret =         # App secret here
token =          # User token here

client = spotify.Client(client_id, secret)

async def main():
    user = await spotify.User.from_token(client, token)

    playlists = filter((lambda playlist: playlist.uri == playlist_uri), await user.get_playlists())

    try:
        playlist = next(playlists)
    except StopIteration:
        print('No playlists were found!', file=sys.stderr)
        return
    else:
        await playlist.sort(reverse=True, key=(lambda track: track.popularity))

if __name__ == '__main__':
    client.loop.run_until_complete(main())

Usage with flask

from typing import Tuple

import flask
import spotify.sync as spotify

SPOTIFY_CLIENT = spotify.Client('SPOTIFY_CLIENT_ID', 'SPOTIFY_CLIENT_SECRET')

APP = flask.Flask(__name__)
APP.config.from_mapping({'spotify_client': SPOTIFY_CLIENT})

REDIRECT_URI: str = 'http://localhost:8888/spotify/callback'

OAUTH2_SCOPES: Tuple[str] = ('user-modify-playback-state', 'user-read-currently-playing', 'user-read-playback-state')
OAUTH2: spotify.OAuth2 = spotify.OAuth2(SPOTIFY_CLIENT.id, REDIRECT_URI, scope=OAUTH2_SCOPES)

@APP.route('/spotify/callback')
def spotify_callback():
    try:
        code = flask.request.args['code']
    except KeyError:
        return flask.redirect('/spotify/failed')
    else:
        flask.session['spotify_user'] = spotify.User.from_code(
            SPOTIFY_CLIENT,
            code,
            redirect_uri=REDIRECT_URI,
            refresh=True
        )

    return flask.redirect('/')

@APP.route('spotify/failed')
def spotify_failed():
    flask.session.pop('spotify_user', None)
    return 'Failed to authenticate with Spotify.'

@APP.route('/')
@APP.route('/index')
def index():
    try:
        return repr(flask.session['spotify_user'])
    except KeyError:
        return flask.redirect(OAUTH2.url)

if __name__ == '__main__':
    APP.run('127.0.0.1', port=8888, debug=False)

Resources

For resources look at the examples or ask in the discord

Release files for spotify 0.6.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for spotify 0.6.1
File Size Uploaded
spotify-0.6.1.tar.gz 34.8 kB Details

Release files / spotify-0.6.1.tar.gz

Download URL spotify-0.6.1.tar.gz
Size 34.8 kB
Tags Source
SHA-256 checksum
How to use checksums
fe03c80540e634227da5830265c0bdf5adc1cc7fef2f1a7c14052ce587b25480
BLAKE2b-256 checksum
How to use checksums
40ffb5dc553b5b644fae0a278ce062b94dd227a5aacb0844d2d86158a56dc9ce
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.4

Release history Release notifications | RSS feed

0.10.2

2 release files

0.10.1

2 release files

0.10.0

2 release files

0.9.0

2 release files

0.8.6

2 release files

0.8.5

2 release files

0.8.4

2 release files

0.8.3

2 release files

0.8.2

2 release files

0.8.1

2 release files

0.8.0

2 release files

0.7.0

1 release file

0.6.3

1 release file

This release

0.6.1 This release

1 release file

0.6.0

1 release file

0.5.1

1 release file

0.5.0

1 release file

0.4.8

1 release file

0.4.0

1 release file

0.3.0

1 release file

0.2.0

1 release file

0.1.9

1 release file

0.1.8

1 release file

0.1.7

1 release file

0.1.6

1 release file

0.1.5

1 release file

0.1.4

1 release file

0.1.3

2 release files

0.1.2

1 release file

0.1.1

1 release file

0.0.2

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page