feat(api): update existing artists in MusicLibrary

This commit is contained in:
Julian Tölle 2021-11-21 15:53:27 +01:00
parent a0c28e2324
commit a0ffe108e1
11 changed files with 176 additions and 1 deletions

View file

@ -126,6 +126,17 @@ export class SpotifyService {
});
}
async runUpdaterForAllEntities(): Promise<void> {
this.logger.debug("Starting Spotify updater loop");
const oldestArtist =
await this.musicLibraryService.getArtistWithOldestUpdate();
if (oldestArtist) {
await this.updateArtist(oldestArtist.spotify.id);
}
}
async importTrack(
spotifyID: string,
retryOnExpiredToken: boolean = true
@ -266,6 +277,44 @@ export class SpotifyService {
});
}
async updateArtist(
spotifyID: string,
retryOnExpiredToken: boolean = true
): Promise<Artist> {
const artist = await this.importArtist(spotifyID, retryOnExpiredToken);
let spotifyArtist: ArtistObject;
try {
spotifyArtist = await this.spotifyApi.getArtist(
this.appAccessToken,
spotifyID
);
} catch (err) {
if (err.response && err.response.status === 401 && retryOnExpiredToken) {
await this.refreshAppAccessToken();
return this.updateArtist(spotifyID, false);
}
throw err;
}
const genres = await Promise.all(
spotifyArtist.genres.map((genreName) => this.importGenre(genreName))
);
this.musicLibraryService.updateArtist({
artist,
updatedFields: {
name: spotifyArtist.name,
genres,
},
});
return artist;
}
async importGenre(name: string): Promise<Genre> {
const genre = await this.musicLibraryService.findGenre({
name,