navidrome/api/media_annotation.go

114 lines
3.0 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/domain"
"github.com/deluan/gosonic/engine"
"github.com/deluan/gosonic/utils"
)
type MediaAnnotationController struct {
BaseAPIController
scrobbler engine.Scrobbler
2016-03-23 22:46:54 +01:00
ratings engine.Ratings
}
func (c *MediaAnnotationController) Prepare() {
2016-03-23 22:46:54 +01:00
utils.ResolveDependencies(&c.scrobbler, &c.ratings)
}
2016-03-24 00:37:48 +01:00
func (c *MediaAnnotationController) SetRating() {
id := c.RequiredParamString("id", "Required id parameter is missing")
rating := c.RequiredParamInt("rating", "Required rating parameter is missing")
beego.Debug("Setting rating", rating, "for id", id)
err := c.ratings.SetRating(id, rating)
switch {
case err == domain.ErrNotFound:
beego.Error(err)
c.SendError(responses.ErrorDataNotFound, "Id not found")
case err != nil:
beego.Error(err)
c.SendError(responses.ErrorGeneric, "Internal Error")
}
c.SendEmptyResponse()
}
2016-03-23 22:46:54 +01:00
func (c *MediaAnnotationController) Star() {
ids := c.RequiredParamStrings("id", "Required id parameter is missing")
beego.Debug("Starring ids:", ids)
err := c.ratings.SetStar(true, ids...)
switch {
case err == domain.ErrNotFound:
2016-03-23 22:46:54 +01:00
beego.Error(err)
c.SendError(responses.ErrorDataNotFound, "Id not found")
2016-03-23 22:46:54 +01:00
case err != nil:
beego.Error(err)
c.SendError(responses.ErrorGeneric, "Internal Error")
}
c.SendEmptyResponse()
}
func (c *MediaAnnotationController) Unstar() {
ids := c.RequiredParamStrings("id", "Required id parameter is missing")
beego.Debug("Unstarring ids:", ids)
err := c.ratings.SetStar(false, ids...)
switch {
case err == domain.ErrNotFound:
2016-03-23 22:46:54 +01:00
beego.Error(err)
c.SendError(responses.ErrorDataNotFound, "Directory not found")
case err != nil:
beego.Error(err)
c.SendError(responses.ErrorGeneric, "Internal Error")
}
c.SendEmptyResponse()
}
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) {
2016-03-23 17:35:10 +01:00
c.SendError(responses.ErrorGeneric, fmt.Sprintf("Wrong number of timestamps: %d", len(times)))
2016-03-22 00:35:40 +01:00
}
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 15:28:13 +01:00
for i, id := range ids {
2016-03-22 00:35:40 +01:00
var t time.Time
if len(times) > 0 {
t = times[i]
} else {
t = time.Now()
}
2016-03-22 00:35:40 +01:00
if submission {
2016-03-22 15:28:13 +01:00
mf, err := c.scrobbler.Register(playerId, id, t)
2016-03-22 00:35:40 +01:00
if err != nil {
2016-03-22 15:28:13 +01:00
beego.Error("Error scrobbling", id, "-", err)
continue
2016-03-22 00:35:40 +01:00
}
2016-03-22 15:28:13 +01:00
beego.Info(fmt.Sprintf(`Scrobbled (%s) "%s" at %v`, id, mf.Title, t))
2016-03-22 00:35:40 +01:00
} else {
mf, err := c.scrobbler.NowPlaying(playerId, playerName, id, username)
2016-03-22 00:35:40 +01:00
if err != nil {
2016-03-22 15:28:13 +01:00
beego.Error("Error setting", id, "as current song:", err)
continue
2016-03-22 00:35:40 +01:00
}
2016-03-22 15:28:13 +01:00
beego.Info(fmt.Sprintf(`Now Playing (%s) "%s" at %v`, id, mf.Title, t))
2016-03-17 02:04:41 +01:00
}
}
2016-03-23 22:46:54 +01:00
c.SendEmptyResponse()
}