Skip to content

Commit 941aa0e

Browse files
Merge pull request #214 from gargnityansh/songs
added songs of artist
2 parents 2eb1b75 + 2b9fc86 commit 941aa0e

File tree

3 files changed

+95
-0
lines changed

3 files changed

+95
-0
lines changed

songs-of-artist/config.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
client_id = ""
2+
secret_key = ""

songs-of-artist/readme.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
Songs of Artist
2+
3+
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.
4+
5+
Getting Started
6+
7+
Install spotipy using pip
8+
9+
$ pip install spotipy
10+
11+
After installation
12+
13+
Update the client_id and secret_key in the configuration file to authenticate the spotipy api
14+
15+
After updation
16+
17+
Run the file songs_of_artists.py to see the songs of your favorite artist.

songs-of-artist/songs_of_artist.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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

Comments
 (0)