### Setup Virtual Environment, Install Dependencies, and Run Tests Source: https://github.com/spotipy-dev/spotipy/blob/master/CONTRIBUTING.md Steps to create a virtual environment, activate it, install the project dependencies, and run the test suite. ```bash $ virtualenv --python=python3 env $ source env/bin/activate (env) $ pip install -e . (env) $ python -m unittest discover -v tests ``` -------------------------------- ### Create Virtual Environment and Install Dependencies Source: https://github.com/spotipy-dev/spotipy/blob/master/docs/index.md Set up a Python virtual environment, install project dependencies, and install Spotipy in editable mode. Run tests to ensure the setup is correct. ```bash $ virtualenv –python=python3.12 env (env) $ pip install –user -e . (env) $ python -m unittest discover -v tests ``` -------------------------------- ### Install Spotipy Source: https://github.com/spotipy-dev/spotipy/blob/master/README.md Installs the Spotipy library using pip. Use the py command for Windows. ```bash pip install spotipy ``` ```bash py -m pip install spotipy ``` ```bash pip install spotipy --upgrade ``` -------------------------------- ### Install Spotipy Source: https://github.com/spotipy-dev/spotipy/blob/master/TUTORIAL.md Install the Spotipy library and upgrade to the latest version using pip. ```bash pip install spotipy --upgrade ``` -------------------------------- ### Install Test Dependencies Source: https://github.com/spotipy-dev/spotipy/blob/master/CONTRIBUTING.md Install the necessary packages for testing, including development dependencies. ```bash pip install "'.[test]'" ``` -------------------------------- ### Authorization Code Flow Quick Start Source: https://github.com/spotipy-dev/spotipy/blob/master/docs/index.md Authenticate using the Authorization Code Flow with a specified scope and retrieve saved tracks for the current user. This example demonstrates basic authentication and data retrieval. ```python import spotipy from spotipy.oauth2 import SpotifyOAuth scope = "user-library-read" sp = spotipy.Spotify(auth_manager=SpotifyOAuth(scope=scope)) results = sp.current_user_saved_tracks() for idx, item in enumerate(results['items']): track = item['track'] print(idx, track['artists'][0]['name'], " – ", track['name']) ``` -------------------------------- ### Spotipy Authentication Setup Source: https://github.com/spotipy-dev/spotipy/blob/master/TUTORIAL.md Initialize Spotipy with SpotifyOAuth, providing your application's client ID, client secret, redirect URI, and the scope for reading the user's library. ```python import spotipy from spotipy.oauth2 import SpotifyOAuth sp = spotipy.Spotify(auth_manager=SpotifyOAuth(client_id="YOUR_APP_CLIENT_ID", client_secret="YOUR_APP_CLIENT_SECRET", redirect_uri="YOUR_APP_REDIRECT_URI", scope="user-library-read")) ``` -------------------------------- ### Install Spotipy Package Source: https://github.com/spotipy-dev/spotipy/blob/master/TUTORIAL.md Install the Spotipy library using pip if you encounter a 'ModuleNotFoundError'. This command ensures the package is available for your project. ```bash pip install spotipy ``` -------------------------------- ### Authenticate and Fetch User Playlists Source: https://github.com/spotipy-dev/spotipy/blob/master/docs/index.md Use SpotifyClientCredentials to authenticate and then fetch a user's playlists. This example demonstrates iterating through paginated results. ```python import spotipy from spotipy.oauth2 import SpotifyClientCredentials auth_manager = SpotifyClientCredentials() sp = spotipy.Spotify(auth_manager=auth_manager) playlists = sp.user_playlists('spotify') while playlists: for i, playlist in enumerate(playlists['items']): print(f"{i + 1 + playlists['offset']:4d} {playlist['uri']} {playlist['name']}") if playlists['next']: playlists = sp.next(playlists) else: playlists = None ``` -------------------------------- ### Check Python Version Source: https://github.com/spotipy-dev/spotipy/blob/master/TUTORIAL.md Verify your installed Python version. This is useful for troubleshooting 'command not found' errors. ```bash python --version ``` ```bash python3 --version ``` -------------------------------- ### Verify Code Style with Flake8 Source: https://github.com/spotipy-dev/spotipy/blob/master/docs/index.md Install Flake8 to check your Python code for style guide violations and potential errors. This command checks the current directory. ```bash pip install flake8 flake8 . ``` -------------------------------- ### Check Import List Storage with Isort Source: https://github.com/spotipy-dev/spotipy/blob/master/docs/index.md Install Isort to ensure that import statements are correctly sorted and stored. This command checks the current directory for compliance. ```bash pip install isort isort . -c -v ``` -------------------------------- ### Get Top Tracks with Preview and Cover Art Source: https://github.com/spotipy-dev/spotipy/blob/master/docs/index.md Retrieves the top 10 tracks for a given artist, printing their names, preview URLs, and the URL of their album's cover art. Requires the artist's Spotify URI. ```python import spotipy from spotipy.oauth2 import SpotifyClientCredentials lz_uri = 'spotify:artist:36QJpDe2go2KgaRleHCDTp' spotify = spotipy.Spotify(client_credentials_manager=SpotifyClientCredentials()) results = spotify.artist_top_tracks(lz_uri) for track in results['tracks'][:10]: print('track : ' + track['name']) print('audio : ' + track['preview_url']) print('cover art: ' + track['album']['images'][0]['url']) print() ``` -------------------------------- ### Get User's Saved Tracks (User Authentication) Source: https://github.com/spotipy-dev/spotipy/blob/master/README.md Retrieves a list of tracks saved by the current user. Requires user authentication with specified scopes and a redirect URI. ```python import spotipy from spotipy.oauth2 import SpotifyOAuth sp = spotipy.Spotify(auth_manager=SpotifyOAuth(client_id="YOUR_APP_CLIENT_ID", client_secret="YOUR_APP_CLIENT_SECRET", redirect_uri="YOUR_APP_REDIRECT_URI", scope="user-library-read")) results = sp.current_user_saved_tracks() for idx, item in enumerate(results['items']): track = item['track'] print(idx, track['artists'][0]['name'], " – ", track['name']) ``` -------------------------------- ### Create Directory Source: https://github.com/spotipy-dev/spotipy/blob/master/TUTORIAL.md Create a new folder for your Spotipy application code using the mkdir command. ```bash mkdir folder_name ``` -------------------------------- ### Package Spotipy for PyPI Source: https://github.com/spotipy-dev/spotipy/blob/master/docs/index.md Build source and wheel distributions for Spotipy and check them using Twine. Upload the distributions to the PyPI legacy repository, skipping existing files. ```bash python setup.py sdist bdist_wheel python3 setup.py sdist bdist_wheel twine check dist/.* twine upload –repository-url https://upload.pypi.org/legacy/ –skip-existing dist/ *.(whl|gz|zip)~dist/*linux*.whl ``` -------------------------------- ### Export Environment Variables for Spotipy (Linux/Mac) Source: https://github.com/spotipy-dev/spotipy/blob/master/CONTRIBUTING.md Set the necessary environment variables for client ID, client secret, username, and redirect URI on Linux or Mac systems. ```bash # Linux or Mac export SPOTIPY_CLIENT_ID=client_id_here export SPOTIPY_CLIENT_SECRET=client_secret_here export SPOTIPY_CLIENT_USERNAME=client_username_here # This is actually an id not spotify display name and can be found [here](https://www.spotify.com/us/account/overview/) export SPOTIPY_REDIRECT_URI=http://127.0.0.1:8080 # Make url is set in app you created to get your ID and SECRET ``` -------------------------------- ### Prepare Changelog Entry for Release Source: https://github.com/spotipy-dev/spotipy/blob/master/CONTRIBUTING.md Format the changelog for an upcoming release, including sections for added, fixed, and removed changes. ```markdown ## Unreleased Add your changes below. ### Added ### Fixed ### Removed ``` -------------------------------- ### Export Environment Variables for Spotipy (Windows) Source: https://github.com/spotipy-dev/spotipy/blob/master/CONTRIBUTING.md Set the necessary environment variables for client ID, client secret, username, and redirect URI on Windows systems. ```powershell # Windows $env:SPOTIPY_CLIENT_ID="client_id_here" $env:SPOTIPY_CLIENT_SECRET="client_secret_here" $env:SPOTIPY_CLIENT_USERNAME="client_username_here" $env:SPOTIPY_REDIRECT_URI="http://127.0.0.1:8080" ``` -------------------------------- ### Request New Token with Show Dialog Source: https://github.com/spotipy-dev/spotipy/blob/master/FAQ.md Use `show_dialog=True` when requesting a new token to ensure the correct user account is used, especially when encountering playlist ownership errors. ```python sp = spotipy.Spotify(auth_manager=SpotifyOAuth(show_dialog=True)) ``` -------------------------------- ### Export Environment Variables for Spotipy Source: https://github.com/spotipy-dev/spotipy/blob/master/docs/index.md Set the necessary environment variables before running Spotipy applications. Ensure the redirect URI is configured in your Spotify app. ```bash export SPOTIPY_CLIENT_ID=client_id_here export SPOTIPY_CLIENT_SECRET=client_secret_here export SPOTIPY_CLIENT_USERNAME=client_username_here # This is actually an id not spotify display name export SPOTIPY_REDIRECT_URI=http://127.0.0.1:8080 # Make url is set in app you created to get your ID and SECRET ``` -------------------------------- ### Run Python Application Source: https://github.com/spotipy-dev/spotipy/blob/master/TUTORIAL.md Execute your Python script from the terminal. Ensure you are in the same directory as your main.py file. ```bash python main.py ``` -------------------------------- ### Create Python File with Vim Source: https://github.com/spotipy-dev/spotipy/blob/master/TUTORIAL.md Create a new Python file named main.py using the Vim text editor in the terminal. ```bash vim main.py ``` -------------------------------- ### Check Import List Correctness with isort Source: https://github.com/spotipy-dev/spotipy/blob/master/CONTRIBUTING.md Verify that import lists are stored correctly using isort. ```bash isort . -c ``` -------------------------------- ### Set Environment Variables for Client Credentials Source: https://github.com/spotipy-dev/spotipy/blob/master/docs/index.md Set the SPOTIPY_CLIENT_ID and SPOTIPY_CLIENT_SECRET environment variables to authenticate using the Client Credentials Flow. This is required for server-to-server authentication. ```bash export SPOTIPY_CLIENT_ID='your-spotify-client-id' export SPOTIPY_CLIENT_SECRET='your-spotify-client-secret' ``` -------------------------------- ### Execute with Python 3 Source: https://github.com/spotipy-dev/spotipy/blob/master/TUTORIAL.md Run your Python script using the python3 command, often necessary on systems where 'python' defaults to an older version. ```bash python3 main.py ``` -------------------------------- ### Set Environment Variables for Credentials Source: https://github.com/spotipy-dev/spotipy/blob/master/docs/index.md Set environment variables for Spotify API client ID, client secret, and redirect URI. This is an alternative to hardcoding credentials in your source code. ```bash export SPOTIPY_CLIENT_ID='your-spotify-client-id' export SPOTIPY_CLIENT_SECRET='your-spotify-client-secret' export SPOTIPY_REDIRECT_URI='your-app-redirect-url' ``` -------------------------------- ### Fetch and Print Artist Albums Source: https://github.com/spotipy-dev/spotipy/blob/master/TUTORIAL.md Retrieve all album names for a given artist URI from Spotify and print them to the console. Handles pagination to ensure all albums are fetched. ```python results = sp.artist_albums(taylor_uri, album_type='album') albums = results['items'] while results['next']: results = sp.next(results) albums.extend(results['items']) for album in albums: print(album['name']) ``` -------------------------------- ### List Albums by Artist Source: https://github.com/spotipy-dev/spotipy/blob/master/docs/index.md Fetches and prints the names of all albums released by a specific artist using their Spotify URI. It handles pagination to retrieve all album results. ```python import spotipy from spotipy.oauth2 import SpotifyClientCredentials birdy_uri = 'spotify:artist:2WX2uTcsvV5OnS0inACecP' spotify = spotipy.Spotify(client_credentials_manager=SpotifyClientCredentials()) results = spotify.artist_albums(birdy_uri, album_type='album') albums = results['items'] while results['next']: results = spotify.next(results) albums.extend(results['items']) for album in albums: print(album['name']) ``` -------------------------------- ### Verify Code Style with Flake8 Source: https://github.com/spotipy-dev/spotipy/blob/master/CONTRIBUTING.md Check the code style using Flake8 to ensure compliance with project standards. ```bash flake8 . ``` -------------------------------- ### Disable Browser for Authorization Source: https://github.com/spotipy-dev/spotipy/blob/master/FAQ.md Set `open_browser=False` when instantiating `SpotifyOAuth` or `SpotifyPKCE` to handle authorization in headless or browserless environments. You will need to manually open the provided authorization URI. ```python sp = spotipy.Spotify(auth_manager=SpotifyOAuth(open_browser=False)) ``` -------------------------------- ### Automatically Fix Code Style Source: https://github.com/spotipy-dev/spotipy/blob/master/CONTRIBUTING.md Use autopep8 to automatically format the code according to style guidelines. ```bash autopep8 --in-place --aggressive --recursive . ``` -------------------------------- ### Find Artist Image URL by Name Source: https://github.com/spotipy-dev/spotipy/blob/master/docs/index.md Searches for an artist by name and prints the artist's name along with the URL of their primary image. Defaults to 'Radiohead' if no name is provided via command-line arguments. ```python import spotipy import sys from spotipy.oauth2 import SpotifyClientCredentials spotify = spotipy.Spotify(auth_manager=SpotifyClientCredentials()) if len(sys.argv) > 1: name = ' '.join(sys.argv[1:]) else: name = 'Radiohead' results = spotify.search(q='artist:' + name, type='artist') items = results['artists']['items'] if len(items) > 0: artist = items[0] print(artist['name'], artist['images'][0]['url']) ``` -------------------------------- ### Automatically Sort Imports with isort Source: https://github.com/spotipy-dev/spotipy/blob/master/CONTRIBUTING.md Automatically sort and organize import statements using isort. ```bash isort . ``` -------------------------------- ### Disable Retries for Spotify API Requests Source: https://github.com/spotipy-dev/spotipy/blob/master/FAQ.md Pass `retries=0` to the `Spotify` constructor to disable the backoff-retry strategy and receive `SpotifyException` errors immediately when rate limits are hit, instead of waiting. ```python sp = spotipy.Spotify( retries=0, ... ) ``` -------------------------------- ### Search for Tracks (No Authentication) Source: https://github.com/spotipy-dev/spotipy/blob/master/README.md Searches for tracks using the Spotify API without requiring user authentication. Requires client ID and client secret. ```python import spotipy from spotipy.oauth2 import SpotifyClientCredentials sp = spotipy.Spotify(auth_manager=SpotifyClientCredentials(client_id="YOUR_APP_CLIENT_ID", client_secret="YOUR_APP_CLIENT_SECRET")) results = sp.search(q='weezer', limit=20) for idx, track in enumerate(results['tracks']['items']): print(idx, track['name']) ``` -------------------------------- ### Define Artist URI Source: https://github.com/spotipy-dev/spotipy/blob/master/TUTORIAL.md Assign the Spotify URI for an artist to a variable. This is used to identify the artist in subsequent API calls. ```python taylor_uri = 'spotify:artist:06HL4z0CvFAxyc27GXpf02' ``` -------------------------------- ### Search Tracks in Specific Market Source: https://github.com/spotipy-dev/spotipy/blob/master/FAQ.md Specify the `market` parameter in the `search` function to find tracks in a particular country, as the default is the US market. ```python search("abba", market="DE") ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.