aboutsummaryrefslogtreecommitdiffstats
path: root/data.go
blob: 9e5f5d5c9b4a242622c8e32565fc442456c4055a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package main

import (
	"database/sql"
	"encoding/xml"
	"log"
	"time"

	_ "github.com/mattn/go-sqlite3"
)

type Scrobble struct {
	Artist      Correctable `xml:"artist" json:"artist"`
	AlbumArtist Correctable `xml:"albumArtist" json:"albumArtist"`
	TrackName   Correctable `xml:"track" json:"track"`
	Album       Correctable `xml:"album" json:"album"`
	TrackNumber int         `xml:"-" json:"-"`
	Duration    int         `xml:"-" json:"-"`
	Time        time.Time   `xml:"timestamp" json:"timestamp,string"`
	Chosen      bool        `xml:"-" json:"-"`
	Mbid        string      `xml:"-" json:"-"`
	SongId      int
	SessionKey  string `xml:"-" json:"-"`
	UserId      int
	Image       string
}

type Session struct {
	XMLName       xml.Name `json:"-" xml:"session"`
	User          string   `json:"name" xml:"name"`
	Key           string   `json:"key" xml:"key"`
	UserId        int
	Client        string
	ClientVersion string
	Protocol      string
	Created       time.Time
	Subscriber    int64 `json:"subscriber" xml:"subscriber"`
}

type DataStore interface {
	PutSession(*Session) error
	GetSession(key string) (*Session, error)
	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
}

type SqlStore struct {
	*sql.DB
}

func (store *SqlStore) PutSession(s *Session) error {
	query := `
	INSERT INTO scrobbling_sessions (user_id, session_key, client, client_version, protocol)
	VALUES ($1, $2, $3, $4, $5)`
	_, err := store.Exec(query, s.UserId, s.Key, s.Client, s.ClientVersion, s.Protocol)
	return err
}

func (store *SqlStore) GetSession(key string) (*Session, error) {
	query := `
	SELECT user_id, session_key, client, client_version, protocol, created
	FROM scrobbling_sessions WHERE session_key = $1`
	row := store.QueryRow(query, key)
	s := new(Session)
	err := row.Scan(&s.UserId, &s.Key, &s.Client, &s.ClientVersion, &s.Protocol,
		&s.Created)
	return s, err
}

func (store *SqlStore) GetUser(lfmName string) (int, string, error) {
	var password string
	var userId int
	row := store.QueryRow("SELECT user_id, lfm_password FROM users WHERE lfm_name = $1",
		lfmName)
	err := row.Scan(&userId, &password)
	return userId, password, err
}

func (store *SqlStore) PutScrobbles(scrobbles []Scrobble) error {
	tx, err := store.Begin()
	if err != nil {
		return err
	}
	query := `
	INSERT INTO scrobbles
	(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 {
		return err
	}

	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.SongId,
			s.SessionKey, s.UserId)
		if err != nil {
			tx.Rollback()
			return err
		}
	}
	return tx.Commit()
}

func (store *SqlStore) RecentScrobbles(userId int) []*Scrobble {
	scrobbles := make([]*Scrobble, 0, 10)
	query := `
	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()

	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)
	}
	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
}