""" This module contains a simple function to connect to the spotify API without having to load your credentials within your script or notebook. ???+ warning "Pre-requisites" To use this module, the following variables must be defined in your .env file: `spotifyClientId`, `spotifyClientSecret` + `spotifyUsername` if you want to use the endpoints that need authentication. Or alternatively, you need to pass your ID and Secret as arguments to the `connect_spotify` function. See the [.env file configuration][configure-your-env-file] section if needed It uses the [spotipy](https://spotipy.readthedocs.io/en/latest/) package under the hood, so you can use all of this package features. You can use the `sp` object as your spotipy object and access the spotify API from there. ???+ example === "To use the public endpoints" ``` from djagitit.api.spotify import connect_spotify sp = connect_spotify() #Or if you want to specify your credentials manually: sp = connect_spotify(CLIENT_ID='YOUR_CLIENT_ID', CLIENT_SECRET='YOUR_CLIENT_SECRET') #To get data on a masterpiece in history of music: sp.album('1cCAb1vN8uUsdfEylVmTLs') ``` === "To use endpoints with specific scopes that require authentication" By default, the function uses `https://play.spotify.com/` as the redirect URL. You can either add it to your app redirect urls or use a different one by passing the `REDIRECT_URL` argument (see 2nd example). ``` from djagitit.api.spotify import connect_spotify sp = connect_spotify(SCOPE='user-library-read') #Or if you want to specify your credentials manually: sp = connect_spotify(CLIENT_ID='YOUR_CLIENT_ID', CLIENT_SECRET='YOUR_CLIENT_SECRET', SCOPE='user-library-read', USERNAME='YOUR_USERNAME', REDIRECT_URL='YOUR_REDIRECT_URL' ) #To get the last 10 tracks played by the current user: sp.current_user_recently_played(limit=10) ``` """ from dotenv import load_dotenv import os import pathlib import spotipy from spotipy.oauth2 import SpotifyClientCredentials from spotipy.oauth2 import CacheFileHandler from spotipy.oauth2 import SpotifyOAuth load_dotenv() def __safely_getenv(var): """Checks if environment variable is defined before returning it""" if not os.getenv(var): raise EnvironmentError(f'Environment variable {var} not defined!') return os.getenv(var) def connect_spotify(CLIENT_ID=None, CLIENT_SECRET=None, USERNAME=None, SCOPE=None, REDIRECT_URL='https://play.spotify.com/'): if not CLIENT_ID: CLIENT_ID = __safely_getenv('spotifyClientId') if not CLIENT_SECRET: CLIENT_SECRET = __safely_getenv('spotifyClientSecret') if not SCOPE: client_credentials_manager = SpotifyClientCredentials(client_id=CLIENT_ID, client_secret=CLIENT_SECRET) sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager) else: if not USERNAME: USERNAME = os.getenv('spotifyUsername') if not USERNAME: raise Exception("Error: USERNAME is required when SCOPE is specified OR fill the spotifyUsername variable in your .env file.") spotifyTokenPath = pathlib.Path(__safely_getenv('spotifyTokenDirPath')) cacheHandler = CacheFileHandler(cache_path=spotifyTokenPath.joinpath(f'.spotify_{USERNAME}_{SCOPE}.cache')) auth_manager = SpotifyOAuth(client_id=CLIENT_ID, client_secret=CLIENT_SECRET, redirect_uri=REDIRECT_URL, scope=SCOPE, cache_handler=cacheHandler) sp = spotipy.Spotify(auth_manager=auth_manager) sp.trace = False return sp