|
@@ -3,6 +3,7 @@ package main
|
3
|
3
|
import (
|
4
|
4
|
"encoding/json"
|
5
|
5
|
"errors"
|
|
6
|
+ "flag"
|
6
|
7
|
"fmt"
|
7
|
8
|
"github.com/jasonlvhit/gocron"
|
8
|
9
|
"github.com/lamperi/e4bot/spotify"
|
|
@@ -15,9 +16,12 @@ import (
|
15
|
16
|
"time"
|
16
|
17
|
)
|
17
|
18
|
|
18
|
|
-var songs SongPriorityQueue
|
|
19
|
+type App struct {
|
|
20
|
+ db *DB
|
|
21
|
+ credentials Credentials
|
|
22
|
+}
|
19
|
23
|
|
20
|
|
-var credentials struct {
|
|
24
|
+type Credentials struct {
|
21
|
25
|
APIURL string
|
22
|
26
|
UserName string
|
23
|
27
|
Password string
|
|
@@ -26,6 +30,18 @@ var credentials struct {
|
26
|
30
|
ListenAddr string
|
27
|
31
|
}
|
28
|
32
|
|
|
33
|
+func (app *App) CreateSpotifyClient() *spotify.SpotifyClient {
|
|
34
|
+ spotifyClient := spotify.NewClient(app.credentials.SpotifyClientID, app.credentials.SpotifyClientSecret)
|
|
35
|
+ return spotifyClient
|
|
36
|
+}
|
|
37
|
+
|
|
38
|
+func (app *App) LaunchWeb() {
|
|
39
|
+ spotifyClient := app.CreateSpotifyClient()
|
|
40
|
+ go func() {
|
|
41
|
+ webStart(app.credentials.ListenAddr, app.db, spotifyClient)
|
|
42
|
+ }()
|
|
43
|
+}
|
|
44
|
+
|
29
|
45
|
func appendSong(wikiText string, author string, newSong string) string {
|
30
|
46
|
const AUTHOR_MARK = "<!-- Lisääjä -->"
|
31
|
47
|
const SONG_MARK = "<!-- Kappale -->"
|
|
@@ -46,8 +62,13 @@ func appendSong(wikiText string, author string, newSong string) string {
|
46
|
62
|
return strings.Join(changedLines, "\n")
|
47
|
63
|
}
|
48
|
64
|
|
49
|
|
-func addSong(updateTitle, updateSection, author, song string) (bool, error) {
|
50
|
|
- wiki := CreateWikiClient(credentials.APIURL, credentials.UserName, credentials.Password)
|
|
65
|
+func (app *App) wikiClient() *WikiClient {
|
|
66
|
+ wiki := CreateWikiClient(app.credentials.APIURL, app.credentials.UserName, app.credentials.Password)
|
|
67
|
+ return wiki
|
|
68
|
+}
|
|
69
|
+
|
|
70
|
+func (app *App) AddSong(updateTitle, updateSection, author, song string) (bool, error) {
|
|
71
|
+ wiki := app.wikiClient()
|
51
|
72
|
|
52
|
73
|
sections, err := wiki.GetWikiPageSections(updateTitle)
|
53
|
74
|
if err != nil {
|
|
@@ -76,69 +97,34 @@ func addSong(updateTitle, updateSection, author, song string) (bool, error) {
|
76
|
97
|
return false, errors.New("Could not find matching section")
|
77
|
98
|
}
|
78
|
99
|
|
79
|
|
-func songSynced(userId, roundId int) error {
|
80
|
|
- query := `UPDATE public.entry SET synced = true WHERE user_id = $1 AND round_id = $2`
|
81
|
|
- res, err := getDb().Exec(query, userId, roundId)
|
82
|
|
-
|
83
|
|
- if err != nil {
|
84
|
|
- return err
|
85
|
|
- }
|
86
|
|
-
|
87
|
|
- affected, err := res.RowsAffected()
|
88
|
|
- if err != nil {
|
89
|
|
- return err
|
90
|
|
- }
|
91
|
|
-
|
92
|
|
- if affected != 1 {
|
93
|
|
- return errors.New("Unknown entry ID")
|
94
|
|
- }
|
95
|
|
- return nil
|
|
100
|
+func (app *App) SongSynced(userId, roundId int) error {
|
|
101
|
+ _, err := app.db.EntrySynced(userId, roundId)
|
|
102
|
+ return err
|
96
|
103
|
}
|
97
|
104
|
|
98
|
|
-func submitSong() {
|
99
|
|
- query := `
|
100
|
|
- SELECT e.user_id, e.round_id, e.artist, e.title, e.spotify_url, p.article, u.username, r.section
|
101
|
|
- FROM public.entry e
|
102
|
|
- JOIN public."user" u ON u.id = e.user_id
|
103
|
|
- JOIN public.round r ON r.id = e.round_id
|
104
|
|
- JOIN public.panel p ON p.id = r.panel_id
|
105
|
|
- WHERE r.start < current_timestamp AND e.synced = false`
|
106
|
|
- rows, err := getDb().Query(query)
|
|
105
|
+func (app *App) SubmitSongs() {
|
|
106
|
+ entries, err := app.db.FindEntriesToSync()
|
107
|
107
|
if err != nil {
|
108
|
|
- log.Println("Error while reading songs from database:", err)
|
|
108
|
+ log.Println("Error while finding entries to sync:", err)
|
109
|
109
|
return
|
110
|
110
|
}
|
111
|
|
- defer rows.Close()
|
112
|
|
- for rows.Next() {
|
113
|
|
- var (
|
114
|
|
- userId, roundId int
|
115
|
|
- artist, title, spotifyURL, article, username, section string
|
116
|
|
- )
|
117
|
|
- err := rows.Scan(&userId, &roundId, &artist, &title, &spotifyURL, &article, &username, §ion)
|
118
|
|
- if err != nil {
|
119
|
|
- log.Println("Error while scanning row:", err)
|
120
|
|
- continue
|
121
|
|
- }
|
122
|
|
- song := songWikiText(spotifyURL, artist, title)
|
|
111
|
+
|
|
112
|
+ for _, entry := range entries {
|
|
113
|
+ song := songWikiText(entry.spotifyURL, entry.artist, entry.title)
|
123
|
114
|
|
124
|
115
|
fmt.Println("Time has passed for " + song)
|
125
|
116
|
|
126
|
|
- success, err := addSong(article, section, username, song)
|
|
117
|
+ success, err := app.AddSong(entry.article, entry.section, entry.username, song)
|
127
|
118
|
if err != nil {
|
128
|
119
|
log.Println("Error while adding song:", err)
|
129
|
120
|
}
|
130
|
121
|
if success {
|
131
|
|
- err = songSynced(userId, roundId)
|
|
122
|
+ err = app.SongSynced(entry.userId, entry.roundId)
|
132
|
123
|
if err != nil {
|
133
|
124
|
fmt.Println("Error received:", err)
|
134
|
125
|
}
|
135
|
126
|
}
|
136
|
127
|
}
|
137
|
|
- err = rows.Err()
|
138
|
|
- if err != nil {
|
139
|
|
- log.Println("Error after reading cursor:", err)
|
140
|
|
- return
|
141
|
|
- }
|
142
|
128
|
}
|
143
|
129
|
|
144
|
130
|
func isCurrentAuthor(line, author string) bool {
|
|
@@ -229,8 +215,8 @@ func appendAverages(wikiText string) string {
|
229
|
215
|
return strings.Join(changedLines, "\n")
|
230
|
216
|
}
|
231
|
217
|
|
232
|
|
-func fixAverages(title string) error {
|
233
|
|
- wiki := CreateWikiClient(credentials.APIURL, credentials.UserName, credentials.Password)
|
|
218
|
+func (app *App) FixAverages(title string) error {
|
|
219
|
+ wiki := app.wikiClient()
|
234
|
220
|
|
235
|
221
|
sections, err := wiki.GetWikiPageSections(title)
|
236
|
222
|
if err != nil {
|
|
@@ -270,23 +256,28 @@ func fixAverages(title string) error {
|
270
|
256
|
return nil
|
271
|
257
|
}
|
272
|
258
|
|
273
|
|
-func fixAveragesTask() {
|
274
|
|
- err := fixAverages("Levyraati 2018")
|
|
259
|
+func (app *App) FixAveragesTask() {
|
|
260
|
+ err := app.FixAverages("Levyraati 2018")
|
275
|
261
|
if err != nil {
|
276
|
262
|
fmt.Println("Error while calculating averages:", err)
|
277
|
263
|
}
|
278
|
264
|
}
|
279
|
265
|
|
280
|
|
-func initCreds() error {
|
281
|
|
- f, err := os.Open("credentials.json")
|
|
266
|
+func initCreds() (Credentials, error) {
|
|
267
|
+ var credsFile string
|
|
268
|
+ flag.StringVar(&credsFile, "credentials", "credentials.json", "JSON config to hold app credentials")
|
|
269
|
+ flag.Parse()
|
|
270
|
+
|
|
271
|
+ var credentials Credentials
|
|
272
|
+ f, err := os.Open(credsFile)
|
282
|
273
|
if err != nil {
|
283
|
|
- return err
|
|
274
|
+ return credentials, err
|
284
|
275
|
}
|
285
|
276
|
defer f.Close()
|
286
|
277
|
|
287
|
278
|
if err != nil {
|
288
|
279
|
log.Fatal(err)
|
289
|
|
- return err
|
|
280
|
+ return credentials, err
|
290
|
281
|
}
|
291
|
282
|
dec := json.NewDecoder(f)
|
292
|
283
|
for {
|
|
@@ -296,7 +287,7 @@ func initCreds() error {
|
296
|
287
|
log.Fatal(err)
|
297
|
288
|
}
|
298
|
289
|
}
|
299
|
|
- return nil
|
|
290
|
+ return credentials, nil
|
300
|
291
|
}
|
301
|
292
|
|
302
|
293
|
func songWikiText(url string, artist string, title string) string {
|
|
@@ -304,19 +295,16 @@ func songWikiText(url string, artist string, title string) string {
|
304
|
295
|
}
|
305
|
296
|
|
306
|
297
|
func main() {
|
307
|
|
- err := initCreds()
|
|
298
|
+ creds, err := initCreds()
|
308
|
299
|
if err != nil {
|
309
|
300
|
panic(err)
|
310
|
301
|
}
|
311
|
302
|
|
312
|
|
- spotifyClient := spotify.NewClient(credentials.SpotifyClientID, credentials.SpotifyClientSecret)
|
313
|
|
- go func() {
|
314
|
|
- webStart(credentials.ListenAddr, spotifyClient)
|
315
|
|
- }()
|
316
|
|
-
|
317
|
|
- gocron.Every(1).Hour().Do(fixAveragesTask)
|
|
303
|
+ a := App{InitDatabase(), creds}
|
|
304
|
+ a.LaunchWeb()
|
318
|
305
|
|
319
|
|
- gocron.Every(1).Second().Do(submitSong)
|
|
306
|
+ gocron.Every(1).Hour().Do(a.FixAveragesTask)
|
|
307
|
+ gocron.Every(1).Second().Do(a.SubmitSongs)
|
320
|
308
|
<-gocron.Start()
|
321
|
309
|
|
322
|
310
|
}
|