update auth.py to use a auth.json file instead, with functional default file

This commit is contained in:
Xevion
2019-10-27 22:29:56 -05:00
parent 92da3e4355
commit bb7d4b3cb9
4 changed files with 40 additions and 9 deletions

2
.gitignore vendored
View File

@@ -2,7 +2,7 @@ __pycache__/**
tracks/** tracks/**
cache_xevioni/** cache_xevioni/**
.cache-* .cache-*
auth.py auth.json
export/** export/**
# Byte-compiled / optimized / DLL files # Byte-compiled / optimized / DLL files

32
auth.py Normal file
View File

@@ -0,0 +1,32 @@
import logging, sys, os, json
# Open the file and get keys
PATH = os.path.join(sys.path[0], 'auth.json')
# Ensure the file exists, if not, generate one and error with a reason
if not os.path.exists(PATH):
with open(PATH, 'w') as file:
# Dump a pretty-printed dictionary with default values
json.dump(
{
'USERNAME' : 'Your Username Here',
'CLIENT_ID' : 'Your Client ID Here',
'CLIENT_SECRET' : 'Your Client Secret Here',
'REDIRECT_URI' : 'Your Redirect URI Callback Here',
'SCOPE' : ['Your Scopes Here']
},
file,
indent=3
)
# Error critically, then exit
logging.critical("No \'auth.json\' file detected, one has been created for you")
logging.critical("Please fill out with your Spotify credentials, and then restart the program")
sys.exit()
FILE = json.load(open(PATH, 'r'))
# Load all configuration variables
USERNAME = FILE['USERNAME']
CLIENT_ID = FILE['CLIENT_ID']
CLIENT_SECRET = FILE['CLIENT_SECRET']
REDIRECT_URI = FILE['REDIRECT_URI']
SCOPE = ' '.join(FILE['SCOPE'])

View File

@@ -13,14 +13,13 @@ def main():
refresh() refresh()
process.main() process.main()
# Refreshes tracks from files if the token from Spotipy has expired, # Refreshes tracks from files if the token from Spotipy has expired,
# thus keeping us up to date in most cases while keeping rate limits # thus keeping us up to date in most cases while keeping rate limits
def refresh(): def refresh():
file_path = os.path.join(sys.path[0], f'.cache-{auth.username}') file_path = os.path.join(sys.path[0], f'.cache-{auth.USERNAME}')
if os.path.exists(file_path): if os.path.exists(file_path):
cache = json.load(open(file_path, 'r')) cache = json.load(open(file_path, 'r'))
if time.time() > cache['expires_at']: if True or time.time() > cache['expires_at']:
logging.info('Refreshing Spotify data by pulling tracks, this may take a moment.') logging.info('Refreshing Spotify data by pulling tracks, this may take a moment.')
pull.main() pull.main()
else: else:

10
pull.py
View File

@@ -15,11 +15,11 @@ def main():
logging.info('Authorizing with Spotify via Spotipy') logging.info('Authorizing with Spotify via Spotipy')
logging.warning('May require User Interaction to authenticate properly!') logging.warning('May require User Interaction to authenticate properly!')
token = util.prompt_for_user_token( token = util.prompt_for_user_token(
username=auth.username, username=auth.USERNAME,
scope=auth.scope, scope=auth.SCOPE,
client_id=auth.client_id, client_id=auth.CLIENT_ID,
client_secret=auth.client_secret, client_secret=auth.CLIENT_SECRET,
redirect_uri=auth.redirect_uri redirect_uri=auth.REDIRECT_URI
) )
sp = spotipy.Spotify(auth=token) sp = spotipy.Spotify(auth=token)
logging.info('Authorized with Spotify via Spotipy') logging.info('Authorized with Spotify via Spotipy')