-
Notifications
You must be signed in to change notification settings - Fork 2
/
spotify.py
93 lines (73 loc) · 2.69 KB
/
spotify.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import sys
import spotipy
import spotipy.util as util
import codecs
def GetTracks(playlist_response) :
input = str(playlist_response)
idePrefix = "spotify:track:"
delim = "'"
index = 0
result = ""
while index < len(input) :
if input.find(idePrefix, index) != -1 :
index = input.index(idePrefix, index) + 14
delimIndex = input.index(delim, index)
result = result + str(input[index : delimIndex]) + "\n"
index = index + 1
return result
def GetUserId(current_user) :
input = str(current_user)
idPrefix = "spotify:user:"
delim = "'"
if input.find(idPrefix) :
idIndex = input.index(idPrefix) + 13
delimIndex = input.index(delim, idIndex)
result = str(input[idIndex : delimIndex])
return result
return ""
def GetPlaylistId(SpotifyClient, playlistName, userId) :
input = str(playlistName)
idPrefix = "spotify:playlist:"
delim = "'"
response = SpotifyClient.user_playlists(userId)
print(response)
def GetKeyFromConfig(key) :
keyStr = str(key)
f = codecs.open("Config.txt", encoding = "utf-8")
for x in f :
input = str(x)
if input.find(keyStr) != -1 :
tempList = input.split(':')
return tempList[1].strip()
def GetUserName() :
return GetKeyFromConfig(key="USERNAME")
def GetClientId() :
return GetKeyFromConfig(key="CLIENT_ID")
def GetClientSecret() :
return GetKeyFromConfig(key="CLIENT_SECRET")
username = GetUserName()
clientId = GetClientId()
clientSecret = GetClientSecret()
print(username)
print(clientId)
print(clientSecret)
scope = 'user-library-read user-library-modify playlist-modify-private playlist-modify-public'
redirect_uri = 'http://localhost:8080/'
token = util.prompt_for_user_token(username,scope,clientId,clientSecret, redirect_uri)
sp = spotipy.Spotify(auth=token)
userId = GetUserId(current_user = sp.current_user())
playlistURL = str(input("Enter Playlist URL : "))
playlistSplit = playlistURL.split('/')
playlistID = playlistSplit[len(playlistSplit) - 1]
f = open("GPMLibraryParsed.txt", encoding="utf8")
for x in f:
print(x)
response = sp.search(x, limit = 1)
strResponse = str(response)
if strResponse.find("spotify:track") != -1 :
index = strResponse.index("spotify:track")
delimiter = strResponse.index("'", index)
trackId = str(strResponse[index : delimiter])
trackId = trackId.replace("spotify:track:", "")
sp.current_user_saved_tracks_add(tracks=[trackId])
sp.user_playlist_add_tracks(user = userId,playlist_id = playlistID, tracks=[trackId])