|
@@ -10,6 +10,7 @@ import (
|
10
|
10
|
"io"
|
11
|
11
|
"log"
|
12
|
12
|
"os"
|
|
13
|
+ "reflect"
|
13
|
14
|
"regexp"
|
14
|
15
|
"strconv"
|
15
|
16
|
"strings"
|
|
@@ -17,8 +18,9 @@ import (
|
17
|
18
|
)
|
18
|
19
|
|
19
|
20
|
type App struct {
|
20
|
|
- db *DB
|
21
|
|
- credentials Credentials
|
|
21
|
+ db *DB
|
|
22
|
+ credentials Credentials
|
|
23
|
+ spotifyClient *spotify.SpotifyClient
|
22
|
24
|
}
|
23
|
25
|
|
24
|
26
|
type Credentials struct {
|
|
@@ -27,18 +29,20 @@ type Credentials struct {
|
27
|
29
|
Password string
|
28
|
30
|
SpotifyClientID string
|
29
|
31
|
SpotifyClientSecret string
|
|
32
|
+ SpotifyUser string
|
30
|
33
|
ListenAddr string
|
31
|
34
|
}
|
32
|
35
|
|
33
|
36
|
func (app *App) CreateSpotifyClient() *spotify.SpotifyClient {
|
34
|
37
|
spotifyClient := spotify.NewClient(app.credentials.SpotifyClientID, app.credentials.SpotifyClientSecret)
|
|
38
|
+ spotifyClient.SetupUserAuthenticate()
|
35
|
39
|
return spotifyClient
|
36
|
40
|
}
|
37
|
41
|
|
38
|
42
|
func (app *App) LaunchWeb() {
|
39
|
|
- spotifyClient := app.CreateSpotifyClient()
|
|
43
|
+ app.spotifyClient = app.CreateSpotifyClient()
|
40
|
44
|
go func() {
|
41
|
|
- webStart(app.credentials.ListenAddr, app.db, spotifyClient)
|
|
45
|
+ webStart(app.credentials.ListenAddr, app.db, app.spotifyClient)
|
42
|
46
|
}()
|
43
|
47
|
}
|
44
|
48
|
|
|
@@ -215,7 +219,50 @@ func appendAverages(wikiText string) string {
|
215
|
219
|
return strings.Join(changedLines, "\n")
|
216
|
220
|
}
|
217
|
221
|
|
218
|
|
-func (app *App) FixAverages(title string) error {
|
|
222
|
+func findPlaylist(wikiText string) (string, []string) {
|
|
223
|
+ const SONG_MARK = "<!-- Kappale -->"
|
|
224
|
+ const SPOTIFY_MARK = "https://open.spotify.com/track/"
|
|
225
|
+ const SPOTIFY_PLAYLIST_MARK = "/playlist/"
|
|
226
|
+ const PLAYLIST_MARK = " Spotify-soittolista]"
|
|
227
|
+ lines := strings.Split(wikiText, "\n")
|
|
228
|
+
|
|
229
|
+ playlistId := ""
|
|
230
|
+ tracks := make([]string, 0)
|
|
231
|
+ for _, line := range lines {
|
|
232
|
+ if strings.Index(line, SONG_MARK) != -1 {
|
|
233
|
+ i := strings.Index(line, SPOTIFY_MARK)
|
|
234
|
+ if i != -1 {
|
|
235
|
+ j := strings.Index(line[i:], " ")
|
|
236
|
+ if j != -1 {
|
|
237
|
+ j += i
|
|
238
|
+ }
|
|
239
|
+ trackId := line[i+len(SPOTIFY_MARK) : j]
|
|
240
|
+ tracks = append(tracks, trackId)
|
|
241
|
+ }
|
|
242
|
+ } else if strings.Index(line, SPOTIFY_PLAYLIST_MARK) != -1 && strings.Index(line, PLAYLIST_MARK) != -1 {
|
|
243
|
+ i := strings.Index(line, SPOTIFY_PLAYLIST_MARK)
|
|
244
|
+ j := strings.Index(line[i:], PLAYLIST_MARK)
|
|
245
|
+
|
|
246
|
+ playlistId = line[i+len(SPOTIFY_PLAYLIST_MARK) : i+j]
|
|
247
|
+ q := strings.Index(playlistId, "?")
|
|
248
|
+ if q != -1 {
|
|
249
|
+ playlistId = playlistId[:q]
|
|
250
|
+ }
|
|
251
|
+ }
|
|
252
|
+ }
|
|
253
|
+ fmt.Printf("Found playlist %s and tracks %s\n", playlistId, tracks)
|
|
254
|
+
|
|
255
|
+ return playlistId, tracks
|
|
256
|
+}
|
|
257
|
+
|
|
258
|
+func appendPlaylist(wikiText string, playlist *spotify.PlaylistInfo) string {
|
|
259
|
+ changedText := wikiText + `
|
|
260
|
+ [` + playlist.ExternalUrls.Spotify + ` Spotify-soittolista]
|
|
261
|
+ `
|
|
262
|
+ return changedText
|
|
263
|
+}
|
|
264
|
+
|
|
265
|
+func (app *App) AutomateSection(title string) error {
|
219
|
266
|
wiki := app.wikiClient()
|
220
|
267
|
|
221
|
268
|
sections, err := wiki.GetWikiPageSections(title)
|
|
@@ -235,18 +282,54 @@ func (app *App) FixAverages(title string) error {
|
235
|
282
|
if weekNumber > currentWeek {
|
236
|
283
|
break
|
237
|
284
|
}
|
|
285
|
+ fmt.Println("Checking section", section.title)
|
238
|
286
|
|
239
|
287
|
wikiText, err := wiki.GetWikiPageSectionText(title, section.index)
|
240
|
288
|
if err != nil {
|
241
|
289
|
return err
|
242
|
290
|
}
|
|
291
|
+ message := ""
|
243
|
292
|
changedWikiText := appendAverages(wikiText)
|
244
|
293
|
if changedWikiText != wikiText {
|
245
|
|
- //fmt.Println(wikiText)
|
246
|
|
- //fmt.Println(changedWikiText)
|
|
294
|
+ message = message + fmt.Sprintf("Calculate averages for week %d. ", weekNumber)
|
|
295
|
+ }
|
|
296
|
+
|
|
297
|
+ if app.spotifyClient.HasUserLogin() {
|
|
298
|
+ playlistId, tracks := findPlaylist(changedWikiText)
|
|
299
|
+ currentTracks, err := app.db.FindPlaylistBySection(section.title)
|
|
300
|
+ if len(tracks) > 0 && (err != nil || reflect.DeepEqual(currentTracks, tracks)) {
|
|
301
|
+
|
|
302
|
+ spotify := app.spotifyClient
|
|
303
|
+ if playlistId == "" {
|
|
304
|
+
|
|
305
|
+ info, err := spotify.NewPlaylist(title+" "+section.title, app.credentials.SpotifyUser)
|
|
306
|
+ if err != nil {
|
|
307
|
+ log.Println("Error creating playlist")
|
|
308
|
+ return err
|
|
309
|
+ }
|
|
310
|
+ playlistId = info.Id
|
|
311
|
+ changedWikiText = appendPlaylist(changedWikiText, info)
|
|
312
|
+ message = message + fmt.Sprintf("Added link to Spotify playlist for week %d.", weekNumber)
|
|
313
|
+ }
|
|
314
|
+ err := spotify.UpdatePlaylist(app.credentials.SpotifyUser, playlistId, tracks)
|
|
315
|
+ if err != nil {
|
|
316
|
+ log.Println("Error updating playlist")
|
|
317
|
+ return err
|
|
318
|
+ }
|
|
319
|
+ _, err = app.db.UpdatePlaylistBySection(section.title, tracks)
|
|
320
|
+ if err != nil {
|
|
321
|
+ return err
|
|
322
|
+ }
|
|
323
|
+ }
|
|
324
|
+
|
|
325
|
+ }
|
247
|
326
|
|
248
|
|
- _, err := wiki.EditWikiPageSection(title, section.index, changedWikiText,
|
249
|
|
- fmt.Sprintf("Calculate averages for week %d", weekNumber))
|
|
327
|
+ if message != "" {
|
|
328
|
+ fmt.Println(changedWikiText)
|
|
329
|
+
|
|
330
|
+ //_, err := wiki.EditWikiPageSection(title, section.index, changedWikiText,
|
|
331
|
+ // fmt.Sprintf("Calculate averages for week %d", weekNumber))
|
|
332
|
+ err = nil
|
250
|
333
|
if err != nil {
|
251
|
334
|
return err
|
252
|
335
|
}
|
|
@@ -256,11 +339,20 @@ func (app *App) FixAverages(title string) error {
|
256
|
339
|
return nil
|
257
|
340
|
}
|
258
|
341
|
|
259
|
|
-func (app *App) FixAveragesTask() {
|
260
|
|
- err := app.FixAverages("Levyraati 2018")
|
|
342
|
+func (app *App) AutomateSectionTask() {
|
|
343
|
+ panels, err := app.db.FindAllPanels()
|
261
|
344
|
if err != nil {
|
262
|
|
- fmt.Println("Error while calculating averages:", err)
|
|
345
|
+ fmt.Println("Error while checking db for panels:", err)
|
|
346
|
+ return
|
|
347
|
+ }
|
|
348
|
+ for _, panel := range panels {
|
|
349
|
+ fmt.Println("Checking panel", panel)
|
|
350
|
+ err := app.AutomateSection(panel)
|
|
351
|
+ if err != nil {
|
|
352
|
+ fmt.Println("Error while processing panel:", err)
|
|
353
|
+ }
|
263
|
354
|
}
|
|
355
|
+
|
264
|
356
|
}
|
265
|
357
|
|
266
|
358
|
func initCreds() (Credentials, error) {
|
|
@@ -300,10 +392,10 @@ func main() {
|
300
|
392
|
panic(err)
|
301
|
393
|
}
|
302
|
394
|
|
303
|
|
- a := App{InitDatabase(), creds}
|
|
395
|
+ a := App{InitDatabase(), creds, nil}
|
304
|
396
|
a.LaunchWeb()
|
305
|
397
|
|
306
|
|
- gocron.Every(1).Hour().Do(a.FixAveragesTask)
|
|
398
|
+ gocron.Every(1).Hour().Do(a.AutomateSectionTask)
|
307
|
399
|
gocron.Every(1).Second().Do(a.SubmitSongs)
|
308
|
400
|
<-gocron.Start()
|
309
|
401
|
|