# 4.8 - Recommendations

## Introduction

![screenshot](https://github.com/Ringo-Pro/Ringo.Pro/wiki/img/Schermafbeelding_2020-06-17_om_17.37.24.png)

## How it works

When a user search for something the application does a request for both `tracks` and `artists`. The recommendations are based on some of the audio features the track has. First we have to do another request to get the audio features. Currently the code only supports recommendations based on one track. This is because if we were be doing the recommendations on all the tracks the application would make too many request in a short time period for the Spotify API to handle and it will be prompted with an too many request error.

#### Initial search

```javascript
const searchResults = await getDataFromSpotfy(
  `https://api.spotify.com/v1/search?q=${req.query.query}&type=track%2Cartist&limit=10&offset=0`,
  options
);
const audioIdList = searchResults.tracks.items.map((track) => track.id);
const formattedIdList = audioIdList.toString().replace(/,/g, '%2C');
```

#### Request audio features

```jsx
const audioFeatures = await getDataFromSpotfy(
  `https://api.spotify.com/v1/audio-features?ids=${formattedIdList}`,
  options
);
```

#### Mood

When we have the audio features we run the songs through the mood assignment code. This will return a mood. Every mood has a `min` and `max` value for the `danceability`, `energy` and `valence` endpoints. Based on these values we can request recommendations by putting them inside the URL: `&min_feature=`, `&max_feature=`

```javascript
const featuresWithMood = audioFeatures.audio_features.map((track) => {
  return moodFilter.addMood(track);
});

const recommended = await getDataFromSpotfy(
  `https://api.spotify.com/v1/recommendations?limit=${10}&market=US&min_energy=${
    featuresWithMood[0].values.energyValues.min
  }&max_energy=${featuresWithMood[0].values.energyValues.max}&min_valence=${
    featuresWithMood[0].values.valenceValues.min
  }&max_valence=${
    featuresWithMood[0].values.valenceValues.max
  }&min_danceability=${
    featuresWithMood[0].values.danceabilityValues.min
  }&max_danceability=${
    featuresWithMood[0].values.danceabilityValues.max
  }&seed_tracks=${featuresWithMood[0].id}`,
  options
);
```

#### Render

These recommendations will be rendered on the page.

```javascript
res.render(__dirname + '/view/components/result-list.ejs', {
  trackData: searchResults.tracks.items,
  token: access_token,
  recommendations: recommended.tracks,
});
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://meijer-nick1.gitbook.io/meesterproef/4-code/4.8-recommendations.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
