diff options
| author | Guillaume Horel <guillaume.horel@gmail.com> | 2016-12-11 21:01:12 -0500 |
|---|---|---|
| committer | Guillaume Horel <guillaume.horel@gmail.com> | 2016-12-11 21:01:12 -0500 |
| commit | 5396b376d5979d9f2b33dc065be8a54991e83b0b (patch) | |
| tree | 7fa113869a5925e8acc47ae9f628cedf2ed3b4ba /main.go | |
| parent | 590429cdba3596fc60fa051633428a49002b14cb (diff) | |
| download | lastfm-api-5396b376d5979d9f2b33dc065be8a54991e83b0b.tar.gz | |
parse api 2.0 correctly
Diffstat (limited to 'main.go')
| -rw-r--r-- | main.go | 75 |
1 files changed, 62 insertions, 13 deletions
@@ -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) } |
