From c00c4323abff68259562ef84b0fa2a6d2df25843 Mon Sep 17 00:00:00 2001 From: gargnityansh <45964420+gargnityansh@users.noreply.github.com> Date: Sat, 9 Jan 2021 21:49:40 +0530 Subject: [PATCH 1/2] added songs of artist Please enter spotify api details in the config for the working of songs_of_artist file --- songs-of-artist/config.py | 2 + songs-of-artist/songs_of_artist.py | 76 ++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 songs-of-artist/config.py create mode 100644 songs-of-artist/songs_of_artist.py diff --git a/songs-of-artist/config.py b/songs-of-artist/config.py new file mode 100644 index 0000000000..525ecd18d2 --- /dev/null +++ b/songs-of-artist/config.py @@ -0,0 +1,2 @@ +client_id = "" +secret_key = "" \ No newline at end of file diff --git a/songs-of-artist/songs_of_artist.py b/songs-of-artist/songs_of_artist.py new file mode 100644 index 0000000000..6d33b709f1 --- /dev/null +++ b/songs-of-artist/songs_of_artist.py @@ -0,0 +1,76 @@ +import spotipy +import config +from spotipy.oauth2 import SpotifyClientCredentials #To access authorised Spotify data + +client_id = config.client_id +client_secret = config.secret_key + +client_credentials_manager = SpotifyClientCredentials(client_id=client_id, client_secret=client_secret) +sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager) #spotify object to access API +name = input("Enter Artist Name: ") #chosen artist +result = sp.search(name) #search query +artist_uri = '' +for row in result['tracks']['items'][0]['artists']: + if name.lower() in row['name'].lower(): + artist_uri = row['uri'] + break + +#Pull all of the artist's albums +sp_albums = sp.artist_albums(artist_uri, album_type='album') +#Store artist's albums' names' and uris in separate lists + +album_names = [] +album_uris = [] +for i in range(len(sp_albums['items'])): + album_names.append(sp_albums['items'][i]['name']) + album_uris.append(sp_albums['items'][i]['uri']) + + +def albumSongs(uri): + album = uri #assign album uri to a_name + spotify_albums[album] = {} #Creates dictionary for that specific album + #Create keys-values of empty lists inside nested dictionary for album + spotify_albums[album]['album'] = [] #create empty list + spotify_albums[album]['track_number'] = [] + spotify_albums[album]['id'] = [] + spotify_albums[album]['name'] = [] + spotify_albums[album]['uri'] = [] + tracks = sp.album_tracks(album) #pull data on album tracks + for n in range(len(tracks['items'])): #for each song track + spotify_albums[album]['album'].append(album_names[album_count]) #append album name tracked via album_count + spotify_albums[album]['track_number'].append(tracks['items'][n]['track_number']) + spotify_albums[album]['id'].append(tracks['items'][n]['id']) + spotify_albums[album]['name'].append(tracks['items'][n]['name']) + spotify_albums[album]['uri'].append(tracks['items'][n]['uri']) + +spotify_albums = {} +album_count = 0 +for i in album_uris: #each album + albumSongs(i) + album_count+=1 #Updates album count once all tracks have been added + +def popularity(album): + spotify_albums[album]['popularity'] = [] + track_count = 0 + for track in spotify_albums[album]['uri']: + pop = sp.track(track) + spotify_albums[album]['popularity'].append(pop['popularity']) + track_count+=1 + +for i in spotify_albums: + popularity(i) + +dic_df = {} +dic_df['name'] = [] +dic_df['popularity'] = [] +for album in spotify_albums: + for feature in ['name', 'popularity']: + dic_df[feature].extend(spotify_albums[album][feature]) + + +import pandas as pd +df = pd.DataFrame.from_dict(dic_df) + +final_df = df.sort_values('popularity', ascending=False) +pd.set_option("display.max_rows", None, "display.max_columns", None) +print(final_df) From 2b9fc865abbe187ead5bdd942ae8fb052cc29942 Mon Sep 17 00:00:00 2001 From: gargnityansh <45964420+gargnityansh@users.noreply.github.com> Date: Sun, 10 Jan 2021 22:01:31 +0530 Subject: [PATCH 2/2] created readme.md --- songs-of-artist/readme.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 songs-of-artist/readme.md diff --git a/songs-of-artist/readme.md b/songs-of-artist/readme.md new file mode 100644 index 0000000000..f5334ace1b --- /dev/null +++ b/songs-of-artist/readme.md @@ -0,0 +1,17 @@ +Songs of Artist + +A music search application, using a few named technologies that allows users to search for the songs of their favorite Artists in a very less time. + +Getting Started + +Install spotipy using pip + +$ pip install spotipy + +After installation + +Update the client_id and secret_key in the configuration file to authenticate the spotipy api + +After updation + +Run the file songs_of_artists.py to see the songs of your favorite artist.