fucked up with the last commit, everything is fine now

This commit is contained in:
Adrian
2018-03-30 13:15:29 +02:00
parent fc9582805b
commit 1949f99ace
2 changed files with 18 additions and 21 deletions

1
.cache-ribe1912 Normal file
View File

@@ -0,0 +1 @@
{"access_token": "BQDm9EX4KPKTzCCn6l8UAR-wIsU-GipWSspVO0rp2YgjUNXbggeylmZFhjsgpfK7w47j4rAoRNXA_F3kpKZf09cmQp8p0iKOxf3-rge2kgRtd5vaNLoPVsHtU6Hqlvd7K8WKpeWWxyMaxDSiovC3Hy-5zUC-I-Jau4XjssenIkNF-k4", "expires_in": 3600, "expires_at": 1522412048, "token_type": "Bearer", "scope": "playlist-modify-public", "refresh_token": "AQBW26IVCaa55FGT90tJtlZvYnXapLTDuytVyGNDxvagou_BOEs7Xjno7Cnr7yFpUWn0FwrCS6wppHmCezOQbcPtRW5pN3l2fIaMZ31gwlVonJgQtqqOIueDb_uSAVwu7Gw"}

View File

@@ -2,56 +2,52 @@ import pprint
import sys import sys
import spotipy import spotipy
import os import os
import config as cnf
import spotipy.util as util 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(): def getUsernameAndLinks():
if len(sys.argv) > 2: username = cnf.username
username = sys.argv[1] if len(sys.argv) > 1:
trackLinks = sys.argv[2:] trackLinks = sys.argv[1:]
else: else:
print("Usage: %s username playlist_id track_id ..." % (sys.argv[0],)) print("Usage: %s track_id ..." % (sys.argv[0],))
sys.exit() sys.exit()
return username, trackLinks return username, trackLinks
def checkAndConvertLink(trackLinks): def checkAndConvertLink(trackLinks):
linkString = trackLinks[0] linkString = trackLinks[0]
#'?' + str is added to the hyperlink when copying songs from the app / desktop. Remove this to run in webbrowser #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/')
questionMarkIndex = linkString.find('?') questionMarkIndex = linkString.find('?')
print(trackStringIndex)
print(questionMarkIndex)
if questionMarkIndex != -1: if questionMarkIndex != -1:
trackLinks = [linkString[0:questionMarkIndex]] #Only supports adding one song at a time so far. 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.
else: else:
print("Link format is already in base-62 form")
pass pass
return trackLinks return trackLinks
def addSongToPlaylist(): def addSongToPlaylist():
#Get keys which allows to connect to the Spotify app
clientID, clientSecret, redirectUri, playlistID = getKeysAndPlaylist()
username, trackLinks = getUsernameAndLinks() username, trackLinks = getUsernameAndLinks()
trackLink = checkAndConvertLink(trackLinks) trackLinks = checkAndConvertLink(trackLinks)
#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=clientID, client_secret=clientSecret, redirect_uri = redirectUri) token = util.prompt_for_user_token(username, scope, client_id=cnf.clientID, client_secret=cnf.clientSecret, redirect_uri = cnf.redirectUri)
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(username, playlistID, trackLinks) results = sp.user_playlist_add_tracks(cnf.username, cnf.playlistID, trackLinks)
print(results) print(results)
else: else:
print("Can't get token for", username) print("Can't get token for", username)