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,6 +1,19 @@
SPOTIFY_KEY = ""
SPOTIFY_SECRET = ""
SPOTIFY_USER_ID = ""
import os
import spotipy
from spotipy.oauth2 import SpotifyOAuth
from dotenv import load_dotenv
# load .env file
load_dotenv()
SPOTIFY_CLIENT_ID = os.getenv('SPOTIFY_CLIENT_ID')
SPOTIFY_CLIENT_SECRET = os.getenv('SPOTIFY_CLIENT_SECRET')
SPOTIFY_REDIRECT_URI = os.getenv('SPOTIFY_REDIRECT_URI')
SPOTIFY_USER_ID = os.getenv('SPOTIFY_USER_ID')
if __name__ == "__main__":
if not SPOTIFY_CLIENT_ID or not SPOTIFY_CLIENT_SECRET or not SPOTIFY_REDIRECT_URI:
raise ValueError("Please provide the required information in the .env file.")
VERBOSE = True
@ -18,9 +31,9 @@ def remove_duplicates(input_list: list) -> list:
return list(set(input_list))
def spotifyauth() -> spotipy.Spotify:
return spotipy.Spotify(auth_manager=SpotifyOAuth(client_id=SPOTIFY_KEY,
client_secret=SPOTIFY_SECRET,
redirect_uri="http://localhost:6969",
return spotipy.Spotify(auth_manager=SpotifyOAuth(client_id=SPOTIFY_CLIENT_ID,
client_secret=SPOTIFY_CLIENT_SECRET,
redirect_uri=SPOTIFY_REDIRECT_URI,
scope="playlist-modify-public playlist-modify-private"))
def getDiscographyArtist(sp: spotipy.Spotify, first = False):

View file

@ -1,13 +1,21 @@
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
@ -19,7 +27,7 @@ def get_last_track_of_playlist(playlist_id):
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

View file

@ -3,14 +3,22 @@ from datetime import datetime
import json
import urllib.parse
import time
import os
from dotenv import load_dotenv
# load .env file
load_dotenv()
starttime = time.time()
API_KEY = ""
USERNAME = ""
LASTFM_API_KEY = os.getenv('LASTFM_API_KEY')
LASTFM_USERNAME = os.getenv('LASTFM_USERNAME')
if not LASTFM_API_KEY or not LASTFM_USERNAME:
raise ValueError("Please provide the required information in the .env file.")
# Get the total number of scrobbles for the user
url = f"http://ws.audioscrobbler.com/2.0/?method=user.getinfo&user={USERNAME}&api_key={API_KEY}&format=json"
url = f"http://ws.audioscrobbler.com/2.0/?method=user.getinfo&user={LASTFM_USERNAME}&api_key={LASTFM_API_KEY}&format=json"
response = requests.get(url)
data = response.json()
total_scrobbles = int(data['user']['playcount'])
@ -21,7 +29,7 @@ page_count = (total_scrobbles + 200 - 1) // 200
# Fetch all scrobbles by paginating through the API responses
all_scrobbles = []
for page in range(1, page_count + 1):
url = f"http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user={USERNAME}&api_key={API_KEY}&format=json&page={page}"
url = f"http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user={LASTFM_USERNAME}&api_key={LASTFM_API_KEY}&format=json&page={page}"
response = requests.get(url)
data = response.json()
tracks = data['recenttracks']['track']
@ -30,7 +38,7 @@ for page in range(1, page_count + 1):
artist = urllib.parse.quote(track['artist']['#text'])
song = urllib.parse.quote(track['name'])
track_url = f"http://ws.audioscrobbler.com/2.0/?method=track.getInfo&api_key={API_KEY}&artist={artist}&track={song}&format=json"
track_url = f"http://ws.audioscrobbler.com/2.0/?method=track.getInfo&api_key={LASTFM_API_KEY}&artist={artist}&track={song}&format=json"
#print(track_url)
track_response = requests.get(track_url)
track_data = track_response.json()

View file

@ -1,13 +1,23 @@
LASTFM_KEY = ""
LASTFM_SECRET = ""
SPOTIFY_KEY = ""
SPOTIFY_SECRET = ""
import pylast, spotipy, sys, os, time
from spotipy.oauth2 import SpotifyOAuth
from dotenv import load_dotenv
# load .env file
load_dotenv()
# Define your Last.fm API credentials
LASTFM_API_KEY = os.getenv(' LASTFM_API_KEY')
LASTFM_API_SECRET = os.getenv('LASTFM_API_SECRET')
LASTFM_USERNAME = os.getenv('LASTFM_USERNAME')
LASTFM_PASSWORD_HASH = os.getenv('LASTFM_PASSWORD_HASH')
# Define your Spotify API credentials
SPOTIPY_CLIENT_ID = os.getenv('SPOTIPY_CLIENT_ID')
SPOTIPY_CLIENT_SECRET = os.getenv('SPOTIPY_CLIENT_SECRET')
SPOTIPY_REDIRECT_URI = os.getenv('SPOTIPY_REDIRECT_URI')
VERBOSE = True
import pylast, os, spotipy
from spotipy.oauth2 import SpotifyOAuth
def verboseprint(message):
if VERBOSE:
print(message)
@ -15,7 +25,7 @@ def verboseprint(message):
#last.fm auth
def lastfmauth():
SESSION_KEY_FILE = os.path.join(os.path.expanduser("~"), ".session_key")
network = pylast.LastFMNetwork(LASTFM_KEY, LASTFM_SECRET)
network = pylast.LastFMNetwork(LASTFM_API_KEY, LASTFM_API_SECRET)
if not os.path.exists(SESSION_KEY_FILE):
skg = pylast.SessionKeyGenerator(network)
url = skg.get_web_auth_url()
@ -42,9 +52,9 @@ def lastfmauth():
#spotify
def spotifyauth():
return spotipy.Spotify(auth_manager=SpotifyOAuth(client_id=SPOTIFY_KEY,
client_secret=SPOTIFY_SECRET,
redirect_uri="http://localhost:42010",
return spotipy.Spotify(auth_manager=SpotifyOAuth(client_id=SPOTIPY_CLIENT_ID,
client_secret=SPOTIPY_CLIENT_SECRET,
redirect_uri=SPOTIPY_REDIRECT_URI,
scope="user-library-read"))
def show_tracks(results):
tracknr = results['offset']
@ -58,7 +68,7 @@ def show_tracks(results):
track.love()
except pylast.NetworkError:
try:
sleep(1)
time.sleep(1)
track = network.get_track(track['artists'][0]['name'], track['name'])
track.love()
except pylast.NetworkError:

View file

@ -1,19 +1,23 @@
import pylast, spotipy, sys, os, time
from spotipy.oauth2 import SpotifyOAuth
from dotenv import load_dotenv
# load .env file
load_dotenv()
# Define your Last.fm API credentials
LASTFM_API_KEY = ""
LASTFM_API_SECRET = ""
LASTFM_USERNAME = "Jonas_Jones"
LASTFM_PASSWORD_HASH = pylast.md5("")
LASTFM_API_KEY = os.getenv(' LASTFM_API_KEY')
LASTFM_API_SECRET = os.getenv('LASTFM_API_SECRET')
LASTFM_USERNAME = os.getenv('LASTFM_USERNAME')
LASTFM_PASSWORD_HASH = os.getenv('LASTFM_PASSWORD_HASH')
# Define your Spotify API credentials
SPOTIPY_CLIENT_ID = ""
SPOTIPY_CLIENT_SECRET = ""
SPOTIPY_REDIRECT_URI = ""
SPOTIPY_CLIENT_ID = os.getenv('SPOTIPY_CLIENT_ID')
SPOTIPY_CLIENT_SECRET = os.getenv('SPOTIPY_CLIENT_SECRET')
SPOTIPY_REDIRECT_URI = os.getenv('SPOTIPY_REDIRECT_URI')
# Define your playlist IDs
LIKEDSONGPLAYLIST_ID = ""
LIKEDSONGPLAYLIST_ID = os.getenv('LIKEDSONGPLAYLIST_ID')
def progress_bar(current, total, last_time_stamp=time.time(), etastr=None):
current = total if current > total else current