aboutsummaryrefslogtreecommitdiffstats
path: root/apiv1.go
diff options
context:
space:
mode:
Diffstat (limited to 'apiv1.go')
-rw-r--r--apiv1.go41
1 files changed, 35 insertions, 6 deletions
diff --git a/apiv1.go b/apiv1.go
index e555a18..1f96819 100644
--- a/apiv1.go
+++ b/apiv1.go
@@ -177,15 +177,14 @@ func (app *App) scrobbleHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "BADSESSION")
return
}
- scrobbles, _ := parseScrobbles(r.Form, session)
+ scrobbles, _ := parseScrobbles(r.Form, session)
for i, s := range scrobbles {
app.GetSong(&s)
scrobbles[i] = s
}
- err = app.PutScrobbles(scrobbles)
- if err != nil {
+ if err := app.PutScrobbles(scrobbles); err != nil {
log.Println(err)
fmt.Fprintln(w, "Failed Database")
return
@@ -193,10 +192,40 @@ func (app *App) scrobbleHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "OK")
}
+func parseNowPlaying(values url.Values) (Scrobble, error) {
+ var scrobble Scrobble
+ scrobble.TrackName = NewCorrectable(values.Get("t"))
+ scrobble.Artist = NewCorrectable(values.Get("a"))
+ scrobble.Album = NewCorrectable(values.Get("b"))
+ scrobble.Mbid = values.Get("m")
+ if tn, err := strconv.Atoi(values.Get("n")); err != nil {
+ return scrobble, errors.New("Could not parse track number")
+ } else {
+ scrobble.TrackNumber = tn
+ }
+ if duration, err := strconv.Atoi(values.Get("l")); err != nil {
+ return scrobble, errors.New("Could not parse duration")
+ } else {
+ scrobble.Duration = duration
+ }
+ return scrobble, nil
+}
+
func (app *App) nowPlayingHandler(w http.ResponseWriter, r *http.Request) {
- if _, err := app.GetSession(r.FormValue("s")); err != nil {
+ session, err := app.GetSession(r.FormValue("s"))
+ if err != nil {
+ log.Println(err)
fmt.Fprintln(w, "BADSESSION")
- } else {
- fmt.Fprintln(w, "OK")
+ return
+ }
+ s, err := parseNowPlaying(r.Form)
+ s.SessionKey = session.Key
+ s.UserId = session.UserId
+ app.GetSong(&s)
+ if err := app.PutNowPlaying(s); err != nil {
+ log.Println(err)
+ fmt.Fprintln(w, "Failed Database")
+ return
}
+ fmt.Fprintln(w, "OK")
}