From 0f616584488289164fc2a49b03ddd633694ae7a8 Mon Sep 17 00:00:00 2001 From: KevinMidboe Date: Fri, 30 Mar 2018 21:58:35 -0400 Subject: [PATCH] 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. --- spotify.py | 35 +++++++++++++++++++++++++++-------- 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/spotify.py b/spotify.py index 7fece09..be29594 100644 --- a/spotify.py +++ b/spotify.py @@ -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)