|
|
|
"""Most Popular Albums Per Artist With Gradio.ipynb |
|
|
|
Automatically generated by Colaboratory. |
|
|
|
Original file is located at |
|
https://colab.research.google.com/drive/1wMpev3zhNcOdO_amUdfeE6HCflOB8KIL |
|
|
|
# Set up Spotify credentials |
|
|
|
Before getting started you need: |
|
|
|
* Spotify API permissions & credentials that could apply for [here](https://developer.spotify.com/). Simply log in, go to your “dashboard” and select “create client id” and follow the instructions. Spotify are not too strict on providing permissions so put anything you like when they ask for commercial application. |
|
|
|
* Python module — spotipy — imported |
|
""" |
|
|
|
import spotipy |
|
|
|
from spotipy.oauth2 import SpotifyClientCredentials |
|
from fuzzywuzzy import fuzz |
|
import pandas as pd |
|
import seaborn as sns |
|
import gradio as gr |
|
|
|
import matplotlib.pyplot as plt |
|
import time |
|
import numpy as np |
|
|
|
|
|
|
|
def choose_artist(name_input, sp): |
|
results = sp.search(name_input) |
|
|
|
top_matches = [] |
|
counter = 0 |
|
|
|
for i in results['tracks']['items']: |
|
|
|
current_item = results['tracks']['items'][counter]['artists'] |
|
counter+=1 |
|
|
|
counter2 = 0 |
|
for i in current_item: |
|
|
|
|
|
top_matches.append((current_item[counter2]['name'], current_item[counter2]['uri'])) |
|
counter2+=1 |
|
|
|
|
|
top_matches = list(set(top_matches)) |
|
|
|
fuzzy_matches = [] |
|
|
|
for i in top_matches: |
|
|
|
ratio = fuzz.ratio(name_input, i[0]) |
|
|
|
fuzzy_matches.append((i[0], ratio, i[1])) |
|
|
|
fuzzy_matches = sorted(fuzzy_matches, key=lambda tup: tup[1], reverse=True) |
|
|
|
chosen = fuzzy_matches[0][0] |
|
chosen_id = fuzzy_matches[0][1] |
|
chosen_uri = fuzzy_matches[0][2] |
|
print("The results are based on the artist: ", chosen) |
|
return chosen, chosen_id, chosen_uri |
|
|
|
|
|
def find_albums(artist_uri, sp): |
|
sp_albums = sp.artist_albums(artist_uri, album_type='album', limit=50) |
|
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']) |
|
return album_uris, album_names |
|
|
|
|
|
def albumSongs(album, sp, album_count, album_names, spotify_albums): |
|
spotify_albums[album] = {} |
|
|
|
spotify_albums[album]['album_name'] = [] |
|
spotify_albums[album]['track_number'] = [] |
|
spotify_albums[album]['song_id'] = [] |
|
spotify_albums[album]['song_name'] = [] |
|
spotify_albums[album]['song_uri'] = [] |
|
|
|
tracks = sp.album_tracks(album) |
|
|
|
for n in range(len(tracks['items'])): |
|
spotify_albums[album]['album_name'].append(album_names[album_count]) |
|
spotify_albums[album]['track_number'].append(tracks['items'][n]['track_number']) |
|
spotify_albums[album]['song_id'].append(tracks['items'][n]['id']) |
|
spotify_albums[album]['song_name'].append(tracks['items'][n]['name']) |
|
spotify_albums[album]['song_uri'].append(tracks['items'][n]['uri']) |
|
|
|
|
|
def popularity(album, sp, spotify_albums): |
|
|
|
spotify_albums[album]['popularity'] = [] |
|
|
|
track_count = 0 |
|
for track in spotify_albums[album]['song_uri']: |
|
|
|
pop = sp.track(track) |
|
spotify_albums[album]['popularity'].append(pop['popularity']) |
|
track_count+=1 |
|
|
|
def gradio_music_graph(client_id, client_secret, artist_name): |
|
|
|
client_credentials_manager = SpotifyClientCredentials(client_id=client_id, client_secret=client_secret) |
|
sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager) |
|
|
|
chosen_artist, ratio_score, artist_uri = choose_artist(artist_name, sp=sp) |
|
|
|
album_uris, album_names = find_albums(artist_uri, sp=sp) |
|
|
|
spotify_albums = {} |
|
|
|
album_count = 0 |
|
for i in album_uris: |
|
albumSongs(i, sp=sp, album_count=album_count, album_names=album_names, spotify_albums=spotify_albums) |
|
print("Songs from " + str(album_names[album_count]) + " have been added to spotify_albums dictionary") |
|
album_count+=1 |
|
|
|
|
|
sleep_min = 2 |
|
sleep_max = 5 |
|
start_time = time.time() |
|
request_count = 0 |
|
|
|
for album in spotify_albums: |
|
popularity(album, sp=sp, spotify_albums=spotify_albums) |
|
request_count+=1 |
|
if request_count % 5 == 0: |
|
|
|
time.sleep(np.random.uniform(sleep_min, sleep_max)) |
|
|
|
|
|
|
|
|
|
dic_df = {} |
|
|
|
dic_df['album_name'] = [] |
|
dic_df['track_number'] = [] |
|
dic_df['song_id'] = [] |
|
dic_df['song_name'] = [] |
|
dic_df['song_uri'] = [] |
|
dic_df['popularity'] = [] |
|
|
|
for album in spotify_albums: |
|
for feature in spotify_albums[album]: |
|
dic_df[feature].extend(spotify_albums[album][feature]) |
|
|
|
|
|
df = pd.DataFrame.from_dict(dic_df) |
|
df = df.sort_values(by='popularity') |
|
df = df.drop_duplicates(subset=['song_id'], keep=False) |
|
|
|
sns.set_style('ticks') |
|
|
|
fig, ax = plt.subplots() |
|
fig.set_size_inches(11, 8) |
|
ax.set_xticklabels(ax.get_xticklabels(), rotation=25, ha="right", wrap=True) |
|
plt.tight_layout(rect=[1, 2.5, 1, 0.45]) |
|
|
|
sns.boxplot(x=df["album_name"], y=df["popularity"], ax=ax) |
|
|
|
plt.show() |
|
|
|
return fig |
|
|
|
music_plots = gr.Interface(gradio_music_graph, ["text", "text", "text"], |
|
["plot"], title="Popular Songs By Album Box Plot Distribution on Spotify", description="Using your Spotify API Access from https://developer.spotify.com/ you can see your favourite artist's most popular albums on Spotify") |
|
|
|
music_plots.launch(debug=True) |