package main
import (
"fmt"
"strconv"
"github.com/navidrome/navidrome/plugins/pdk/go/pdk"
"github.com/navidrome/navidrome/plugins/pdk/go/scrobbler"
)
func init() {
scrobbler.Register(&testScrobbler{})
}
type testScrobbler struct{}
func (t *testScrobbler) IsAuthorized(scrobbler.IsAuthorizedRequest) (bool, error) {
return checkAuthConfig(), nil
}
func (t *testScrobbler) NowPlaying(input scrobbler.NowPlayingRequest) error {
if err := checkConfigError(); err != nil {
return err
}
artistName := ""
if len(input.Track.Artists) > 0 {
artistName = input.Track.Artists[0].Name
}
pdk.Log(pdk.LogInfo, "NowPlaying: "+input.Track.Title+" by "+artistName)
return nil
}
func (t *testScrobbler) Scrobble(input scrobbler.ScrobbleRequest) error {
if err := checkConfigError(); err != nil {
return err
}
artistName := ""
if len(input.Track.Artists) > 0 {
artistName = input.Track.Artists[0].Name
}
pdk.Log(pdk.LogInfo, "Scrobble: "+input.Track.Title+" by "+artistName)
return nil
}
func (t *testScrobbler) PlaybackReport(input scrobbler.PlaybackReportRequest) error {
if err := checkConfigError(); err != nil {
return err
}
artistName := ""
if len(input.Track.Artists) > 0 {
artistName = input.Track.Artists[0].Name
}
pdk.Log(pdk.LogInfo, "PlaybackReport: "+input.Track.Title+" by "+artistName+" state="+input.State)
return nil
}
func checkConfigError() error {
errMsg, hasErr := pdk.GetConfig("error")
if !hasErr || errMsg == "" {
return nil
}
errType, _ := pdk.GetConfig("error_type")
switch errType {
case scrobbler.ScrobblerErrorNotAuthorized.Error():
return fmt.Errorf("%w: %s", scrobbler.ScrobblerErrorNotAuthorized, errMsg)
case scrobbler.ScrobblerErrorRetryLater.Error():
return fmt.Errorf("%w: %s", scrobbler.ScrobblerErrorRetryLater, errMsg)
default:
return fmt.Errorf("%w: %s", scrobbler.ScrobblerErrorUnrecoverable, errMsg)
}
}
func checkAuthConfig() bool {
authStr, hasAuth := pdk.GetConfig("authorized")
if !hasAuth {
return true
}
auth, err := strconv.ParseBool(authStr)
if err != nil {
return true
}
return auth
}
func main() {}