initial commit

This commit is contained in:
azertop 2023-12-25 00:07:15 +01:00
parent 4ad6f8028c
commit 7d494828dd
8 changed files with 115555 additions and 0 deletions

28
README.md Normal file
View File

@ -0,0 +1,28 @@
# SpotifyApp
Welcome to the GitHub repository of my Spotify Mood Tracker project in Python!
The goal of this project is to track the mood of users on a daily basis by analyzing their Spotify listening history. The application uses the Spotify API to extract user's listening data, including musical genre, tempo, tone, and energy, and then uses data processing and machine learning techniques to determine the mood of each day.
The requirements.txt file included in the repository lists all the required Python dependencies to run the application. To install these dependencies, please execute the following command:
```
pip install -r requirements.txt
```
In this project, Spotify's credentials are set as environment variable. You fill have to set them before running the code.
```
export SPOTIFY_CLIENT_ID=""
export SPOTIFY_CLIENT_SECRET=""
```
The repository also contains examples of Spotify listening data to aid in testing the application. The data folder contains CSV files representing my listening history. You can use these files to test the application and see how it works. You can also download your own ListeningHistory on Spotify.
The main.py file contains the main code of the application, which uses the Spotify API and Python libraries to extract and analyze the listening data. The machine learning model is trained using scikit-learn and pickle is used to save the trained models.
Here you can see an example of what the calendar produced by this project looks like :
![](cal.png)
Have fun tracking your daily mood using your Spotify listening history!

60002
StreamingHistory0.json Normal file

File diff suppressed because it is too large Load Diff

55442
StreamingHistory1.json Normal file

File diff suppressed because it is too large Load Diff

BIN
cal.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

BIN
labelencoder.pickle Normal file

Binary file not shown.

77
main.py Normal file
View File

@ -0,0 +1,77 @@
import os
import pandas as pd
import spotipy
from spotipy.oauth2 import SpotifyClientCredentials
import pickle
import numpy as np
import july
import matplotlib.pyplot as plt
client_id = os.environ.get('SPOTIFY_CLIENT_ID')
client_secret = os.environ.get('SPOTIFY_CLIENT_SECRET')
client_credentials_manager = SpotifyClientCredentials(
client_id=client_id, client_secret=client_secret)
sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager)
#First we get the listening history
df = pd.read_json('StreamingHistory0.json')
df_temp = pd.read_json('StreamingHistory1.json')
df = pd.concat([df,df_temp])
df['Track'] = df['artistName']+'-'+df['trackName']
#We keep the date whitout the hour
df["endTime"]=df["endTime"].apply(lambda x:x[0:10])
del df_temp
musics=pd.DataFrame(df['Track'].unique())
df_audio_features = pd.DataFrame()
fail = []
for music in musics[0]:
results = sp.search(q=music, type="track")
if len(results)!=0:
track_uri = results["tracks"]["items"][0]["uri"]
#We use Spotify's feature
features = sp.audio_features(tracks=[track_uri])[0]
print(features)
if type(features) !='NoneType':
df_audio_features = df_audio_features.append(features, ignore_index=True)
else :
fail.append(music)
else :
fail.append(music)
features=["danceability","acousticness","energy","instrumentalness","liveness","valence","loudness","speechiness"]
df.drop("mode",axis=1)
df[df.columns[6:16]]=df[features]
with open('model.pickle', 'rb') as f:
pipe = pickle.load(f)
with open('labelencoder.pickle', 'rb') as f:
le = pickle.load(f)
#We predict the mood of each song
mood = pipe.predict(df[features])
df['mood']=le.inverse_transform(mood)
df['mood_int']=mood
#We group by day keeping the mode
mood_list = df.groupby('endTime')['mood_int'].apply(lambda x: x.mode()[0])
df
#We plot the result
july.heatmap(mood_list.index, mood_list.values, title='User\'s Mood', cmap="Dark2",month_grid=True,value_label=False)
'''
0 : calm
1 : energetic
2 : happy
3 : sad
'''

6
requirements.txt Normal file
View File

@ -0,0 +1,6 @@
pandas
spotipy
matplotlib
scikit-learn
pickle-mixin
july

BIN
svm_model.pickle Normal file

Binary file not shown.