slevin48 / music

Music App based on Spotify API and Streamlit framework
MIT License
14 stars 3 forks source link
boto3 python spotipy streamlit

Music 48 🎵Open in Streamlit

sergent-pepper

Started out as a Spotify music recommendation app

NEW: Added features from music-generator 🎹

Content:

  1. Parse Streaming History
  2. Music Taste Analysis
  3. Get Recommendation
  4. Spotify API

1. Parse Streaming History

Streaming history can be retrieved from the Spotify profile as JSON, or through the spotipy functions current_user_recently_played or current_user_saved_tracks

with open("recently_played_20210306.json","r") as f:
    results = json.load(f)

tracks = []
for idx, item in enumerate(results['items']):
    track = item['track']
    tracks.append([idx, track['artists'][0]['name'], track['name']])

trackDict = {"id":[], "artist":[],"name":[]}
for idx, item in enumerate(results['items']):
    track = item['track']
    trackDict["id"].append(idx)
    trackDict["artist"].append(track['artists'][0]['name'])
    trackDict["name"].append(track['name'])

import pandas as pd
trackDf = pd.DataFrame.from_dict(trackDict)

2. Music Taste Analysis

Analysis of features to produce a polar plot

feature-plot

import spotifyAPI
from secret import clientId,clientSecret
token  = spotifyAPI.get_token(clientId,clientSecret)
lucy_id = spotifyAPI.get_track_id2('Lucy in the Sky', token, artist = 'The Beatles')

url = "https://open.spotify.com/track/"+lucy_id
import webbrowser
webbrowser.open(url)

import pandas as pd

lucy_features = spotifyAPI.get_features(lucy_id,token)
df = pd.DataFrame(lucy_features, index=[0])
df_features = df.loc[: ,['acousticness', 'danceability', 'energy', 'instrumentalness', 'liveness', 'speechiness', 'valence']]

spotifyAPI.feature_plot(df_features)

3. Get Recommendation

json_response = spotifyAPI.get_track_reco(lucy_id,token)
uris =[]
for i in json_response['tracks']:
            uris.append(i)
            print(f"\"{i['name']}\" by {i['artists'][0]['name']}")

4. Spotify API

This notebook leverages the Spotipy module to access the Spotify API:

https://spotipy.readthedocs.io/

spotifyNoneUserData

Scopes

Resources