mirror of
https://github.com/JonasunderscoreJones/turbo-octo-potato.git
synced 2025-10-25 19:19:19 +02:00
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:
parent
446430ec1d
commit
6d2ece92ee
5 changed files with 89 additions and 46 deletions
|
|
@ -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):
|
||||
|
|
@ -31,7 +44,7 @@ def getDiscographyArtist(sp: spotipy.Spotify, first = False):
|
|||
print("======================")
|
||||
user_artist_input = input("Input your next Artist (name or Spotify ID). Leave empty if all have been inputed\n")
|
||||
if user_artist_input == "": return
|
||||
|
||||
|
||||
print("Looking up Artist...")
|
||||
try:
|
||||
if len(user_artist_input) == 22:
|
||||
|
|
@ -70,7 +83,7 @@ def getArtistAlbum(sp: spotipy.Spotify, artists: list[str]) -> list[(str, str, s
|
|||
exit()
|
||||
|
||||
albums.append(this_artist_albums)
|
||||
|
||||
|
||||
except TimeoutError:
|
||||
print("\nNetwork unreachable. THIS IS NOT RECOVERABLE. Please restart the process")
|
||||
exit()
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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,16 +29,16 @@ 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']
|
||||
|
||||
|
||||
for track in tracks:
|
||||
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()
|
||||
|
|
|
|||
|
|
@ -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:
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
@ -46,7 +50,7 @@ def handle_playlist_part_return(playlist_part, all_songs):
|
|||
track_name = item["track"]["name"]
|
||||
artist_name = item["track"]["artists"][0]["name"]
|
||||
all_songs.append((track_uri, track_name, artist_name))
|
||||
|
||||
|
||||
return all_songs
|
||||
|
||||
def get_all_songs_from_playlist(playlist_id):
|
||||
|
|
@ -61,7 +65,7 @@ def get_all_songs_from_playlist(playlist_id):
|
|||
if not playlist_part["items"]:
|
||||
break
|
||||
all_songs = handle_playlist_part_return(playlist_part, all_songs)
|
||||
|
||||
|
||||
progress_print, last_time_stamp = progress_bar(offset+limit, playlist_part["total"], last_time_stamp)
|
||||
verboseprint(progress_print, end="\r")
|
||||
|
||||
|
|
@ -82,7 +86,7 @@ def get_all_liked_songs():
|
|||
if not liked_songs_chunk["items"]:
|
||||
break
|
||||
all_liked_songs = handle_playlist_part_return(liked_songs_chunk, all_liked_songs)
|
||||
|
||||
|
||||
progress_print, last_time_stamp = progress_bar(offset+limit, liked_songs_chunk["total"], last_time_stamp)
|
||||
verboseprint(progress_print, end="\r")
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue