add scrobblingt
This commit is contained in:
@@ -84,9 +84,9 @@ type templateData struct {
|
||||
User *db.User
|
||||
SelectedUser *db.User
|
||||
AllUsers []*db.User
|
||||
ArtistCount uint
|
||||
AlbumCount uint
|
||||
TrackCount uint
|
||||
ArtistCount int
|
||||
AlbumCount int
|
||||
TrackCount int
|
||||
CurrentLastFMAPIKey string
|
||||
CurrentLastFMAPISecret string
|
||||
RequestRoot string
|
||||
@@ -96,6 +96,14 @@ func getStrParam(r *http.Request, key string) string {
|
||||
return r.URL.Query().Get(key)
|
||||
}
|
||||
|
||||
func getStrParamOr(r *http.Request, key, or string) string {
|
||||
val := getStrParam(r, key)
|
||||
if val == "" {
|
||||
return or
|
||||
}
|
||||
return val
|
||||
}
|
||||
|
||||
func getIntParam(r *http.Request, key string) (int, error) {
|
||||
strVal := r.URL.Query().Get(key)
|
||||
if strVal == "" {
|
||||
@@ -156,7 +164,7 @@ func respond(w http.ResponseWriter, r *http.Request,
|
||||
}
|
||||
|
||||
func respondError(w http.ResponseWriter, r *http.Request,
|
||||
code uint64, message string) {
|
||||
code int, message string) {
|
||||
respondRaw(w, r, http.StatusBadRequest, subsonic.NewError(
|
||||
code, message,
|
||||
))
|
||||
|
||||
@@ -4,10 +4,12 @@ import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"time"
|
||||
"unicode"
|
||||
|
||||
"github.com/jinzhu/gorm"
|
||||
"github.com/sentriz/gonic/db"
|
||||
"github.com/sentriz/gonic/lastfm"
|
||||
"github.com/sentriz/gonic/subsonic"
|
||||
|
||||
"github.com/mozillazg/go-unidecode"
|
||||
@@ -210,6 +212,51 @@ func (c *Controller) GetLicence(w http.ResponseWriter, r *http.Request) {
|
||||
respond(w, r, sub)
|
||||
}
|
||||
|
||||
func (c *Controller) Scrobble(w http.ResponseWriter, r *http.Request) {
|
||||
id, err := getIntParam(r, "id")
|
||||
if err != nil {
|
||||
respondError(w, r, 10, "please provide an `id` parameter")
|
||||
return
|
||||
}
|
||||
// fetch user to get lastfm session
|
||||
username := getStrParam(r, "u")
|
||||
user := c.GetUserFromName(username)
|
||||
if user == nil {
|
||||
respondError(w, r, 10, "could not find a user with that name")
|
||||
return
|
||||
}
|
||||
if user.LastFMSession == "" {
|
||||
respondError(w, r, 0, fmt.Sprintf("no last.fm session for this user: %v", err))
|
||||
return
|
||||
}
|
||||
// fetch track for getting info to send to last.fm function
|
||||
var track db.Track
|
||||
c.DB.
|
||||
Preload("Album").
|
||||
Preload("AlbumArtist").
|
||||
First(&track, id)
|
||||
// get time from args or use now
|
||||
time := getIntParamOr(r, "time", int(time.Now().Unix()))
|
||||
// get submission, where the default is true. we will
|
||||
// check if it's false later
|
||||
submission := getStrParamOr(r, "submission", "true")
|
||||
// scrobble with above info
|
||||
err = lastfm.Scrobble(
|
||||
c.GetSetting("lastfm_api_key"),
|
||||
c.GetSetting("lastfm_secret"),
|
||||
user.LastFMSession,
|
||||
&track,
|
||||
time,
|
||||
submission != "false",
|
||||
)
|
||||
if err != nil {
|
||||
respondError(w, r, 0, fmt.Sprintf("error when submitting: %v", err))
|
||||
return
|
||||
}
|
||||
sub := subsonic.NewResponse()
|
||||
respond(w, r, sub)
|
||||
}
|
||||
|
||||
func (c *Controller) NotFound(w http.ResponseWriter, r *http.Request) {
|
||||
respondError(w, r, 0, "unknown route")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user