-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathspotify_accessor.py
More file actions
116 lines (94 loc) · 3.72 KB
/
spotify_accessor.py
File metadata and controls
116 lines (94 loc) · 3.72 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
from spotipy.oauth2 import SpotifyClientCredentials
import spotipy
import spotipy.util as util
import numpy as np
from dotenv import load_dotenv
load_dotenv()
import os
# Spotify Token Access
username = os.getenv("USERNAME")
client_id = os.getenv("CLIENT_ID")
client_secret = os.getenv("CLIENT_SECRET")
if not client_id or not client_secret or not username:
print('ERROR: One of client_id, client_secret, or username is unset in spotify_accessor.py.')
exit(1)
client_credentials_manager = SpotifyClientCredentials(client_id=client_id, client_secret=client_secret)
sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager)
# Create Spotify playlist ID
def create_playlist(username, playlist_name):
username = username
token = util.prompt_for_user_token(username=username, scope='playlist-modify-public', client_id=client_id,
client_secret=client_secret, redirect_uri="http://localhost:8888/callback")
if token:
sp = spotipy.Spotify(auth=token)
sp.trace = False
playlists = sp.user_playlist_create(username, playlist_name)
return playlists['id']
# Get trackID for songs
def get_track_id(song_name1, artist_name1, album_name1):
print("Adding songs to playlist!")
id_list = []
my_array = []
album_list = []
song_list = []
i = 0
while i < len(song_name1):
artist = artist_name1[i]
track = song_name1[i]
try:
track_id = sp.search(q='artist:' + artist + ' track:' + track, type='track')
except spotipy.exceptions.SpotifyException:
print(f'WARNING: Could not find "{track}" in Spotify.')
continue
for songsID in track_id['tracks']['items']:
id_list.append(songsID['id'])
if not id_list:
album_list.append(album_name1[i])
song_list.append(song_name1[i])
else:
my_array.append(id_list[0])
id_list = []
i += 1
return my_array, album_list, song_list
# Get trackID for songs
def get_missing_track_id(missing_albums1, missing_tracks1):
id_list = []
my_array = []
i = 0
while i < len(missing_tracks1):
album = missing_albums1[i]
track = missing_tracks1[i]
track_id = sp.search(q='album:' + album + ' track:' + track, type='track')
for songsID in track_id['tracks']['items']:
id_list.append(songsID['id'])
if not id_list:
print('Could not add: ' + missing_tracks1[i])
else:
my_array.append(id_list[0])
id_list = []
i += 1
return my_array
def add_more_than_100_songs_to_playlist(username, playlist_id, track_ids):
#split array in size of 50
if(len(track_ids) > 50) :
track_id_chunks = np.array_split(track_ids, len(track_ids)/50)
for track_id_chunk in track_id_chunks:
add_songs_to_playlist(username, playlist_id, track_id_chunk)
else :
add_songs_to_playlist(username, playlist_id, track_ids)
# Add songs to Spotify Playlist
def add_songs_to_playlist(username, playlist_id, track_ids):
username = username
playlist_id = playlist_id
track_ids = track_ids
token = util.prompt_for_user_token(username=username, scope='playlist-modify-public', client_id=client_id,
client_secret=client_secret, redirect_uri="http://localhost:8888/callback")
if token:
sp = spotipy.Spotify(auth=token)
sp.trace = False
results = sp.user_playlist_add_tracks(username, playlist_id, track_ids)
print('Finished transferring playlist')
return results
def add_song_ids(multiple_tracks1, more_tracks1):
result = multiple_tracks1 + more_tracks1
return result