mirror of
https://github.com/gabehf/Koito.git
synced 2026-03-07 21:48:18 -08:00
chore: initial public commit
This commit is contained in:
commit
fc9054b78c
250 changed files with 32809 additions and 0 deletions
189
internal/images/deezer.go
Normal file
189
internal/images/deezer.go
Normal file
|
|
@ -0,0 +1,189 @@
|
|||
package images
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"github.com/gabehf/koito/internal/logger"
|
||||
"github.com/gabehf/koito/internal/utils"
|
||||
"github.com/gabehf/koito/queue"
|
||||
)
|
||||
|
||||
type DeezerClient struct {
|
||||
url string
|
||||
userAgent string
|
||||
requestQueue *queue.RequestQueue
|
||||
}
|
||||
|
||||
type DeezerAlbumResponse struct {
|
||||
Data []DeezerAlbum `json:"data"`
|
||||
}
|
||||
type DeezerAlbum struct {
|
||||
Title string `json:"title"`
|
||||
CoverXL string `json:"cover_xl"`
|
||||
CoverSm string `json:"cover_small"`
|
||||
CoverMd string `json:"cover_medium"`
|
||||
CoverBig string `json:"cover_big"`
|
||||
}
|
||||
type DeezerArtistResponse struct {
|
||||
Data []DeezerArtist `json:"data"`
|
||||
}
|
||||
type DeezerArtist struct {
|
||||
Name string `json:"name"`
|
||||
PictureXL string `json:"picture_xl"`
|
||||
PictureSm string `json:"picture_small"`
|
||||
PictureMd string `json:"picture_medium"`
|
||||
PictureBig string `json:"picture_big"`
|
||||
}
|
||||
|
||||
const (
|
||||
deezerBaseUrl = "https://api.deezer.com"
|
||||
albumImageEndpoint = "/search/album?q=%s"
|
||||
artistImageEndpoint = "/search/artist?q=%s"
|
||||
)
|
||||
|
||||
func NewDeezerClient(useragent string) *DeezerClient {
|
||||
ret := new(DeezerClient)
|
||||
ret.url = deezerBaseUrl
|
||||
ret.userAgent = useragent
|
||||
ret.requestQueue = queue.NewRequestQueue(1, 1)
|
||||
return ret
|
||||
}
|
||||
|
||||
func (c *DeezerClient) Shutdown() {
|
||||
c.requestQueue.Shutdown()
|
||||
}
|
||||
|
||||
func (c *DeezerClient) queue(ctx context.Context, req *http.Request) ([]byte, error) {
|
||||
l := logger.FromContext(ctx)
|
||||
req.Header.Set("User-Agent", c.userAgent)
|
||||
req.Header.Set("Accept", "application/json")
|
||||
|
||||
resultChan := c.requestQueue.Enqueue(func(client *http.Client, done chan<- queue.RequestResult) {
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
l.Debug().Err(err).Str("url", req.RequestURI).Msg("Failed to contact ImageSrc")
|
||||
done <- queue.RequestResult{Err: err}
|
||||
return
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
done <- queue.RequestResult{Body: body, Err: err}
|
||||
})
|
||||
|
||||
result := <-resultChan
|
||||
return result.Body, result.Err
|
||||
}
|
||||
|
||||
func (c *DeezerClient) getEntity(ctx context.Context, endpoint string, result any) error {
|
||||
l := logger.FromContext(ctx)
|
||||
url := deezerBaseUrl + endpoint
|
||||
l.Debug().Msgf("Sending request to ImageSrc: GET %s", url)
|
||||
req, err := http.NewRequest("GET", url, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
l.Debug().Msg("Adding ImageSrc request to queue")
|
||||
body, err := c.queue(ctx, req)
|
||||
if err != nil {
|
||||
l.Debug().Err(err)
|
||||
return err
|
||||
}
|
||||
|
||||
err = json.Unmarshal(body, result)
|
||||
if err != nil {
|
||||
l.Debug().Err(err)
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *DeezerClient) GetArtistImages(ctx context.Context, aliases []string) (string, error) {
|
||||
l := logger.FromContext(ctx)
|
||||
resp := new(DeezerArtistResponse)
|
||||
|
||||
aliasesUniq := utils.UniqueIgnoringCase(aliases)
|
||||
aliasesAscii := utils.RemoveNonAscii(aliasesUniq)
|
||||
|
||||
// Deezer very often uses romanized names for foreign artists, so check those first
|
||||
for _, a := range aliasesAscii {
|
||||
err := c.getEntity(ctx, fmt.Sprintf(artistImageEndpoint, url.QueryEscape(fmt.Sprintf("artist:\"%s\"", a))), resp)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if len(resp.Data) < 1 {
|
||||
return "", errors.New("artist image not found")
|
||||
}
|
||||
for _, v := range resp.Data {
|
||||
if strings.EqualFold(v.Name, a) {
|
||||
img := v.PictureXL
|
||||
l.Debug().Msgf("Found artist images for %s: %v", a, img)
|
||||
return img, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// if no romanized name exists or couldn't be found, check the rest
|
||||
for _, a := range utils.RemoveInBoth(aliasesUniq, aliasesAscii) {
|
||||
err := c.getEntity(ctx, fmt.Sprintf(artistImageEndpoint, url.QueryEscape(fmt.Sprintf("artist:\"%s\"", a))), resp)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if len(resp.Data) < 1 {
|
||||
return "", errors.New("artist image not found")
|
||||
}
|
||||
for _, v := range resp.Data {
|
||||
if strings.EqualFold(v.Name, a) {
|
||||
img := v.PictureXL
|
||||
l.Debug().Msgf("Found artist images for %s: %v", a, img)
|
||||
return img, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
return "", errors.New("artist image not found")
|
||||
}
|
||||
|
||||
func (c *DeezerClient) GetAlbumImages(ctx context.Context, artists []string, album string) (string, error) {
|
||||
l := logger.FromContext(ctx)
|
||||
resp := new(DeezerAlbumResponse)
|
||||
l.Debug().Msgf("Finding album image for %s from artist(s) %v", album, artists)
|
||||
// try to find artist + album match for all artists
|
||||
for _, alias := range artists {
|
||||
err := c.getEntity(ctx, fmt.Sprintf(albumImageEndpoint, url.QueryEscape(fmt.Sprintf("artist:\"%s\"album:\"%s\"", alias, album))), resp)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if len(resp.Data) > 0 {
|
||||
for _, v := range resp.Data {
|
||||
if strings.EqualFold(v.Title, album) {
|
||||
img := v.CoverXL
|
||||
l.Debug().Msgf("Found album images for %s: %v", album, img)
|
||||
return img, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// if none are found, try to find an album just by album title
|
||||
err := c.getEntity(ctx, fmt.Sprintf(albumImageEndpoint, url.QueryEscape(fmt.Sprintf("album:\"%s\"", album))), resp)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
for _, v := range resp.Data {
|
||||
if strings.EqualFold(v.Title, album) {
|
||||
img := v.CoverXL
|
||||
l.Debug().Msgf("Found album images for %s: %v", album, img)
|
||||
return img, nil
|
||||
}
|
||||
}
|
||||
|
||||
return "", errors.New("album image not found")
|
||||
}
|
||||
103
internal/images/imagesrc.go
Normal file
103
internal/images/imagesrc.go
Normal file
|
|
@ -0,0 +1,103 @@
|
|||
// package imagesrc defines interfaces for album and artist image providers
|
||||
package images
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"sync"
|
||||
|
||||
"github.com/gabehf/koito/internal/logger"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type ImageSource struct {
|
||||
deezerEnabled bool
|
||||
deezerC *DeezerClient
|
||||
caaEnabled bool
|
||||
}
|
||||
type ImageSourceOpts struct {
|
||||
UserAgent string
|
||||
EnableCAA bool
|
||||
EnableDeezer bool
|
||||
}
|
||||
|
||||
var once sync.Once
|
||||
var imgsrc ImageSource
|
||||
|
||||
type ArtistImageOpts struct {
|
||||
Aliases []string
|
||||
}
|
||||
|
||||
type AlbumImageOpts struct {
|
||||
Artists []string
|
||||
Album string
|
||||
ReleaseMbzID *uuid.UUID
|
||||
ReleaseGroupMbzID *uuid.UUID
|
||||
}
|
||||
|
||||
const caaBaseUrl = "https://coverartarchive.org"
|
||||
|
||||
// all functions are no-op if no providers are enabled
|
||||
func Initialize(opts ImageSourceOpts) {
|
||||
once.Do(func() {
|
||||
if opts.EnableCAA {
|
||||
imgsrc.caaEnabled = true
|
||||
}
|
||||
if opts.EnableDeezer {
|
||||
imgsrc.deezerEnabled = true
|
||||
imgsrc.deezerC = NewDeezerClient(opts.UserAgent)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func GetArtistImage(ctx context.Context, opts ArtistImageOpts) (string, error) {
|
||||
l := logger.FromContext(ctx)
|
||||
if imgsrc.deezerC != nil {
|
||||
img, err := imgsrc.deezerC.GetArtistImages(ctx, opts.Aliases)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return img, nil
|
||||
}
|
||||
l.Warn().Msg("No image providers are enabled")
|
||||
return "", nil
|
||||
}
|
||||
func GetAlbumImage(ctx context.Context, opts AlbumImageOpts) (string, error) {
|
||||
l := logger.FromContext(ctx)
|
||||
if imgsrc.caaEnabled {
|
||||
l.Debug().Msg("Attempting to find album image from CoverArtArchive")
|
||||
if opts.ReleaseMbzID != nil && *opts.ReleaseMbzID != uuid.Nil {
|
||||
url := fmt.Sprintf(caaBaseUrl+"/release/%s/front", opts.ReleaseMbzID.String())
|
||||
resp, err := http.DefaultClient.Head(url)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if resp.StatusCode == 200 {
|
||||
return url, nil
|
||||
}
|
||||
l.Debug().Str("url", url).Str("status", resp.Status).Msg("Could not find album cover from CoverArtArchive with MusicBrainz release ID")
|
||||
}
|
||||
if opts.ReleaseGroupMbzID != nil && *opts.ReleaseGroupMbzID != uuid.Nil {
|
||||
url := fmt.Sprintf(caaBaseUrl+"/release-group/%s/front", opts.ReleaseGroupMbzID.String())
|
||||
resp, err := http.DefaultClient.Head(url)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if resp.StatusCode == 200 {
|
||||
return url, nil
|
||||
}
|
||||
l.Debug().Str("url", url).Str("status", resp.Status).Msg("Could not find album cover from CoverArtArchive with MusicBrainz release group ID")
|
||||
}
|
||||
}
|
||||
if imgsrc.deezerEnabled {
|
||||
l.Debug().Msg("Attempting to find album image from Deezer")
|
||||
img, err := imgsrc.deezerC.GetAlbumImages(ctx, opts.Artists, opts.Album)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return img, nil
|
||||
}
|
||||
l.Warn().Msg("No image providers are enabled")
|
||||
return "", nil
|
||||
}
|
||||
28
internal/images/mock.go
Normal file
28
internal/images/mock.go
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
package images
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
)
|
||||
|
||||
type MockFinder struct{}
|
||||
|
||||
func (m *MockFinder) GetArtistImage(ctx context.Context, opts ArtistImageOpts) (string, error) {
|
||||
return "", nil
|
||||
}
|
||||
|
||||
func (m *MockFinder) GetAlbumImage(ctx context.Context, opts AlbumImageOpts) (string, error) {
|
||||
return "", nil
|
||||
}
|
||||
func (m *MockFinder) Shutdown() {}
|
||||
|
||||
type ErrorFinder struct{}
|
||||
|
||||
func (m *ErrorFinder) GetArtistImage(ctx context.Context, opts ArtistImageOpts) (string, error) {
|
||||
return "", errors.New("mock error")
|
||||
}
|
||||
|
||||
func (m *ErrorFinder) GetAlbumImage(ctx context.Context, opts AlbumImageOpts) (string, error) {
|
||||
return "", errors.New("mock error")
|
||||
}
|
||||
func (m *ErrorFinder) Shutdown() {}
|
||||
Loading…
Add table
Add a link
Reference in a new issue