From ff81576e21f5b89cbf47856c520df3e5e0c9adbe Mon Sep 17 00:00:00 2001 From: Thibaut Horel Date: Sun, 18 Jun 2017 18:18:36 -0400 Subject: Import listens from lastfm --- data.go | 100 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 94 insertions(+), 6 deletions(-) (limited to 'data.go') diff --git a/data.go b/data.go index 5f1f0fb..3b4a783 100644 --- a/data.go +++ b/data.go @@ -46,6 +46,14 @@ type Song struct { Image string } +type Import struct { + LfmName string + From time.Time + To time.Time + LastFetch time.Time + Done bool +} + type DataStore interface { PutSession(*Session) error GetSession(key string) (*Session, error) @@ -62,6 +70,12 @@ type DataStore interface { InsertUser(u *User) error SaveUser(u *User) error + GetImport(i *Import) error + SaveImport(i *Import) error + InsertImport(i *Import) error + NewImport(name string) *Import + ImportStats(user int) (time.Time, int, error) + InsertUserSession(s *UserSession) error Api } @@ -179,12 +193,18 @@ func (store *SqlStore) NowPlaying(userId int) *Scrobble { } func (store *SqlStore) GetSongId(s *Song) error { - query := ` - SELECT song_id FROM songs - WHERE artist=$1 AND album=$2 AND name=$3 - ` - row := store.QueryRow(query, s.Artist, s.Album, s.Name) - return row.Scan(&s.Id) + var query string + if s.Mbid != "" { + query = `SELECT song_id FROM songs WHERE mbid=$1` + row := store.QueryRow(query, s.Mbid) + return row.Scan(&s.Id) + } else { + query = ` + SELECT song_id FROM songs + WHERE artist=$1 AND album=$2 AND name=$3` + row := store.QueryRow(query, s.Artist, s.Album, s.Name) + return row.Scan(&s.Id) + } } func (store *SqlStore) InsertSong(s *Song) error { @@ -245,3 +265,71 @@ func (store *SqlStore) InsertUserSession(s *UserSession) error { _, err := store.Exec(query, &s.Id, &s.UserId) return err } + +func (store *SqlStore) GetImport(i *Import) error { + query := ` + SELECT last_fetch + FROM scrobble_import WHERE lfm_name = $1 and "from"=$2 and "to"=$3` + row := store.QueryRow(query, i.LfmName, i.From, i.To) + return row.Scan(&i.LastFetch) +} + +func (store *SqlStore) SaveImport(i *Import) error { + query := `UPDATE scrobble_import SET last_fetch=$1, done=$5 + WHERE "from"=$2 and "to"=$3 and lfm_name=$4` + _, err := store.Exec(query, i.LastFetch, i.From, i.To, i.LfmName, i.Done) + return err +} + +func (store *SqlStore) InsertImport(i *Import) error { + query := ` + INSERT into scrobble_import (lfm_name, "from", "to", last_fetch, done) + VALUES ($1, $2, $3, $4, $5)` + _, err := store.Exec(query, i.LfmName, i.From, i.To, i.LastFetch, i.Done) + return err +} + +func (store *SqlStore) NewImport(name string) *Import { + i := &Import{ + LfmName: name, + To: time.Now(), + } + query := `SELECT max("to") FROM scrobble_import WHERE lfm_name=$1` + row := store.QueryRow(query, i.LfmName) + if row.Scan(&i.From) == nil { + i.From = i.From.Add(time.Second) + } + if err := store.InsertImport(i); err != nil { + log.Println(err) + } + return i +} + +func (store *SqlStore) ImportStats(user int) (time.Time, int, error) { + query := ` + SELECT "last", ct + FROM ( + SELECT max("to") "last", lfm_name + FROM scrobble_import + GROUP BY lfm_name + ) i + JOIN ( + SELECT count(*) ct, max(lfm_name) n + FROM scrobbles + JOIN scrobbling_sessions + ON scrobbles.session_key = scrobbling_sessions.session_key + JOIN users + ON scrobbles.user_id = users.user_id + WHERE users.user_id = $1 + AND client = 'import' + ) s + ON lfm_name = n` + var t time.Time + var count int + row := store.QueryRow(query, user) + if err := row.Scan(&t, &count); err != nil { + return t, count, err + } else { + return t, count, nil + } +} -- cgit v1.2.3-70-g09d2