mirror of
https://github.com/gabehf/Koito.git
synced 2026-03-07 21:48:18 -08:00
* add dev branch container to workflow * correctly set the default range of ActivityGrid * fix: set name/short_name to koito (#61) * fix dev container push workflow * fix: race condition with using getComputedStyle primary color for dynamic activity grid darkening (#76) * Fix race condition with using getComputedStyle primary color for dynamic activity grid darkening Instead just use the color from the current theme directly. Tested works on initial load and theme changes. Fixes https://github.com/gabehf/Koito/issues/75 * Rework theme provider to provide the actual Theme object throughtout the app, in addition to the name Split name out of the Theme struct to simplify custom theme saving/reading * fix: set first artist listed as primary by default (#81) * feat: add server-side configuration with default theme (#90) * docs: add example for usage of the main listenbrainz instance (#71) * docs: add example for usage of the main listenbrainz instance * Update scrobbler.md --------- Co-authored-by: Gabe Farrell <90876006+gabehf@users.noreply.github.com> * feat: add server-side cfg and default theme * fix: repair custom theme --------- Co-authored-by: m0d3rnX <jesper@posteo.de> * docs: add default theme cfg option to docs * feat: add ability to manually scrobble track (#91) * feat: add button to manually scrobble from ui * fix: ensure timestamp is in the past, log fix * test: add integration test * feat: add first listened to dates for media items (#92) * fix: ensure error checks for ErrNoRows * feat: add now playing endpoint and ui (#93) * wip * feat: add now playing * fix: set default theme when config is not set * feat: fetch images from subsonic server (#94) * fix: useQuery instead of useEffect for now playing * feat: custom artist separator regex (#95) * Fix race condition with using getComputedStyle primary color for dynamic activity grid darkening Instead just use the color from the current theme directly. Tested works on initial load and theme changes. Fixes https://github.com/gabehf/Koito/issues/75 * Rework theme provider to provide the actual Theme object throughtout the app, in addition to the name Split name out of the Theme struct to simplify custom theme saving/reading * feat: add server-side configuration with default theme (#90) * docs: add example for usage of the main listenbrainz instance (#71) * docs: add example for usage of the main listenbrainz instance * Update scrobbler.md --------- Co-authored-by: Gabe Farrell <90876006+gabehf@users.noreply.github.com> * feat: add server-side cfg and default theme * fix: repair custom theme --------- Co-authored-by: m0d3rnX <jesper@posteo.de> * fix: rebase errors --------- Co-authored-by: pet <128837728+againstpetra@users.noreply.github.com> Co-authored-by: mlandry <mike.landry@gmail.com> Co-authored-by: m0d3rnX <jesper@posteo.de>
137 lines
4.4 KiB
Go
137 lines
4.4 KiB
Go
package images
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"net/url"
|
|
|
|
"github.com/gabehf/koito/internal/cfg"
|
|
"github.com/gabehf/koito/internal/logger"
|
|
"github.com/gabehf/koito/queue"
|
|
)
|
|
|
|
type SubsonicClient struct {
|
|
url string
|
|
userAgent string
|
|
authParams string
|
|
requestQueue *queue.RequestQueue
|
|
}
|
|
|
|
type SubsonicAlbumResponse struct {
|
|
SubsonicResponse struct {
|
|
Status string `json:"status"`
|
|
SearchResult3 struct {
|
|
Album []struct {
|
|
CoverArt string `json:"coverArt"`
|
|
} `json:"album"`
|
|
} `json:"searchResult3"`
|
|
} `json:"subsonic-response"`
|
|
}
|
|
|
|
type SubsonicArtistResponse struct {
|
|
SubsonicResponse struct {
|
|
Status string `json:"status"`
|
|
SearchResult3 struct {
|
|
Artist []struct {
|
|
ArtistImageUrl string `json:"artistImageUrl"`
|
|
} `json:"artist"`
|
|
} `json:"searchResult3"`
|
|
} `json:"subsonic-response"`
|
|
}
|
|
|
|
const (
|
|
subsonicAlbumSearchFmtStr = "/rest/search3?%s&f=json&query=%s&v=1.13.0&c=koito&artistCount=0&songCount=0&albumCount=1"
|
|
subsonicArtistSearchFmtStr = "/rest/search3?%s&f=json&query=%s&v=1.13.0&c=koito&artistCount=1&songCount=0&albumCount=0"
|
|
subsonicCoverArtFmtStr = "/rest/getCoverArt?%s&id=%s&v=1.13.0&c=koito"
|
|
)
|
|
|
|
func NewSubsonicClient() *SubsonicClient {
|
|
ret := new(SubsonicClient)
|
|
ret.url = cfg.SubsonicUrl()
|
|
ret.userAgent = cfg.UserAgent()
|
|
ret.authParams = cfg.SubsonicParams()
|
|
ret.requestQueue = queue.NewRequestQueue(5, 5)
|
|
return ret
|
|
}
|
|
|
|
func (c *SubsonicClient) 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
|
|
} else if resp.StatusCode >= 300 || resp.StatusCode < 200 {
|
|
err = fmt.Errorf("recieved non-ok status from Subsonic: %s", resp.Status)
|
|
done <- queue.RequestResult{Body: nil, Err: err}
|
|
}
|
|
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 *SubsonicClient) getEntity(ctx context.Context, endpoint string, result any) error {
|
|
l := logger.FromContext(ctx)
|
|
url := c.url + endpoint
|
|
l.Debug().Msgf("Sending request to ImageSrc: GET %s", url)
|
|
req, err := http.NewRequest("GET", url, nil)
|
|
if err != nil {
|
|
return fmt.Errorf("getEntity: %w", err)
|
|
}
|
|
l.Debug().Msg("Adding ImageSrc request to queue")
|
|
body, err := c.queue(ctx, req)
|
|
if err != nil {
|
|
l.Err(err).Msg("Subsonic request failed")
|
|
return fmt.Errorf("getEntity: %w", err)
|
|
}
|
|
|
|
err = json.Unmarshal(body, result)
|
|
if err != nil {
|
|
l.Err(err).Msg("Failed to unmarshal Subsonic response")
|
|
return fmt.Errorf("getEntity: %w", err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (c *SubsonicClient) GetAlbumImage(ctx context.Context, artist, album string) (string, error) {
|
|
l := logger.FromContext(ctx)
|
|
resp := new(SubsonicAlbumResponse)
|
|
l.Debug().Msgf("Finding album image for %s from artist %s", album, artist)
|
|
err := c.getEntity(ctx, fmt.Sprintf(subsonicAlbumSearchFmtStr, c.authParams, url.QueryEscape(artist+" "+album)), resp)
|
|
if err != nil {
|
|
return "", fmt.Errorf("GetAlbumImage: %v", err)
|
|
}
|
|
l.Debug().Any("subsonic_response", resp).Send()
|
|
if len(resp.SubsonicResponse.SearchResult3.Album) < 1 || resp.SubsonicResponse.SearchResult3.Album[0].CoverArt == "" {
|
|
return "", fmt.Errorf("GetAlbumImage: failed to get album art")
|
|
}
|
|
return cfg.SubsonicUrl() + fmt.Sprintf(subsonicCoverArtFmtStr, c.authParams, url.QueryEscape(resp.SubsonicResponse.SearchResult3.Album[0].CoverArt)), nil
|
|
}
|
|
|
|
func (c *SubsonicClient) GetArtistImage(ctx context.Context, artist string) (string, error) {
|
|
l := logger.FromContext(ctx)
|
|
resp := new(SubsonicArtistResponse)
|
|
l.Debug().Msgf("Finding artist image for %s", artist)
|
|
err := c.getEntity(ctx, fmt.Sprintf(subsonicArtistSearchFmtStr, c.authParams, url.QueryEscape(artist)), resp)
|
|
if err != nil {
|
|
return "", fmt.Errorf("GetArtistImage: %v", err)
|
|
}
|
|
l.Debug().Any("subsonic_response", resp).Send()
|
|
if len(resp.SubsonicResponse.SearchResult3.Artist) < 1 || resp.SubsonicResponse.SearchResult3.Artist[0].ArtistImageUrl == "" {
|
|
return "", fmt.Errorf("GetArtistImage: failed to get artist art")
|
|
}
|
|
return resp.SubsonicResponse.SearchResult3.Artist[0].ArtistImageUrl, nil
|
|
}
|