# Emotion-Driven Music and IoT Devices for Collaborative Exer-Games
## 5 Music Recommendation and Personalization Based on Emotions
### Generation of a User Profile for Personalization  
The personalization system is based on data services provided by _Spotify_. Functionally, the system uses the musical seed which represents the user's musical preferences and tastes. This seed, combined with the user's requested emotion, is then used to select the appropriate songs to return. Figure 7 illustrates the system's functionalities which are divided into distinct activities, each managed by an orchestrator and triggered via an HTTP Trigger accessible to the user.  
A _Spotify_ premium account and an authorization token are required to access specific _Spotify_ API requests and retrieve user data. Depending on the scope of the data, different permissions are necessary. In this case, the following permissions are utilized: user-library-read, user-top-read, playlist-read-private, user-read-recently-played, and playlist-read-collaborative. These scopes must be set prior to requesting the user's authorization, and this step is handled locally on the device using the Authorization Code with PKCE Flow, which is recommended for this environment. Once the user is registered in the application and grants the necessary permissions, the authentication details are stored in the COSMOS database.  
The process begins when the Compute musical seed trigger is activated by an HTTP GET request. This trigger retrieves the user ID from the JSON body of the request and initiates the orchestrator with the recovered JSON as a parameter. It waits for the orchestrator to return the response to the client.  
The Compute musical seed orchestrator is responsible for coordinating the execution of all subsequent activities. The first activity, Find user's auth activity, retrieves the user's authentication details from the COSMOS database. The user must have been previously registered, and their authentication token must have been requested.  
Once the user's authentication is confirmed, the Get preferred tracks activity retrieves the identifiers of the songs that best match the user's preferences. This is accomplished through several _Spotify_ API requests:  
* Get Saved Tracks retrieves the user's saved songs.
* Get Recently Played Tracks returns the user's recently played songs.
* Get User's Top Items lists the top tracks identified by _Spotify_ as most relevant to the user.
* Get User's Playlists, combined with Get Playlist Items, retrieves the user's playlists and the songs within each playlist.  
Figure 7: Flow of Azure Durable Function for computing the personalization musical seed.  
The output of these API requests is a list of _Spotify_ identifiers representing the user's favorite songs.  
The next activity, Find songs activity, checks the COSMOS database to find songs that are already stored. It returns the stored songs, including their audio features and emotional labels, in JSON format.  
For songs not already stored in the database, the process continues with the following activities: Get Music Info, Label Tracks, and Insert Songs, which are described in Section 5.2.  
Finally, the Update Musical Seed activity calculates the average of the musical features provided by _Spotify_, excluding key and mode due to their discrete nature as well as duration as it is less relevant to user preferences. If a song appears multiple times across the different _Spotify_ requests, its features carry more weight in the seed calculation, emphasizing the importance of songs frequently listened to or saved by the user. The resulting musical seed is then updated in the COSMOS database.