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 pprint
import sys import sys
import spotipy import spotipy
import os import os
import config as cnf import configparser
import spotipy.util as util import spotipy.util as util
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
def getUsernameAndLinks(): def getConfig():
username = cnf.username """
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: if len(sys.argv) > 1:
trackLinks = sys.argv[1:] trackLinks = sys.argv[1:]
else: else:
print("Usage: %s track_id ..." % (sys.argv[0],)) print("Usage: %s track_id ..." % (sys.argv[0],))
sys.exit() sys.exit()
return username, trackLinks return trackLinks
def checkAndConvertLink(trackLinks): def checkAndConvertLink(trackLinks):
linkString = trackLinks[0] linkString = trackLinks[0]
@@ -34,20 +52,21 @@ def checkAndConvertLink(trackLinks):
return trackLinks return trackLinks
def addSongToPlaylist(): def addSongToPlaylist():
config = getConfig()
username, trackLinks = getUsernameAndLinks() trackLinks = getUsernameAndLinks()
trackLinks = checkAndConvertLink(trackLinks) trackLinks = checkAndConvertLink(trackLinks)
username = config['USERNAME']
#This gets the authentication needed to add song to the user's playlist #This gets the authentication needed to add song to the user's playlist
scope = 'playlist-modify-public' 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: if token:
sp = spotipy.Spotify(token) sp = spotipy.Spotify(token)
sp.trace = False sp.trace = False
sp.trace_out = False sp.trace_out = False
#Append to spotify playlist #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) print(results)
else: else:
print("Can't get token for", username) print("Can't get token for", username)