2016-03-11 20:49:01 -05:00
|
|
|
package api
|
|
|
|
|
|
|
|
|
|
import (
|
2016-03-15 17:06:23 -04:00
|
|
|
"fmt"
|
2016-03-11 20:49:01 -05:00
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/astaxie/beego"
|
|
|
|
|
"github.com/deluan/gosonic/api/responses"
|
2016-03-23 18:15:14 -04:00
|
|
|
"github.com/deluan/gosonic/domain"
|
2016-03-16 17:48:44 -04:00
|
|
|
"github.com/deluan/gosonic/engine"
|
2016-03-11 20:49:01 -05:00
|
|
|
"github.com/deluan/gosonic/utils"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type MediaAnnotationController struct {
|
|
|
|
|
BaseAPIController
|
2016-03-16 17:48:44 -04:00
|
|
|
scrobbler engine.Scrobbler
|
2016-03-23 17:46:54 -04:00
|
|
|
ratings engine.Ratings
|
2016-03-11 20:49:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *MediaAnnotationController) Prepare() {
|
2016-03-23 17:46:54 -04:00
|
|
|
utils.ResolveDependencies(&c.scrobbler, &c.ratings)
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-23 19:37:48 -04: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 17:46:54 -04: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 {
|
2016-03-23 18:15:14 -04:00
|
|
|
case err == domain.ErrNotFound:
|
2016-03-23 17:46:54 -04:00
|
|
|
beego.Error(err)
|
2016-03-23 18:15:14 -04:00
|
|
|
c.SendError(responses.ErrorDataNotFound, "Id not found")
|
2016-03-23 17:46:54 -04: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 {
|
2016-03-23 18:15:14 -04:00
|
|
|
case err == domain.ErrNotFound:
|
2016-03-23 17:46:54 -04: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()
|
2016-03-11 20:49:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *MediaAnnotationController) Scrobble() {
|
2016-03-21 19:35:40 -04: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 12:35:10 -04:00
|
|
|
c.SendError(responses.ErrorGeneric, fmt.Sprintf("Wrong number of timestamps: %d", len(times)))
|
2016-03-21 19:35:40 -04:00
|
|
|
}
|
|
|
|
|
submission := c.ParamBool("submission", true)
|
2016-03-17 18:58:09 -04:00
|
|
|
playerId := 1 // TODO Multiple players, based on playerName/username/clientIP(?)
|
2016-03-17 10:37:19 -04:00
|
|
|
playerName := c.ParamString("c")
|
|
|
|
|
username := c.ParamString("u")
|
2016-03-17 09:34:32 -04:00
|
|
|
|
2016-03-21 19:51:22 -04:00
|
|
|
beego.Debug("Scrobbling ids:", ids, "times:", times, "submission:", submission)
|
2016-03-22 10:28:13 -04:00
|
|
|
for i, id := range ids {
|
2016-03-21 19:35:40 -04:00
|
|
|
var t time.Time
|
|
|
|
|
if len(times) > 0 {
|
|
|
|
|
t = times[i]
|
|
|
|
|
} else {
|
|
|
|
|
t = time.Now()
|
2016-03-11 20:49:01 -05:00
|
|
|
}
|
2016-03-21 19:35:40 -04:00
|
|
|
if submission {
|
2016-03-22 10:28:13 -04:00
|
|
|
mf, err := c.scrobbler.Register(playerId, id, t)
|
2016-03-21 19:35:40 -04:00
|
|
|
if err != nil {
|
2016-03-22 10:28:13 -04:00
|
|
|
beego.Error("Error scrobbling", id, "-", err)
|
2016-03-21 20:08:35 -04:00
|
|
|
continue
|
2016-03-21 19:35:40 -04:00
|
|
|
}
|
2016-03-22 10:28:13 -04:00
|
|
|
beego.Info(fmt.Sprintf(`Scrobbled (%s) "%s" at %v`, id, mf.Title, t))
|
2016-03-21 19:35:40 -04:00
|
|
|
} else {
|
2016-03-23 10:06:26 -04:00
|
|
|
mf, err := c.scrobbler.NowPlaying(playerId, playerName, id, username)
|
2016-03-21 19:35:40 -04:00
|
|
|
if err != nil {
|
2016-03-22 10:28:13 -04:00
|
|
|
beego.Error("Error setting", id, "as current song:", err)
|
2016-03-21 20:08:35 -04:00
|
|
|
continue
|
2016-03-21 19:35:40 -04:00
|
|
|
}
|
2016-03-22 10:28:13 -04:00
|
|
|
beego.Info(fmt.Sprintf(`Now Playing (%s) "%s" at %v`, id, mf.Title, t))
|
2016-03-16 21:04:41 -04:00
|
|
|
}
|
2016-03-11 20:49:01 -05:00
|
|
|
}
|
2016-03-23 17:46:54 -04:00
|
|
|
c.SendEmptyResponse()
|
2016-03-11 20:49:01 -05:00
|
|
|
}
|