Scrobble accepts multiple ids
This commit is contained in:
@@ -5,6 +5,8 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"strconv"
|
||||||
|
|
||||||
"github.com/astaxie/beego"
|
"github.com/astaxie/beego"
|
||||||
"github.com/deluan/gosonic/api/responses"
|
"github.com/deluan/gosonic/api/responses"
|
||||||
"github.com/deluan/gosonic/engine"
|
"github.com/deluan/gosonic/engine"
|
||||||
@@ -25,6 +27,14 @@ func (c *BaseAPIController) RequiredParamString(param string, msg string) string
|
|||||||
return p
|
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 {
|
func (c *BaseAPIController) ParamString(param string) string {
|
||||||
return c.Input().Get(param)
|
return c.Input().Get(param)
|
||||||
}
|
}
|
||||||
@@ -38,6 +48,18 @@ func (c *BaseAPIController) ParamTime(param string, def time.Time) time.Time {
|
|||||||
return utils.ToTime(value)
|
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 {
|
func (c *BaseAPIController) ParamInt(param string, def int) int {
|
||||||
value := def
|
value := def
|
||||||
c.Ctx.Input.Bind(&value, param)
|
c.Ctx.Input.Bind(&value, param)
|
||||||
@@ -59,7 +81,7 @@ func (c *BaseAPIController) SendError(errorCode int, message ...interface{}) {
|
|||||||
if len(message) == 0 {
|
if len(message) == 0 {
|
||||||
msg = responses.ErrorMsg(errorCode)
|
msg = responses.ErrorMsg(errorCode)
|
||||||
} else {
|
} 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}
|
response.Error = &responses.Error{Code: errorCode, Message: msg}
|
||||||
|
|
||||||
|
|||||||
+26
-16
@@ -20,38 +20,48 @@ func (c *MediaAnnotationController) Prepare() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *MediaAnnotationController) Scrobble() {
|
func (c *MediaAnnotationController) Scrobble() {
|
||||||
id := c.RequiredParamString("id", "Required id parameter is missing")
|
ids := c.RequiredParamStrings("id", "Required id parameter is missing")
|
||||||
time := c.ParamTime("time", time.Now())
|
times := c.ParamTimes("time")
|
||||||
submission := c.ParamBool("submission", false)
|
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(?)
|
playerId := 1 // TODO Multiple players, based on playerName/username/clientIP(?)
|
||||||
playerName := c.ParamString("c")
|
playerName := c.ParamString("c")
|
||||||
username := c.ParamString("u")
|
username := c.ParamString("u")
|
||||||
|
|
||||||
skip, err := c.scrobbler.DetectSkipped(playerId, id, submission)
|
for i := range ids {
|
||||||
if err != nil {
|
var t time.Time
|
||||||
beego.Error("Error detecting skip:", err)
|
if len(times) > 0 {
|
||||||
}
|
t = times[i]
|
||||||
if skip {
|
} else {
|
||||||
beego.Info("Skipped previous song")
|
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 {
|
if submission {
|
||||||
mf, err := c.scrobbler.Register(playerId, id, time)
|
mf, err := c.scrobbler.Register(playerId, ids[i], t)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
beego.Error("Error scrobbling:", err)
|
beego.Error("Error scrobbling:", err)
|
||||||
c.SendError(responses.ERROR_GENERIC, "Internal error")
|
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 {
|
} else {
|
||||||
mf, err := c.scrobbler.NowPlaying(playerId, id, username, playerName)
|
mf, err := c.scrobbler.NowPlaying(playerId, ids[i], username, playerName)
|
||||||
if err != nil {
|
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")
|
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()
|
response := c.NewEmpty()
|
||||||
c.SendResponse(response)
|
c.SendResponse(response)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user