aboutsummaryrefslogtreecommitdiffstats
path: root/data.go
diff options
context:
space:
mode:
Diffstat (limited to 'data.go')
-rw-r--r--data.go104
1 files changed, 54 insertions, 50 deletions
diff --git a/data.go b/data.go
index 4e28750..318e43e 100644
--- a/data.go
+++ b/data.go
@@ -3,7 +3,6 @@ package main
import (
"database/sql"
"encoding/xml"
- "fmt"
"log"
"time"
@@ -20,25 +19,28 @@ type Scrobble struct {
Time time.Time `xml:"timestamp" json:"timestamp,string"`
Chosen bool `xml:"-" json:"-"`
Mbid string `xml:"-" json:"-"`
- Session string `xml:"-" json:"-"`
+ MbidComp string
+ Session string `xml:"-" json:"-"`
+ User string
Image string
}
type Session struct {
- XMLName xml.Name `json:"-" xml:"session"`
- User string `json:"name" xml:"name"`
- Key string `json:"key" xml:"key"`
- Client string
- Protocol string
- Created time.Time
- Subscriber int64 `json:"subscriber" xml:"subscriber"`
+ XMLName xml.Name `json:"-" xml:"session"`
+ User string `json:"name" xml:"name"`
+ Key string `json:"key" xml:"key"`
+ Client string
+ ClientVersion string
+ Protocol string
+ Created time.Time
+ Subscriber int64 `json:"subscriber" xml:"subscriber"`
}
type DataStore interface {
- PutSession(*Session)
+ PutSession(*Session) error
GetSession(key string) (*Session, error)
GetPassword(userName string) (string, error)
- PutScrobbles([]Scrobble)
+ PutScrobbles([]Scrobble) error
RecentScrobbles(lfmName string) []*Scrobble
Api
}
@@ -47,65 +49,67 @@ type SqlStore struct {
*sql.DB
}
-func NewSession(user string, client string, protocol string) *Session {
- return &Session{
- User: user,
- Key: randomToken(16),
- Client: client,
- Protocol: protocol,
- Created: time.Now(),
- }
-}
-
-func (store *SqlStore) PutSession(s *Session) {
- _, err := store.Exec("INSERT INTO scrobbling_sessions VALUES ($1, $2, $3, $4, $5)",
- s.User, s.Key, s.Client, s.Protocol, s.Created)
- if err != nil {
- log.Println(err)
- }
+func (store *SqlStore) PutSession(s *Session) error {
+ query := `
+ INSERT INTO scrobbling_sessions (lfm_name, session_key, client, client_version, protocol)
+ VALUES ($1, $2, $3, $4, $5)`
+ _, err := store.Exec(query, s.User, s.Key, s.Client, s.ClientVersion, s.Protocol)
+ return err
}
func (store *SqlStore) GetSession(key string) (*Session, error) {
+ query := `
+ SELECT lfm_name, session_key, client, client_version, protocol, created
+ FROM scrobbling_sessions WHERE session_key = $1`
+ row := store.QueryRow(query, key)
s := &Session{}
- row := store.QueryRow("SELECT * FROM scrobbling_sessions WHERE key = $1", key)
- err := row.Scan(&s.User, &s.Key, &s.Client, &s.Protocol, &s.Created)
+ err := row.Scan(&s.User, &s.Key, &s.Client, &s.ClientVersion, &s.Protocol,
+ &s.Created)
return s, err
}
func (store *SqlStore) GetPassword(name string) (string, error) {
var password string
- row := store.QueryRow("SELECT lfm_password FROM users WHERE lfm_name = $1", name)
+ row := store.QueryRow("SELECT lfm_password FROM users WHERE lfm_name = $1",
+ name)
err := row.Scan(&password)
return password, err
}
-func (store *SqlStore) PutScrobbles(scrobbles []Scrobble) {
+func (store *SqlStore) PutScrobbles(scrobbles []Scrobble) error {
+ tx, err := store.Begin()
+ if err != nil {
+ return err
+ }
+ query := `
+ INSERT INTO scrobbles
+ (artist, albumartist, trackname, album, tracknumber, duration, time,
+ chosen, mbid, mbid_computed, session_key, lfm_name)
+ VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12)`
+ st, err := tx.Prepare(query)
+ if err != nil {
+ return err
+ }
for _, s := range scrobbles {
- if _, err := store.Exec(
- "INSERT INTO scrobbles VALUES ($1, $2, $3, $4,$5, $6, $7, $8, $9, $10)",
- s.Artist,
- s.AlbumArtist,
- s.TrackName,
- s.Album,
- s.TrackNumber,
- s.Duration,
- s.Time,
- s.Chosen,
- s.Mbid,
- s.Session,
- ); err != nil {
- fmt.Printf("error : %v\n", err)
+ _, err = st.Exec(s.Artist, s.AlbumArtist, s.TrackName, s.Album,
+ s.TrackNumber, s.Duration, s.Time, s.Chosen, s.Mbid, s.MbidComp,
+ s.Session, s.User)
+ if err != nil {
+ tx.Rollback()
+ return err
}
}
+ return tx.Commit()
}
func (store *SqlStore) RecentScrobbles(lfmName string) []*Scrobble {
scrobbles := make([]*Scrobble, 0, 10)
- rows, err := store.Query("SELECT artist, album, trackname, time, image "+
- "FROM scrobbles JOIN scrobbling_sessions ON session = key "+
- "LEFT JOIN songs ON scrobbles.mbid = songs.mbid "+
- "WHERE lfm_name=$1 ORDER BY time DESC LIMIT 10",
- lfmName)
+ query := `
+ SELECT artist, album, trackname, time, image
+ FROM scrobbles
+ LEFT JOIN songs ON COALESCE(scrobbles.mbid, scrobbles.mbid_computed) = songs.mbid
+ WHERE lfm_name=$1 ORDER BY time DESC LIMIT 10`
+ rows, err := store.Query(query, lfmName)
if err != nil {
log.Println(err)
}