Added .env credential storage

Moved tokens and credentials to the .env file to not store them in the python file itself anymore.

- Added .env credential storage
- Fixed deadspaces
This commit is contained in:
J-onasJones 2023-09-25 16:28:53 +02:00
parent 446430ec1d
commit 6d2ece92ee
5 changed files with 89 additions and 46 deletions

View file

@ -1,25 +1,33 @@
import spotipy
import spotipy, os
from spotipy.oauth2 import SpotifyOAuth
from dotenv import load_dotenv
# load .env file
load_dotenv()
# Set up your Spotify API credentials
client_id = "YOUR_CLIENT_ID"
client_secret = "YOUR_CLIENT_SECRET"
redirect_uri = "YOUR_REDIRECT_URI"
SPOTIFY_CLIENT_ID = os.getenv('SPOTIFY_CLIENT_ID')
SPOTIFY_CLIENT_SECRET = os.getenv('SPOTIFY_CLIENT_SECRET')
SPOTIFY_REDIRECT_URI = os.getenv('SPOTIFY_REDIRECT_URI')
PLAYLIST_ID = os.getenv('SOMEPLAYLIST_ID')
if not SPOTIFY_CLIENT_ID or not SPOTIFY_CLIENT_SECRET or not SPOTIFY_REDIRECT_URI or not PLAYLIST_ID:
raise ValueError("Please provide the required information in the .env file.")
# Create a Spotipy instance with authentication
sp = spotipy.Spotify(auth_manager=SpotifyOAuth(client_id=client_id, client_secret=client_secret, redirect_uri=redirect_uri, scope="playlist-read-private"))
sp = spotipy.Spotify(auth_manager=SpotifyOAuth(client_id=SPOTIFY_CLIENT_ID, client_secret=SPOTIFY_CLIENT_SECRET, redirect_uri=SPOTIFY_REDIRECT_URI, scope="playlist-read-private"))
def get_last_track_of_playlist(playlist_id):
# Get the playlist's tracks
playlist = sp.playlist_tracks(playlist_id)
# Extract the last track
last_track = playlist["items"][-1]["track"]
return last_track
# Replace "YOUR_PLAYLIST_ID" with the actual playlist ID
playlist_id = "YOUR_PLAYLIST_ID"
playlist_id = PLAYLIST_ID
last_track = get_last_track_of_playlist(playlist_id)
# Access information about the last track