From 3543fcb861a122fe3405bf42f83f6dbc56888b8c Mon Sep 17 00:00:00 2001 From: Thibaut Horel Date: Wed, 7 Jun 2017 01:15:43 -0400 Subject: Get additional song metadata from last.fm --- data.go | 45 ++++++++++++++++++++++++++++++++++++--------- 1 file changed, 36 insertions(+), 9 deletions(-) (limited to 'data.go') diff --git a/data.go b/data.go index 373dddb..9e5f5d5 100644 --- a/data.go +++ b/data.go @@ -19,8 +19,8 @@ type Scrobble struct { Time time.Time `xml:"timestamp" json:"timestamp,string"` Chosen bool `xml:"-" json:"-"` Mbid string `xml:"-" json:"-"` - MbidComp string - Session string `xml:"-" json:"-"` + SongId int + SessionKey string `xml:"-" json:"-"` UserId int Image string } @@ -43,6 +43,8 @@ type DataStore interface { GetUser(lfmName string) (int, string, error) PutScrobbles([]Scrobble) error RecentScrobbles(userId int) []*Scrobble + GetSongId(artist, album, name string) (int, error) + InsertSong(s *Scrobble) (int, error) Api } @@ -85,8 +87,8 @@ func (store *SqlStore) PutScrobbles(scrobbles []Scrobble) error { } query := ` INSERT INTO scrobbles - (artist, albumartist, trackname, album, tracknumber, duration, time, - chosen, mbid, mbid_computed, session_key, user_id) + (artist, album_artist, track_name, album, track_number, duration, time, + chosen, mbid, song_id, session_key, user_id) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12)` st, err := tx.Prepare(query) if err != nil { @@ -95,8 +97,8 @@ func (store *SqlStore) PutScrobbles(scrobbles []Scrobble) error { for _, s := range scrobbles { _, 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.UserId) + s.TrackNumber, s.Duration, s.Time, s.Chosen, s.Mbid, s.SongId, + s.SessionKey, s.UserId) if err != nil { tx.Rollback() return err @@ -108,13 +110,14 @@ func (store *SqlStore) PutScrobbles(scrobbles []Scrobble) error { func (store *SqlStore) RecentScrobbles(userId int) []*Scrobble { scrobbles := make([]*Scrobble, 0, 10) query := ` - SELECT artist, album, trackname, time, image - FROM scrobbles - LEFT JOIN songs ON COALESCE(scrobbles.mbid, scrobbles.mbid_computed) = songs.mbid + SELECT s.artist, s.album, s.track_name, s.time, songs.image + FROM scrobbles s + LEFT JOIN songs ON songs.song_id = s.song_id WHERE user_id=$1 ORDER BY time DESC LIMIT 10` rows, err := store.Query(query, userId) if err != nil { log.Println(err) + return scrobbles } defer rows.Close() @@ -126,3 +129,27 @@ func (store *SqlStore) RecentScrobbles(userId int) []*Scrobble { } return scrobbles } + +func (store *SqlStore) GetSongId(artist, album, song string) (int, 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 + +} + +func (store *SqlStore) InsertSong(s *Scrobble) (int, 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 +} -- cgit v1.2.3-70-g09d2