|
| 1 | +import spotipy |
| 2 | +import config |
| 3 | +from spotipy.oauth2 import SpotifyClientCredentials #To access authorised Spotify data |
| 4 | + |
| 5 | +client_id = config.client_id |
| 6 | +client_secret = config.secret_key |
| 7 | + |
| 8 | +client_credentials_manager = SpotifyClientCredentials(client_id=client_id, client_secret=client_secret) |
| 9 | +sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager) #spotify object to access API |
| 10 | +name = input("Enter Artist Name: ") #chosen artist |
| 11 | +result = sp.search(name) #search query |
| 12 | +artist_uri = '' |
| 13 | +for row in result['tracks']['items'][0]['artists']: |
| 14 | + if name.lower() in row['name'].lower(): |
| 15 | + artist_uri = row['uri'] |
| 16 | + break |
| 17 | + |
| 18 | +#Pull all of the artist's albums |
| 19 | +sp_albums = sp.artist_albums(artist_uri, album_type='album') |
| 20 | +#Store artist's albums' names' and uris in separate lists |
| 21 | + |
| 22 | +album_names = [] |
| 23 | +album_uris = [] |
| 24 | +for i in range(len(sp_albums['items'])): |
| 25 | + album_names.append(sp_albums['items'][i]['name']) |
| 26 | + album_uris.append(sp_albums['items'][i]['uri']) |
| 27 | + |
| 28 | + |
| 29 | +def albumSongs(uri): |
| 30 | + album = uri #assign album uri to a_name |
| 31 | + spotify_albums[album] = {} #Creates dictionary for that specific album |
| 32 | + #Create keys-values of empty lists inside nested dictionary for album |
| 33 | + spotify_albums[album]['album'] = [] #create empty list |
| 34 | + spotify_albums[album]['track_number'] = [] |
| 35 | + spotify_albums[album]['id'] = [] |
| 36 | + spotify_albums[album]['name'] = [] |
| 37 | + spotify_albums[album]['uri'] = [] |
| 38 | + tracks = sp.album_tracks(album) #pull data on album tracks |
| 39 | + for n in range(len(tracks['items'])): #for each song track |
| 40 | + spotify_albums[album]['album'].append(album_names[album_count]) #append album name tracked via album_count |
| 41 | + spotify_albums[album]['track_number'].append(tracks['items'][n]['track_number']) |
| 42 | + spotify_albums[album]['id'].append(tracks['items'][n]['id']) |
| 43 | + spotify_albums[album]['name'].append(tracks['items'][n]['name']) |
| 44 | + spotify_albums[album]['uri'].append(tracks['items'][n]['uri']) |
| 45 | + |
| 46 | +spotify_albums = {} |
| 47 | +album_count = 0 |
| 48 | +for i in album_uris: #each album |
| 49 | + albumSongs(i) |
| 50 | + album_count+=1 #Updates album count once all tracks have been added |
| 51 | + |
| 52 | +def popularity(album): |
| 53 | + spotify_albums[album]['popularity'] = [] |
| 54 | + track_count = 0 |
| 55 | + for track in spotify_albums[album]['uri']: |
| 56 | + pop = sp.track(track) |
| 57 | + spotify_albums[album]['popularity'].append(pop['popularity']) |
| 58 | + track_count+=1 |
| 59 | + |
| 60 | +for i in spotify_albums: |
| 61 | + popularity(i) |
| 62 | + |
| 63 | +dic_df = {} |
| 64 | +dic_df['name'] = [] |
| 65 | +dic_df['popularity'] = [] |
| 66 | +for album in spotify_albums: |
| 67 | + for feature in ['name', 'popularity']: |
| 68 | + dic_df[feature].extend(spotify_albums[album][feature]) |
| 69 | + |
| 70 | + |
| 71 | +import pandas as pd |
| 72 | +df = pd.DataFrame.from_dict(dic_df) |
| 73 | + |
| 74 | +final_df = df.sort_values('popularity', ascending=False) |
| 75 | +pd.set_option("display.max_rows", None, "display.max_columns", None) |
| 76 | +print(final_df) |
0 commit comments