Scrobble accepts multiple ids

This commit is contained in:
Deluan 2016-03-21 19:35:40 -04:00
parent 12b1002d51
commit 8e1736703d
2 changed files with 59 additions and 27 deletions

View file

@ -5,6 +5,8 @@ import (
"fmt"
"time"
"strconv"
"github.com/astaxie/beego"
"github.com/deluan/gosonic/api/responses"
"github.com/deluan/gosonic/engine"
@ -25,6 +27,14 @@ func (c *BaseAPIController) RequiredParamString(param string, msg string) string
return p
}
func (c *BaseAPIController) RequiredParamStrings(param string, msg string) []string {
ps := c.Input()[param]
if len(ps) == 0 {
c.SendError(responses.ERROR_MISSING_PARAMETER, msg)
}
return ps
}
func (c *BaseAPIController) ParamString(param string) string {
return c.Input().Get(param)
}
@ -38,6 +48,18 @@ func (c *BaseAPIController) ParamTime(param string, def time.Time) time.Time {
return utils.ToTime(value)
}
func (c *BaseAPIController) ParamTimes(param string) []time.Time {
pStr := c.Input()[param]
times := make([]time.Time, len(pStr))
for i, t := range pStr {
ti, err := strconv.ParseInt(t, 10, 64)
if err == nil {
times[i] = utils.ToTime(ti)
}
}
return times
}
func (c *BaseAPIController) ParamInt(param string, def int) int {
value := def
c.Ctx.Input.Bind(&value, param)
@ -59,7 +81,7 @@ func (c *BaseAPIController) SendError(errorCode int, message ...interface{}) {
if len(message) == 0 {
msg = responses.ErrorMsg(errorCode)
} else {
msg = fmt.Sprintf(message[0].(string), message[1:len(message)]...)
msg = fmt.Sprintf(message[0].(string), message[1:]...)
}
response.Error = &responses.Error{Code: errorCode, Message: msg}

View file

@ -20,38 +20,48 @@ func (c *MediaAnnotationController) Prepare() {
}
func (c *MediaAnnotationController) Scrobble() {
id := c.RequiredParamString("id", "Required id parameter is missing")
time := c.ParamTime("time", time.Now())
submission := c.ParamBool("submission", false)
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)
playerId := 1 // TODO Multiple players, based on playerName/username/clientIP(?)
playerName := c.ParamString("c")
username := c.ParamString("u")
skip, err := c.scrobbler.DetectSkipped(playerId, id, submission)
if err != nil {
beego.Error("Error detecting skip:", err)
}
if skip {
beego.Info("Skipped previous song")
for i := range ids {
var t time.Time
if len(times) > 0 {
t = times[i]
} else {
t = time.Now()
}
// 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, id, time)
mf, err := c.scrobbler.Register(playerId, ids[i], t)
if err != nil {
beego.Error("Error scrobbling:", err)
c.SendError(responses.ERROR_GENERIC, "Internal error")
}
beego.Info(fmt.Sprintf(`Scrobbled (%s) "%s" at %v`, id, mf.Title, time))
beego.Info(fmt.Sprintf(`Scrobbled (%s) "%s" at %v`, ids[i], mf.Title, t))
} else {
mf, err := c.scrobbler.NowPlaying(playerId, id, username, playerName)
mf, err := c.scrobbler.NowPlaying(playerId, ids[i], username, playerName)
if err != nil {
beego.Error("Error setting", id, "as current song:", err)
beego.Error("Error setting", ids[i], "as current song:", err)
c.SendError(responses.ERROR_GENERIC, "Internal error")
}
beego.Info(fmt.Sprintf(`Current Song (%s) "%s" at %v`, id, mf.Title, time))
beego.Info(fmt.Sprintf(`Current Song (%s) "%s" at %v`, ids[i], mf.Title, t))
}
}
response := c.NewEmpty()
c.SendResponse(response)
}