mirror of
https://github.com/apricote/Listory.git
synced 2026-01-13 21:21:02 +00:00
feat(api): update existing artists in MusicLibrary
This commit is contained in:
parent
a0c28e2324
commit
a0ffe108e1
11 changed files with 176 additions and 1 deletions
|
|
@ -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,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue