Skip to content

Commit

Permalink
fix: issue loading albums for artists
Browse files Browse the repository at this point in the history
  • Loading branch information
eligundry committed Jul 12, 2024
1 parent dcad45b commit 7dbb9b7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 20 deletions.
4 changes: 3 additions & 1 deletion app/lib/context.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ export const getRequestContextValues = (
logger: requestLogger,
})

requestLogger.info(undefined)
if (env.NODE_ENV === 'production') {
requestLogger.info(undefined)
}

return {
...context,
Expand Down
39 changes: 20 additions & 19 deletions app/lib/spotify.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,26 +119,21 @@ export class Spotify {
}

getRandomAlbumForArtistByID = async (artistID: string) => {
const artistPromise = this.api.artists.get(artistID)
let resp = await this.api.artists.albums(artistID, 'album', this.country)
let offset = random(0, Math.max(resp.total - 1, 0))
const artistPromise = await this.api.artists.get(artistID)
let resp = await this.api.artists.albums(
artistID,
'album',
this.country,
50,
)

if (offset > resp.items.length - 1) {
resp = await this.api.artists.albums(
artistID,
'album',
this.country,
1,
offset,
)
offset = 0
if (!resp.items.length) {
throw new Error('artist has no albums, please retry')
}

const album = resp.items[offset]
const album = sample(resp.items) ?? resp.items[0]

if (!album) {
throw new Error('could not fetch album from that offset, please retry')
} else if (album.id === this.lastPresentedID) {
if (album.id === this.lastPresentedID) {
throw new Error('album is the one we last presented, please retry')
}

Expand Down Expand Up @@ -503,10 +498,15 @@ export class Spotify {
return playlist
}

searchArists = async (term: string): Promise<SpotifyArtist[]> => {
searchArists = async (
term: string,
limit?: number,
): Promise<SpotifyArtist[]> => {
const results = await this.search({
value: term,
type: ['artist'],
// @ts-ignore
limit,
})

return (
Expand Down Expand Up @@ -610,15 +610,16 @@ export class Spotify {
}
}

getUserTopArtists = async (): Promise<SpotifyArtist[]> => {
getUserTopArtists = async (limit = 50): Promise<SpotifyArtist[]> => {
if (!this.api.getAccessToken()) {
throw new Error('User must be logged in to use this')
}

const artists = await this.api.currentUser.topItems(
'artists',
undefined,
50,
// @ts-ignore C'mon the typing on this API is wild
limit,
)
return artists.items.map((artist) => ({
name: artist.name,
Expand Down

0 comments on commit 7dbb9b7

Please sign in to comment.