summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGuillaume Horel <guillaume.horel@gmail.com>2016-12-11 21:01:12 -0500
committerGuillaume Horel <guillaume.horel@gmail.com>2016-12-11 21:01:12 -0500
commit5396b376d5979d9f2b33dc065be8a54991e83b0b (patch)
tree7fa113869a5925e8acc47ae9f628cedf2ed3b4ba
parent590429cdba3596fc60fa051633428a49002b14cb (diff)
downloadlastfm-api-5396b376d5979d9f2b33dc065be8a54991e83b0b.tar.gz
parse api 2.0 correctly
-rw-r--r--main.go75
1 files changed, 62 insertions, 13 deletions
diff --git a/main.go b/main.go
index 4d8f111..787c811 100644
--- a/main.go
+++ b/main.go
@@ -7,6 +7,7 @@ import (
"net/http/httputil"
"net/url"
"strconv"
+ "strings"
"time"
)
@@ -56,36 +57,46 @@ func mainHandler(ds DataStore, w http.ResponseWriter, r *http.Request) {
func parseValues(values url.Values) map[int]url.Values {
parts := make(map[int]url.Values)
var field string
- var idx int
for key, value := range values {
- if _, err := fmt.Sscanf(key, "%s[%d]", &field, &idx); err != nil {
- parts[idx][field] = append(parts[idx][field], value...)
+ splitted_key := strings.SplitN(key, "[", 2)
+ if len(splitted_key) < 2 {
+ continue
+ }
+ field = splitted_key[0]
+ idx, err := strconv.Atoi(strings.TrimSuffix(splitted_key[1], "]"))
+ if err != nil {
+ continue
+ } else {
+ if _, ok := parts[idx]; !ok {
+ parts[idx] = make(url.Values)
+ }
+ parts[idx][field] = value
}
}
return parts
}
-func parsePart(values url.Values) (Scrobble, error) {
+func parsePart1(values url.Values) (Scrobble, error) {
var scrobble Scrobble
scrobble.TrackName = NewCorrectable(values.Get("t"))
scrobble.Artist = NewCorrectable(values.Get("a"))
- time, err := strconv.Atoi(values.Get("i"))
- if err != nil {
+ if time, err := strconv.Atoi(values.Get("i")); err != nil {
return scrobble, errors.New("Could not parse timestamp")
+ } else {
+ scrobble.Time = time
}
- scrobble.Time = time
scrobble.Album = NewCorrectable(values.Get("b"))
scrobble.Mbid = values.Get("m")
- tn, err := strconv.Atoi(values.Get("n"))
- if err != nil {
+ if tn, err := strconv.Atoi(values.Get("n")); err != nil {
return scrobble, errors.New("Could not parse track number")
+ } else {
+ scrobble.TrackNumber = tn
}
- scrobble.TrackNumber = tn
- duration, err := strconv.Atoi(values.Get("l"))
- if err != nil {
+ if duration, err := strconv.Atoi(values.Get("l")); err != nil {
return scrobble, errors.New("Could not parse duration")
+ } else {
+ scrobble.Duration = duration
}
- scrobble.Duration = duration
chosen := values.Get("o")
if chosen == "P" || chosen == "" {
scrobble.Chosen = true
@@ -93,13 +104,51 @@ func parsePart(values url.Values) (Scrobble, error) {
return scrobble, nil
}
+func parsePart2(values url.Values) (Scrobble, error) {
+ var scrobble Scrobble
+ scrobble.TrackName = NewCorrectable(values.Get("track"))
+ scrobble.Artist = NewCorrectable(values.Get("artist"))
+ if time, err := strconv.Atoi(values.Get("timestamp")); err != nil {
+ return scrobble, errors.New("Could not parse timestamp")
+ } else {
+ scrobble.Time = time
+ }
+ scrobble.Album = NewCorrectable(values.Get("album"))
+ scrobble.Mbid = values.Get("mbid")
+ if tn, ok := values["trackNumber"]; ok {
+ if tn, err := strconv.Atoi(tn[0]); err != nil {
+ return scrobble, errors.New("Could not parse track number")
+ } else {
+ scrobble.TrackNumber = tn
+ }
+ }
+ if duration, err := strconv.Atoi(values.Get("duration")); err != nil {
+ return scrobble, errors.New("Could not parse duration")
+ } else {
+ scrobble.Duration = duration
+ }
+ chosen := values.Get("chosenByUser")
+ if chosen == "1" || chosen == "" {
+ scrobble.Chosen = true
+ }
+ return scrobble, nil
+}
+
func parseScrobbles(values url.Values, session *Session) []Scrobble {
scrobbles := make([]Scrobble, 0, 1)
parts := parseValues(values)
+ var parsePart func(url.Values) (Scrobble, error)
+ if session.Protocol == "2.0" {
+ parsePart = parsePart2
+ } else {
+ parsePart = parsePart1
+ }
for i, c := 0, 0; i < 50 && c < len(parts); i++ {
if part, ok := parts[i]; ok {
c++
if scrobble, err := parsePart(part); err != nil {
+ fmt.Printf("%v\n", err)
+ } else {
scrobble.Session = session.Key
scrobbles = append(scrobbles, scrobble)
}