navidrome/api/media_annotation.go

68 lines
1.8 KiB
Go
Raw Normal View History

package api
import (
2016-03-15 22:06:23 +01:00
"fmt"
"time"
"github.com/astaxie/beego"
"github.com/deluan/gosonic/api/responses"
"github.com/deluan/gosonic/engine"
"github.com/deluan/gosonic/utils"
)
type MediaAnnotationController struct {
BaseAPIController
scrobbler engine.Scrobbler
}
func (c *MediaAnnotationController) Prepare() {
utils.ResolveDependencies(&c.scrobbler)
}
func (c *MediaAnnotationController) Scrobble() {
2016-03-22 00:35:40 +01:00
ids := c.RequiredParamStrings("id", "Required id parameter is missing")
times := c.ParamTimes("time")
if len(times) > 0 && len(times) != len(ids) {
c.SendError(responses.ERROR_GENERIC, fmt.Sprintf("Wrong number of timestamps: %d", len(times)))
}
submission := c.ParamBool("submission", true)
2016-03-17 23:58:09 +01:00
playerId := 1 // TODO Multiple players, based on playerName/username/clientIP(?)
2016-03-17 15:37:19 +01:00
playerName := c.ParamString("c")
username := c.ParamString("u")
2016-03-22 00:51:22 +01:00
beego.Debug("Scrobbling ids:", ids, "times:", times, "submission:", submission)
2016-03-22 00:35:40 +01:00
for i := range ids {
var t time.Time
if len(times) > 0 {
t = times[i]
} else {
t = time.Now()
}
2016-03-22 00:35:40 +01:00
// TODO Fix skipped songs
//skip, err := c.scrobbler.DetectSkipped(playerId, id, submission)
//if err != nil {
// beego.Error("Error detecting skip:", err)
//}
//if skip {
// beego.Info("Skipped previous song")
//}
if submission {
mf, err := c.scrobbler.Register(playerId, ids[i], t)
if err != nil {
beego.Error("Error scrobbling:", err)
continue
2016-03-22 00:35:40 +01:00
}
beego.Info(fmt.Sprintf(`Scrobbled (%s) "%s" at %v`, ids[i], mf.Title, t))
} else {
mf, err := c.scrobbler.NowPlaying(playerId, ids[i], username, playerName)
if err != nil {
beego.Error("Error setting", ids[i], "as current song:", err)
continue
2016-03-22 00:35:40 +01:00
}
beego.Info(fmt.Sprintf(`Current Song (%s) "%s" at %v`, ids[i], mf.Title, t))
2016-03-17 02:04:41 +01:00
}
}
response := c.NewEmpty()
c.SendResponse(response)
}