Changed to use configparser. Now we have a new function for reading the config file and checks if the values have been set, if not asks the user to change the default values in the config file. Moved username from being defined, not used and returned in getUsernameAndLinks to be only be defined and used in addSongToPlaylist.

This commit is contained in:
2018-03-30 21:58:35 -04:00
parent 467532965d
commit 0f61658448

View File

@@ -1,20 +1,38 @@
#!/usr/bin/env python3.6
import pprint
import sys
import spotipy
import os
import config as cnf
import configparser
import spotipy.util as util
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
def getUsernameAndLinks():
username = cnf.username
def getConfig():
"""
Read path and get configuartion file with spotify settings
:return: config settings read from 'config.ini'
:rtype: configparser.ConfigParser
"""
config = configparser.ConfigParser()
config_dir = os.path.join(BASE_DIR, 'config.ini')
config.read(config_dir)
config_values = list(dict(config.items('Spotify')).values())
if any(value.startswith('YOUR') for value in config_values):
print('Please set variables in config.ini file.')
exit(0)
return config
def getLinks():
if len(sys.argv) > 1:
trackLinks = sys.argv[1:]
else:
print("Usage: %s track_id ..." % (sys.argv[0],))
sys.exit()
return username, trackLinks
return trackLinks
def checkAndConvertLink(trackLinks):
linkString = trackLinks[0]
@@ -34,20 +52,21 @@ def checkAndConvertLink(trackLinks):
return trackLinks
def addSongToPlaylist():
username, trackLinks = getUsernameAndLinks()
config = getConfig()
trackLinks = getUsernameAndLinks()
trackLinks = checkAndConvertLink(trackLinks)
username = config['USERNAME']
#This gets the authentication needed to add song to the user's playlist
scope = 'playlist-modify-public'
token = util.prompt_for_user_token(username, scope, client_id=cnf.clientID, client_secret=cnf.clientSecret, redirect_uri = cnf.redirectUri)
token = util.prompt_for_user_token(username, scope, client_id=config['CLIENT_ID'], client_secret=config['CLIENT_SECRET'], redirect_uri = config['REDIRECT_URI'])
if token:
sp = spotipy.Spotify(token)
sp.trace = False
sp.trace_out = False
#Append to spotify playlist
results = sp.user_playlist_add_tracks(cnf.username, cnf.playlistID, trackLinks)
results = sp.user_playlist_add_tracks(username, config['PLAYLIST_ID'], trackLinks)
print(results)
else:
print("Can't get token for", username)