From 858de1edf50cc1128f6b621a5413b2975ca446eb Mon Sep 17 00:00:00 2001 From: Thibaut Horel Date: Sat, 17 Jun 2017 12:49:28 -0400 Subject: Clean fecthing of song metadata from Last.fm --- data.go | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) (limited to 'data.go') diff --git a/data.go b/data.go index b97d4a4..5f1f0fb 100644 --- a/data.go +++ b/data.go @@ -35,6 +35,17 @@ type Session struct { Subscriber int64 `json:"subscriber" xml:"subscriber"` } +type Song struct { + Id int + Artist string + Album string + Name string + TrackNumber int + Duration int + Mbid string + Image string +} + type DataStore interface { PutSession(*Session) error GetSession(key string) (*Session, error) @@ -42,8 +53,9 @@ type DataStore interface { PutNowPlaying(s Scrobble) error NowPlaying(userId int) *Scrobble RecentScrobbles(userId int) []*Scrobble - GetSongId(artist, album, name string) (int, error) - InsertSong(s *Scrobble) (int, error) + + GetSongId(s *Song) error + InsertSong(s *Song) error GetScrobblingUser(lfmName string) (int, string, error) GetUser(u *User) error @@ -166,28 +178,23 @@ func (store *SqlStore) NowPlaying(userId int) *Scrobble { return scrobble } -func (store *SqlStore) GetSongId(artist, album, song string) (int, error) { +func (store *SqlStore) GetSongId(s *Song) error { query := ` SELECT song_id FROM songs WHERE artist=$1 AND album=$2 AND name=$3 ` - var id int - row := store.QueryRow(query, artist, album, song) - err := row.Scan(&id) - return id, err - + row := store.QueryRow(query, s.Artist, s.Album, s.Name) + return row.Scan(&s.Id) } -func (store *SqlStore) InsertSong(s *Scrobble) (int, error) { +func (store *SqlStore) InsertSong(s *Song) error { query := ` INSERT INTO songs (artist, album, name, track_number, duration, mbid, image) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING song_id` - var id int - row := store.QueryRow(query, s.Artist, s.Album, s.TrackName, s.TrackNumber, - s.Duration*1000, s.Mbid, s.Image) - err := row.Scan(&id) - return id, err + row := store.QueryRow(query, s.Artist, s.Album, s.Name, s.TrackNumber, + s.Duration, s.Mbid, s.Image) + return row.Scan(&s.Id) } func (store *SqlStore) InsertUser(user *User) error { -- cgit v1.2.3-70-g09d2