| |
| |
| """ |
| Created on Sat Jul 23 10:15:02 2022 |
| |
| @author: halimbouayad |
| """ |
| import spotipy |
| from spotipy.oauth2 import SpotifyOAuth |
| import json |
| import spotipy.util as util |
| from spotipy.oauth2 import SpotifyClientCredentials |
| import requests |
| from requests import * |
| import pandas as pd |
| import streamlit as st |
| import numpy as np |
| import time |
|
|
| def connect(scope, username): |
| scope = "user-library-read" |
| token = SpotifyOAuth(scope=scope, username=username) |
| spotifyObject = spotipy.Spotify(auth_manager=token) |
| return spotifyObject |
|
|
|
|
| def get_features(a): |
| |
| feature_names = [f for f in a[0]] |
| features = [] |
|
|
| for i in range(len(a)): |
| |
| features.append([a[i][f] for f in a[i]]) |
| |
|
|
| |
| return features, feature_names |
|
|
|
|
|
|
| def get_artist(liste): |
| for i, artists in enumerate(liste): |
| if i ==0: |
| artist=artists['name'] |
| else: |
| artist=artist+", "+artists['name'] |
| return artist |
|
|
| @st.cache |
| def get_data(sp, username): |
| playlists = sp.current_user_playlists(limit=50) |
| |
| tr_names=[] |
| pl_urls=[] |
| pl_names=[] |
| |
| |
| date_added=[] |
| popularity=[] |
| ids=[] |
| artist_list=[] |
| |
| features=pd.DataFrame() |
| df=pd.DataFrame() |
| |
| while playlists: |
| |
| |
| |
| for i, playlist in enumerate(playlists['items']): |
| |
| |
| |
| |
| |
| |
| |
| |
| print(playlist) |
| playlist_content=sp.playlist(playlist['uri']) |
| for i,trax in enumerate(playlist_content['tracks']['items']): |
| |
| |
| tr_names.append(trax['track']['name']) |
| |
| |
| ids.append(trax['track']['id']) |
| |
| date_added.append(trax['added_at']) |
| |
| popularity.append(trax['track']['popularity']) |
| |
| |
| pl_names.append(playlist['name']) |
| |
| |
| pl_urls.append(playlist['uri']) |
| |
| artist_list.append(get_artist(trax['track']['artists'])) |
| |
| test='All done!' |
| |
| if playlists['next']: |
| playlists = sp.next(playlists) |
| else: |
| playlists = None |
| |
| |
|
|
|
|
| df['playlist']=pl_names |
| df['track']=tr_names |
| df['artist']=artist_list |
| df['playlist_url']=pl_urls |
| df['track_id']=ids |
| df['date_added']=date_added |
| df['popularity']=popularity |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| for start in range(0, len(df), 100): |
| |
| if start+100>len(df): |
| end=len(df) |
| else: |
| end=start+100 |
| |
|
|
|
|
| audio_feat=sp.audio_features(df.track_id[start:end]) |
| |
| |
| |
| |
| |
| feat, feat_names = get_features(audio_feat) |
| print('-----feat') |
| print(feat) |
| print('-----features') |
| print(features) |
| features=pd.concat([features, pd.DataFrame(feat)], axis=0) |
| print('-----features') |
| print(features) |
|
|
| |
| |
| |
| features.columns=feat_names |
| |
| df=pd.concat([df.reset_index(), features.reset_index()], axis=1) |
|
|
|
|
| |
| df=df.drop(columns=['index', 'index']) |
| |
| |
| return df, test |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| def EDA(df): |
| st.header("EDA") |
| |
| f_value=st.selectbox('Select a playlist', df.playlist.unique()) |
| dff=df[df.playlist==f_value] |
| |
| cols=['danceability','energy','acousticness','tempo','loudness','valence'] |
| for col in cols[:2]: |
| temp=dff.describe().loc['mean',:][col] |
| st.metric(col, '{:.1%}'.format(temp)) |
| |
| st.text('Top songs from that album:') |
| st.dataframe(dff) |
|
|
| selected=st.selectbox('By playlist (click to select other filter)', df.columns) |
| st.bar_chart(df[df.playlist==f_value].groupby(selected).agg({'danceability':'mean'})) |
| |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| |