mirror of
https://github.com/gabehf/Koito.git
synced 2026-03-18 19:56:33 -07:00
chore: initial public commit
This commit is contained in:
commit
fc9054b78c
250 changed files with 32809 additions and 0 deletions
57
internal/mbz/artist.go
Normal file
57
internal/mbz/artist.go
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
package mbz
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"slices"
|
||||
|
||||
"github.com/gabehf/koito/internal/logger"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type MusicBrainzArtist struct {
|
||||
Name string `json:"name"`
|
||||
SortName string `json:"sort_name"`
|
||||
Gender string `json:"gender"`
|
||||
Area MusicBrainzArea `json:"area"`
|
||||
Aliases []MusicBrainzArtistAlias `json:"aliases"`
|
||||
}
|
||||
type MusicBrainzArtistAlias struct {
|
||||
Name string `json:"name"`
|
||||
Type string `json:"type"`
|
||||
Primary bool `json:"primary"`
|
||||
}
|
||||
|
||||
const artistAliasFmtStr = "%s/ws/2/artist/%s?inc=aliases"
|
||||
|
||||
func (c *MusicBrainzClient) getArtist(ctx context.Context, id uuid.UUID) (*MusicBrainzArtist, error) {
|
||||
var mbzArtist *MusicBrainzArtist
|
||||
err := c.getEntity(ctx, artistAliasFmtStr, id, mbzArtist)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return mbzArtist, nil
|
||||
}
|
||||
|
||||
// Returns the artist name at index 0, and all primary aliases after.
|
||||
func (c *MusicBrainzClient) GetArtistPrimaryAliases(ctx context.Context, id uuid.UUID) ([]string, error) {
|
||||
l := logger.FromContext(ctx)
|
||||
artist, err := c.getArtist(ctx, id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if artist == nil {
|
||||
return nil, errors.New("artist could not be found by musicbrainz")
|
||||
}
|
||||
used := make(map[string]bool)
|
||||
ret := make([]string, 1)
|
||||
ret[0] = artist.Name
|
||||
used[artist.Name] = true
|
||||
for _, alias := range artist.Aliases {
|
||||
if alias.Primary && !slices.Contains(ret, alias.Name) {
|
||||
l.Debug().Msgf("Found primary alias '%s' for artist '%s'", alias.Name, artist.Name)
|
||||
ret = append(ret, alias.Name)
|
||||
}
|
||||
}
|
||||
return ret, nil
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue