Skip to content

When a new entry is added to the user's listening history, we should generate new recommendations PS-213 - #90

Open
DYA13 wants to merge 3 commits into
mainfrom
generate_new_recommendations_PS_213
Open

When a new entry is added to the user's listening history, we should generate new recommendations PS-213#90
DYA13 wants to merge 3 commits into
mainfrom
generate_new_recommendations_PS_213

Conversation

@DYA13

@DYA13 DYA13 commented Nov 29, 2023

Copy link
Copy Markdown
Collaborator

No description provided.

@DYA13
DYA13 requested a review from akosasante November 29, 2023 11:38
@DYA13

DYA13 commented Nov 29, 2023

Copy link
Copy Markdown
Collaborator Author

Akos I have done initial code , could you please guide me further what to do?
I would like to ask as wel SPOTIFY_CLIENT_SECRET should be here instead of SPOTIFY_ACCESS_TOKEN , right?
recommendationUtility.js
const response = await axios.get(spotifyApiEndpoint, {
params: {
seed_artists: artistIds.join(','),
},
headers: {
Authorization: 'SPOTIFY_ACCESS_TOKEN',
},
});
as for spotifyApiEndpoint, should I generate it on site?
Probably files would need refactoring

@akosasante akosasante left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a quick note on ordering of tickets. I see in Jira that I marked PS-213 as being blocked by PS-212. So that means we need work from PS-212 first. And it looks like you've assigned yourself that ticket ("When a user starts playing an album, add it to the listening history")

But PS-212 is marked as being blocked by PS-182 ("Backend should store play history"), which is assigned to Natalia. Just a reminder to make sure y'all have talked and are not re-doing same work by implementing the schema for ListeningHistory.js

If the idea is that in your PR here you're just writing up a barebones version of that schema, to be filled out further by Natalia's work then that's totally fine 👍 . I just wanted to call it out to make sure everyone is on the same page :)

Comment thread src/utils/recommendationUtility.js Outdated
Comment thread src/utils/recommendationUtility.js Outdated
Comment thread src/utils/recommendationUtility.js Outdated
@DYA13
DYA13 requested a review from akosasante February 29, 2024 11:28

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A little bit of bad planning on my part sorry! It looks like @NatachaKey 's ticket PS-182 also led to her creating a schema for storing recently listened to albums: https://github.com/Code-the-Dream-School/dd-prac-team4-back/pull/91/files

Let's merge those changes in (git merge origin/backendStoresPlayHistory) and then include the post-save callback you've done here but instead in the RecentlyListened schema

// Access the model to fetch ListeningHistory documents
const ListeningHistory = this.constructor;

// Fetch the top 5 listened to artists from the user's listening history

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Fetch the top 5 listened to artists from the user's listening history
// Fetch the 5 most recent listened to albums from the user's listening history

// Extract artistIds from listeningHistory
const artistIds = listeningHistory.map((entry) => entry.artistId);

// Make a call to Spotify's "Get Recommendations" endpoint

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before we can make any calls to Spotify's api we need to authenticate our session. We can follow the same pattern we've done over in the fetchAlbumSpotify file:

// Get an access token using client credentials flow
const data = await spotifyApi.clientCredentialsGrant();
spotifyApi.setAccessToken(data.body['access_token']);

}

// Extract artistIds from listeningHistory
const artistIds = listeningHistory.map((entry) => entry.artistId);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently, we aren't storing the spotify artist id in our application. So we will need to first make a call to Spotify to get the artist id. 

There is a Get Album by id endpoint: https://developer.spotify.com/documentation/web-api/reference/get-an-album

Our spotify-web-api library that we're using can access it with the getAlbum function: https://github.com/thelinmichael/spotify-web-api-node#more-examples

The album id we need is the spotify album id and not our own Mongo object id. Right now, the only place we're storing that is in the spotifyUrl, we may want to change that later but for now, we can follow a similar pattern as the frontend where we use regex to extract that id from the url field: https://github.com/Code-the-Dream-School/dd-prac-team4-front/blob/40ffb979b0229c5f2485eaecc87e31af55fd3fa4/src/components/album/AlbumPreview.js#L19-L29

An album can have multiple artists, for now just grab the first artist id in the returned list.

songId: track.id,
}));

await AlbumRecommendation.insertMany(recommendations);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We will probably want to make AlbumRecommendations have a uniqueIndex so that we don't insert the same recommended song multiple times. And in that case we'll want to add ordered: false so that it will insert all unique entries and return errors for any that were not unique: https://mongoosejs.com/docs/api/model.html#Model.insertMany()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants