aboutsummaryrefslogtreecommitdiffstats
path: root/data.go
diff options
context:
space:
mode:
Diffstat (limited to 'data.go')
-rw-r--r--data.go53
1 files changed, 34 insertions, 19 deletions
diff --git a/data.go b/data.go
index 065e891..4e28750 100644
--- a/data.go
+++ b/data.go
@@ -4,6 +4,7 @@ import (
"database/sql"
"encoding/xml"
"fmt"
+ "log"
"time"
_ "github.com/mattn/go-sqlite3"
@@ -16,10 +17,11 @@ type Scrobble struct {
Album Correctable `xml:"album" json:"album"`
TrackNumber int `xml:"-" json:"-"`
Duration int `xml:"-" json:"-"`
- Time int `xml:"timestamp" json:"timestamp,string"`
+ Time time.Time `xml:"timestamp" json:"timestamp,string"`
Chosen bool `xml:"-" json:"-"`
Mbid string `xml:"-" json:"-"`
Session string `xml:"-" json:"-"`
+ Image string
}
type Session struct {
@@ -28,7 +30,7 @@ type Session struct {
Key string `json:"key" xml:"key"`
Client string
Protocol string
- Created int64
+ Created time.Time
Subscriber int64 `json:"subscriber" xml:"subscriber"`
}
@@ -37,6 +39,7 @@ type DataStore interface {
GetSession(key string) (*Session, error)
GetPassword(userName string) (string, error)
PutScrobbles([]Scrobble)
+ RecentScrobbles(lfmName string) []*Scrobble
Api
}
@@ -44,43 +47,34 @@ type SqlStore struct {
*sql.DB
}
-func NewSqlStore() *SqlStore {
- db, err := sql.Open("sqlite3", "./test.db")
- if err != nil {
- fmt.Println(err)
- }
- err = db.Ping()
- if err != nil {
- fmt.Println(err)
- }
- return &SqlStore{db}
-}
-
func NewSession(user string, client string, protocol string) *Session {
return &Session{
User: user,
Key: randomToken(16),
Client: client,
Protocol: protocol,
- Created: time.Now().Unix(),
+ Created: time.Now(),
}
}
func (store *SqlStore) PutSession(s *Session) {
- store.Exec("INSERT INTO sessions VALUES (?, ?, ?, ?, ?)",
+ _, 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) GetSession(key string) (*Session, error) {
s := &Session{}
- row := store.QueryRow("SELECT * FROM sessions WHERE key = ?", key)
+ row := store.QueryRow("SELECT * FROM scrobbling_sessions WHERE key = $1", key)
err := row.Scan(&s.User, &s.Key, &s.Client, &s.Protocol, &s.Created)
return s, err
}
func (store *SqlStore) GetPassword(name string) (string, error) {
var password string
- row := store.QueryRow("SELECT password FROM users WHERE name = ?", name)
+ row := store.QueryRow("SELECT lfm_password FROM users WHERE lfm_name = $1", name)
err := row.Scan(&password)
return password, err
}
@@ -88,7 +82,7 @@ func (store *SqlStore) GetPassword(name string) (string, error) {
func (store *SqlStore) PutScrobbles(scrobbles []Scrobble) {
for _, s := range scrobbles {
if _, err := store.Exec(
- "INSERT INTO scrobbles VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
+ "INSERT INTO scrobbles VALUES ($1, $2, $3, $4,$5, $6, $7, $8, $9, $10)",
s.Artist,
s.AlbumArtist,
s.TrackName,
@@ -104,3 +98,24 @@ func (store *SqlStore) PutScrobbles(scrobbles []Scrobble) {
}
}
}
+
+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)
+ if err != nil {
+ log.Println(err)
+ }
+
+ for rows.Next() {
+ scrobble := new(Scrobble)
+ rows.Scan(&scrobble.Artist.Name, &scrobble.Album.Name,
+ &scrobble.TrackName.Name, &scrobble.Time, &scrobble.Image)
+ scrobbles = append(scrobbles, scrobble)
+ }
+ rows.Close()
+ return scrobbles
+}