Skip to content

Commit

Permalink
Fix artist search when lastfm information isn't available
Browse files Browse the repository at this point in the history
  • Loading branch information
sferra committed Feb 16, 2024
1 parent d01e199 commit 30139bc
Showing 1 changed file with 27 additions and 14 deletions.
41 changes: 27 additions & 14 deletions packages/core/src/plugins/meta/discogs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import {
import {
DiscogsReleaseSearchResponse,
DiscogsArtistSearchResponse,
DiscogsArtistReleasesSearchResponse,
DiscogsArtistInfo,
DiscogsArtistSearchResult,
DiscogsReleaseSearchResult,
Expand Down Expand Up @@ -175,29 +174,19 @@ class DiscogsMetaProvider extends MetaProvider {

const lastFmInfo: LastFmArtistInfo = (await (await this.lastfm.getArtistInfo(discogsInfo.name)).json()).artist;
const lastFmTopTracks: LastfmTopTracks = (await (await this.lastfm.getArtistTopTracks(discogsInfo.name)).json()).toptracks;

const coverImage = this.getCoverImage(discogsInfo);

const spotifyToken = await getToken();
const similarArtists = await Promise.all(take(lastFmInfo.similar.artist, 5).map(async artist => {
const similarArtist = await searchArtists(spotifyToken, artist.name);

return {
name: artist.name,
thumbnail: similarArtist?.images[0]?.url
};
}));
const similarArtists = await this.getSimilarArtists(lastFmInfo);

Check warning on line 178 in packages/core/src/plugins/meta/discogs.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/plugins/meta/discogs.ts#L178

Added line #L178 was not covered by tests

return {
id: discogsInfo.id,
name: discogsInfo.name,
description: _.get(lastFmInfo, 'bio.summary'),
tags: _.map(_.get(lastFmInfo, 'tags.tag'), 'name'),
onTour: lastFmInfo.ontour === '1',
onTour: this.isArtistOnTour(lastFmInfo),
coverImage,
thumb: coverImage,
images: _.map(discogsInfo.images, 'resource_url'),
topTracks: _.map(lastFmTopTracks.track, (track: LastfmTrack) => ({
topTracks: _.map(lastFmTopTracks?.track, (track: LastfmTrack) => ({
name: track.name,
title: track.name,
thumb: coverImage,
Expand All @@ -210,6 +199,30 @@ class DiscogsMetaProvider extends MetaProvider {
};
}

async getSimilarArtists(artistInfo: LastFmArtistInfo | undefined) {
if (!artistInfo) {
return [];

Check warning on line 204 in packages/core/src/plugins/meta/discogs.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/plugins/meta/discogs.ts#L204

Added line #L204 was not covered by tests
}
const spotifyToken = await getToken();
return await Promise.all(

Check warning on line 207 in packages/core/src/plugins/meta/discogs.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/plugins/meta/discogs.ts#L206-L207

Added lines #L206 - L207 were not covered by tests
take(artistInfo.similar.artist, 5)
.map(async artist => {
const similarArtist = await searchArtists(spotifyToken, artist.name);
return {

Check warning on line 211 in packages/core/src/plugins/meta/discogs.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/plugins/meta/discogs.ts#L210-L211

Added lines #L210 - L211 were not covered by tests
name: artist.name,
thumbnail: similarArtist?.images[0]?.url
};
})
);
}

isArtistOnTour(artistInfo: LastFmArtistInfo | undefined): boolean {
if (!artistInfo) {
return false;

Check warning on line 221 in packages/core/src/plugins/meta/discogs.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/plugins/meta/discogs.ts#L221

Added line #L221 was not covered by tests
}
return artistInfo?.ontour === '1';
}

async fetchArtistDetailsByName(artistName: string): Promise<ArtistDetails> {
const artistSearch = await (await Discogs.search(artistName, 'artist')).json();
const artist: DiscogsArtistSearchResult = _.head(artistSearch.results);
Expand Down

0 comments on commit 30139bc

Please sign in to comment.