From fc9582805b2b7dc99b3d1a1c53e700356662374e Mon Sep 17 00:00:00 2001 From: Adrian Date: Fri, 30 Mar 2018 13:11:26 +0200 Subject: [PATCH] Revert "Can now add songs to playlist, with any spotify URL, from desktop app or browser. Need to wait for facebook to start working on the chat-bot" This reverts commit 86fea244dd902791b18c63cb5756cbe388ff2ddb. --- .cache-ribe1912 | 1 - config.py | 8 -------- spotify.py | 38 +++++++++++++++++++++----------------- test.py | 30 ++++++++++++++++++++++++++++++ 4 files changed, 51 insertions(+), 26 deletions(-) delete mode 100644 .cache-ribe1912 delete mode 100644 config.py create mode 100644 test.py diff --git a/.cache-ribe1912 b/.cache-ribe1912 deleted file mode 100644 index a946310..0000000 --- a/.cache-ribe1912 +++ /dev/null @@ -1 +0,0 @@ -{"access_token": "BQDD6iS0idy9_aS8ckrfEp9NbWbVI5udyc2E4V7LUUPMG60kvk5Pry13QsHmOMOKv5O5D7XGT6UeGs25-DqtM4LgFkgztCay2p_n9C6FvDrJYmvgBMivJQOpUWK1TiNRl4vbGKazb7_n_vTmuLfs8QqUtshwFuuIO4MrL2vYkcbupRI", "expires_in": 3600, "expires_at": 1522411741, "token_type": "Bearer", "scope": "playlist-modify-public", "refresh_token": "AQCCEkzHmh5Vdbn1a6Y1ZV_0_uXApmRYHNk7zrLaC6sZxernecjXe-v2U3aqoFoE-cZssf-UxL9xpGDsubptEef29ig0ZmxVJT3nmEWau14jKFj8DW0NbjcVGVdBKSXg7Uo"} \ No newline at end of file diff --git a/config.py b/config.py deleted file mode 100644 index 0ad623c..0000000 --- a/config.py +++ /dev/null @@ -1,8 +0,0 @@ -import os -username = 'ribe1912' -clientID = '9f8caa1d3bb44187854c5282f94f83b2' -#damn os-function doesn't work -clientSecret = os.environ.get('client_secret') -clientSecret = 'fa0dd2c91ca045dbb53e158e49f1aa3d' -redirectUri = 'https://blooming-refuge-48604.herokuapp.com/' -playlistID = 'https://open.spotify.com/user/ribe1912/playlist/0mTtRpipjSnsq3Rgd12CQu' diff --git a/spotify.py b/spotify.py index 7fece09..7989dd6 100644 --- a/spotify.py +++ b/spotify.py @@ -2,52 +2,56 @@ import pprint import sys import spotipy import os -import config as cnf import spotipy.util as util +def getKeysAndPlaylist(): + + clientID = '9f8caa1d3bb44187854c5282f94f83b2' + clientSecret = os.environ.get('client_secret') + print("HHEHEHEHEHE") + print(clientSecret) + redirectUri = 'https://blooming-refuge-48604.herokuapp.com/' + playlistID = 'https://open.spotify.com/user/ribe1912/playlist/0mTtRpipjSnsq3Rgd12CQu' + + return clientID, clientSecret, redirectUri, playlistID def getUsernameAndLinks(): - username = cnf.username - if len(sys.argv) > 1: - trackLinks = sys.argv[1:] + if len(sys.argv) > 2: + username = sys.argv[1] + trackLinks = sys.argv[2:] else: - print("Usage: %s track_id ..." % (sys.argv[0],)) + print("Usage: %s username playlist_id track_id ..." % (sys.argv[0],)) sys.exit() - return username, trackLinks def checkAndConvertLink(trackLinks): linkString = trackLinks[0] - #Just getting the base-62 number out of the spotify link so that links from browser, desktop and app can be used - trackStringIndex = linkString.find('track/') + #'?' + str is added to the hyperlink when copying songs from the app / desktop. Remove this to run in webbrowser questionMarkIndex = linkString.find('?') - print(trackStringIndex) - print(questionMarkIndex) if questionMarkIndex != -1: - print("Getting the base-62 numbers out of hyperlink") - trackLinks = [linkString[trackStringIndex + len('track/'): questionMarkIndex]] #Only supports adding one song at a time so far. + trackLinks = [linkString[0:questionMarkIndex]] #Only supports adding one song at a time so far. else: - print("Link format is already in base-62 form") pass - return trackLinks def addSongToPlaylist(): + #Get keys which allows to connect to the Spotify app + clientID, clientSecret, redirectUri, playlistID = getKeysAndPlaylist() username, trackLinks = getUsernameAndLinks() - trackLinks = checkAndConvertLink(trackLinks) + trackLink = checkAndConvertLink(trackLinks) #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=clientID, client_secret=clientSecret, redirect_uri = redirectUri) 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, playlistID, trackLinks) print(results) else: print("Can't get token for", username) diff --git a/test.py b/test.py new file mode 100644 index 0000000..15c3cca --- /dev/null +++ b/test.py @@ -0,0 +1,30 @@ +# Creates a playlist for a user + +import pprint +import sys +import os +import subprocess + +import spotipy +import spotipy.util as util + + +if len(sys.argv) > 2: + username = sys.argv[1] + playlist_name = sys.argv[2] + playlist_description = sys.argv[3] +else: + print("Usage: %s username playlist-name playlist-description" % (sys.argv[0],)) + sys.exit() + +scope = 'playlist-modify-public' +token = util.prompt_for_user_token(username, scope, client_id='9f8caa1d3bb44187854c5282f94f83b2',client_secret='fa0dd2c91ca045dbb53e158e49f1aa3d',redirect_uri='https://blooming-refuge-48604.herokuapp.com/') + +if token: + sp = spotipy.Spotify(auth=token) + sp.trace = False + playlists = sp.user_playlist_create(username, playlist_name, + playlist_description) + pprint.pprint(playlists) +else: + print("Can't get token for", username)