Recommendations in track index

This commit is contained in:
James Barnsley
2017-11-13 16:52:53 +13:00
parent da443805da
commit d5e2b56d95
5 changed files with 118 additions and 69 deletions

View File

@ -869,7 +869,7 @@ export function getFavorites(limit = 50, term = 'long_term'){
export function getRecommendations(uris = [], limit = 20){
return (dispatch, getState) => {
dispatch({type: 'SPOTIFY_RECOMMENDATIONS_LOADED', tracks: [], artists_uris: [], albums_uris: []})
dispatch({type: 'CLEAR_SPOTIFY_RECOMMENDATIONS'});
// build our starting point
var artists_ids = []
@ -909,15 +909,16 @@ export function getRecommendations(uris = [], limit = 20){
sendRequest(dispatch, getState, endpoint)
.then(
response => {
var tracks = Object.assign([], response.tracks);
// We only get simple artist objects, so we need to
// get the full object. We'll add URIs to our recommendations
// anyway so we can proceed in the meantime
var artists_uris = []
if (response.tracks.length > artists_ids.length && response.tracks.length > 10){
if (tracks.length > artists_ids.length && tracks.length > 10){
while (artists_uris.length < 5){
var random_index = Math.round(Math.random() * (response.tracks.length - 1))
var artist = response.tracks[random_index].artists[0]
var random_index = Math.round(Math.random() * (tracks.length - 1))
var artist = tracks[random_index].artists[0]
// Make sure this artist is not already in our sample, and
// is not one of the seeds
@ -931,10 +932,10 @@ export function getRecommendations(uris = [], limit = 20){
// Copy already loaded albums into array
var albums = []
var albums_uris = []
if (response.tracks.length > 10){
if (tracks.length > 10){
while (albums.length < 5){
var random_index = Math.round(Math.random() * (response.tracks.length - 1))
var album = response.tracks[random_index].album
var random_index = Math.round(Math.random() * (tracks.length - 1))
var album = tracks[random_index].album
// Make sure this album is not already in our sample
if (!albums_uris.includes(album.uri)){
@ -944,19 +945,23 @@ export function getRecommendations(uris = [], limit = 20){
}
}
// Officially add albums to index
dispatch({
type: 'ALBUMS_LOADED',
albums: albums
})
});
dispatch({
type: 'TRACKS_LOADED',
tracks: tracks
});
dispatch({
type: 'SPOTIFY_RECOMMENDATIONS_LOADED',
seeds_uris: uris,
tracks: response.tracks,
tracks_uris: helpers.arrayOf('uri',tracks),
artists_uris: artists_uris,
albums_uris: helpers.arrayOf('uri',albums)
})
});
},
error => {
dispatch(coreActions.handleException(

View File

@ -105,6 +105,9 @@ export default function reducer(spotify = {}, action){
}
);
case 'CLEAR_SPOTIFY_RECOMMENDATIONS':
return Object.assign({}, spotify, {recommendations: {artists_uris: [], albums_uris: [], tracks_uris: []}});
case 'SPOTIFY_RECOMMENDATIONS_LOADED':
return Object.assign(
{},
@ -113,9 +116,9 @@ export default function reducer(spotify = {}, action){
recommendations: {
artists_uris: action.artists_uris,
albums_uris: action.albums_uris,
tracks: helpers.formatTracks(action.tracks)
tracks_uris: action.tracks_uris
}
})
});
case 'SPOTIFY_FAVORITES_LOADED':
return Object.assign(