Skip to content

Commit

Permalink
fix: get recommendations
Browse files Browse the repository at this point in the history
  • Loading branch information
Raul Andrade committed Feb 28, 2022
1 parent 8cccc25 commit aa027c7
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/constants/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export { scopes } from './scopes'
export { MAX_RANDOM_FY_ITEMS } from './values'
export { MAX_RANDOM_FY_ITEMS, MAX_ARTISTS_TO_SHOW_PER_TURN } from './values'
1 change: 1 addition & 0 deletions src/constants/values.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export const MAX_RANDOM_FY_ITEMS = 20
export const MAX_ARTISTS_TO_SHOW_PER_TURN = 3
19 changes: 17 additions & 2 deletions src/pages/api/recommendations.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { MAX_ARTISTS_TO_SHOW_PER_TURN } from '../../constants'
import type { NextApiRequest, NextApiResponse } from 'next'

import { spotifyApi } from 'services'
import { Rnd } from 'utils'

export default async function handler(
req: NextApiRequest,
Expand All @@ -14,11 +16,24 @@ export default async function handler(
req.query?.name as string
)

const artistCollection = response.body.artists.slice(0, 3)
const artists = response.body.artists
const sliceEnd = Rnd.getRndNumber({
min: MAX_ARTISTS_TO_SHOW_PER_TURN,
max: artists.length
})
const sliceStart = sliceEnd - MAX_ARTISTS_TO_SHOW_PER_TURN

const artistCollection = response.body.artists.slice(sliceStart, sliceEnd)

const topTrackCollection: any[] = []
for (const artist of artistCollection) {
const topTrack = await spotifyApi.getArtistTopTracks(artist.id, 'BR')
topTrackCollection.push(topTrack.body.tracks[0])
const topTrackSelected = Rnd.getRndNumber({
min: 0,
max: topTrack.body.tracks.length
})

topTrackCollection.push(topTrack.body.tracks[topTrackSelected])
}

const result = artistCollection.map((artist, index) => ({
Expand Down

1 comment on commit aa027c7

@vercel
Copy link

@vercel vercel bot commented on aa027c7 Feb 28, 2022

Choose a reason for hiding this comment

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

Please sign in to comment.