diff options
Diffstat (limited to 'apiv1.go')
| -rw-r--r-- | apiv1.go | 58 |
1 files changed, 37 insertions, 21 deletions
@@ -42,15 +42,25 @@ func (app *App) mainHandler(w http.ResponseWriter, r *http.Request) { return } - client := r.FormValue("c") - s := NewSession(user, client, protocol) - app.PutSession(s) + s := &Session{ + User: user, + Client: r.FormValue("c"), + ClientVersion: r.FormValue("v"), + Key: randomToken(16), + Protocol: protocol, + } + if err := app.PutSession(s); err != nil { + log.Println(err) + fmt.Fprintln(w, "FAILED Couldn't create session") + return + } + fmt.Fprintln(w, "OK") - fmt.Fprintf(w, "%s\n", s.Key) + fmt.Fprintln(w, s.Key) fmt.Fprintln(w, "http://post.audioscrobbler.com:80/np") fmt.Fprintln(w, "http://post.audioscrobbler.com:80/scrobble") } else { - fmt.Fprintf(w, "<html>This is an endpoint, see <a href=\"http://www.last.fm/api/submissions\">here</a></html>") + fmt.Fprintf(w, "api") } } @@ -160,27 +170,33 @@ func parseScrobbles(values url.Values, session *Session) ([]Scrobble, int) { } func (app *App) scrobbleHandler(w http.ResponseWriter, r *http.Request) { - if session, 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 { - scrobbles, _ := parseScrobbles(r.Form, session) + return + } + scrobbles, _ := parseScrobbles(r.Form, session) - for i, s := range scrobbles { - t := app.TrackInfo(s.TrackName.Name, s.Artist.Name) - s.Mbid = t.Mbid - scrobbles[i] = s - _, err = app.DB.Exec("INSERT INTO songs VALUES ($1, $2) "+ - "ON CONFLICT (mbid) DO UPDATE SET mbid=$1, image=$2", - t.Mbid, t.GetImage("medium")) - if err != nil { - log.Println(err) - } + for i, s := range scrobbles { + t := app.TrackInfo(s.TrackName.Name, s.Artist.Name) + s.MbidComp = t.Mbid + scrobbles[i] = s + _, err = app.DB.Exec("INSERT INTO songs VALUES ($1, $2) "+ + "ON CONFLICT (mbid) DO UPDATE SET mbid=$1, image=$2", + t.Mbid, t.GetImage("medium")) + if err != nil { + log.Println(err) } + } - fmt.Printf("%v\n", scrobbles) - app.PutScrobbles(scrobbles) - fmt.Fprintln(w, "OK") + err = app.PutScrobbles(scrobbles) + if err != nil { + log.Println(err) + fmt.Fprintln(w, "Failed Database") + return } + fmt.Fprintln(w, "OK") } func (app *App) nowPlayingHandler(w http.ResponseWriter, r *http.Request) { |
