-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetGenre.py
49 lines (40 loc) · 1.7 KB
/
getGenre.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
# import libraries
import pandas as pd
import spotipy, Data.Input.myKeys as myKeys
from spotipy.oauth2 import SpotifyClientCredentials
# load in data
streamingHistory = pd.read_csv(r'Data\Input\streamingHistory.csv')
# get unique entries for artist column
uniqueArtist = pd.DataFrame(streamingHistory.artistName.unique())
# create new columns to hold data
uniqueArtist['id'] = None
uniqueArtist['genres'] = None
# rename column
uniqueArtist = uniqueArtist.rename(columns={0: 'artistName'})
# keys to spotify api
cid = myKeys.clientID
secret = myKeys.secret
#spotipy credentials manager
client_credentials_manager = SpotifyClientCredentials(client_id=cid, client_secret=secret)
sp = spotipy.Spotify(client_credentials_manager = client_credentials_manager)
genreList = []
# for each row in dataframe select artist
#for i in range(5):
for i in range(len(uniqueArtist)):
artist = uniqueArtist.loc[i, 'artistName']
#print(searchQuery)
searchResults = sp.search(q=artist, type='artist', limit = 1)
# if doesn't exist = None
if len(searchResults['artists']['items']) == 0:
uniqueArtist.loc[i,'id':] = None
uniqueArtist.loc[i,'genres'] = None
print(artist + ': *** Not found on Spotify ***')
continue
# else enter desired fields into dataframe
else:
uniqueArtist.loc[i, 'id'] = searchResults['artists']['items'][0]['id']
genreList.extend(searchResults['artists']['items'][0]['genres'])
uniqueArtist.loc[i, 'genres'] = searchResults['artists']['items'][0]['genres']
genreList = list(set(genreList))
uniqueArtist = uniqueArtist.explode('genres') # explode lists, (similar to melt?)
uniqueArtist.to_csv(r'Data\artistData.csv', index = False)# save to CSV