|
@@ -6,7 +6,7 @@ import (
|
6
|
6
|
"flag"
|
7
|
7
|
"fmt"
|
8
|
8
|
"github.com/jasonlvhit/gocron"
|
9
|
|
- "github.com/lamperi/e4bot/spotify"
|
|
9
|
+ "github.com/zmb3/spotify"
|
10
|
10
|
"io"
|
11
|
11
|
"log"
|
12
|
12
|
"os"
|
|
@@ -18,9 +18,13 @@ import (
|
18
|
18
|
)
|
19
|
19
|
|
20
|
20
|
type App struct {
|
21
|
|
- db *DB
|
22
|
|
- credentials Credentials
|
23
|
|
- spotifyClient *spotify.SpotifyClient
|
|
21
|
+ db *DB
|
|
22
|
+ credentials Credentials
|
|
23
|
+ spotify *AppSpotify
|
|
24
|
+}
|
|
25
|
+type AppSpotify struct {
|
|
26
|
+ auth *spotify.Authenticator
|
|
27
|
+ client *spotify.Client
|
24
|
28
|
}
|
25
|
29
|
|
26
|
30
|
type Credentials struct {
|
|
@@ -34,16 +38,16 @@ type Credentials struct {
|
34
|
38
|
ListenAddr string
|
35
|
39
|
}
|
36
|
40
|
|
37
|
|
-func (app *App) CreateSpotifyClient() *spotify.SpotifyClient {
|
38
|
|
- spotifyClient := spotify.NewClient(app.credentials.SpotifyClientID, app.credentials.SpotifyClientSecret)
|
39
|
|
- spotifyClient.SetupUserAuthenticate(app.credentials.SpotifyCallback)
|
40
|
|
- return spotifyClient
|
|
41
|
+func (app *App) CreateSpotifyAuthenticator() *spotify.Authenticator {
|
|
42
|
+ auth := spotify.NewAuthenticator(app.credentials.SpotifyCallback, spotify.ScopePlaylistModifyPublic)
|
|
43
|
+ auth.SetAuthInfo(app.credentials.SpotifyClientID, app.credentials.SpotifyClientSecret)
|
|
44
|
+ return &auth
|
41
|
45
|
}
|
42
|
46
|
|
43
|
47
|
func (app *App) LaunchWeb() {
|
44
|
|
- app.spotifyClient = app.CreateSpotifyClient()
|
|
48
|
+ app.spotify.auth = app.CreateSpotifyAuthenticator()
|
45
|
49
|
go func() {
|
46
|
|
- webStart(app.credentials.ListenAddr, app.db, app.spotifyClient)
|
|
50
|
+ webStart(app.credentials.ListenAddr, app.db, app.spotify)
|
47
|
51
|
}()
|
48
|
52
|
}
|
49
|
53
|
|
|
@@ -225,15 +229,15 @@ func appendAverages(wikiText string) string {
|
225
|
229
|
return strings.Join(changedLines, "\n")
|
226
|
230
|
}
|
227
|
231
|
|
228
|
|
-func findPlaylist(wikiText string) (string, []string) {
|
|
232
|
+func findPlaylist(wikiText string) (spotify.ID, []spotify.ID) {
|
229
|
233
|
const SONG_MARK = "<!-- Kappale -->"
|
230
|
234
|
const SPOTIFY_MARK = "https://open.spotify.com/track/"
|
231
|
235
|
const SPOTIFY_PLAYLIST_MARK = "/playlist/"
|
232
|
236
|
const PLAYLIST_MARK = " Spotify-soittolista]"
|
233
|
237
|
lines := strings.Split(wikiText, "\n")
|
234
|
238
|
|
235
|
|
- playlistId := ""
|
236
|
|
- tracks := make([]string, 0)
|
|
239
|
+ var playlistId spotify.ID
|
|
240
|
+ tracks := make([]spotify.ID, 0)
|
237
|
241
|
for _, line := range lines {
|
238
|
242
|
if strings.Index(line, SONG_MARK) != -1 {
|
239
|
243
|
i := strings.Index(line, SPOTIFY_MARK)
|
|
@@ -243,27 +247,28 @@ func findPlaylist(wikiText string) (string, []string) {
|
243
|
247
|
j += i
|
244
|
248
|
}
|
245
|
249
|
trackId := line[i+len(SPOTIFY_MARK) : j]
|
246
|
|
- tracks = append(tracks, trackId)
|
|
250
|
+ tracks = append(tracks, spotify.ID(trackId))
|
247
|
251
|
}
|
248
|
252
|
} else if strings.Index(line, SPOTIFY_PLAYLIST_MARK) != -1 && strings.Index(line, PLAYLIST_MARK) != -1 {
|
249
|
253
|
i := strings.Index(line, SPOTIFY_PLAYLIST_MARK)
|
250
|
254
|
j := strings.Index(line[i:], PLAYLIST_MARK)
|
251
|
255
|
|
252
|
|
- playlistId = line[i+len(SPOTIFY_PLAYLIST_MARK) : i+j]
|
253
|
|
- q := strings.Index(playlistId, "?")
|
|
256
|
+ playlist := line[i+len(SPOTIFY_PLAYLIST_MARK) : i+j]
|
|
257
|
+ q := strings.Index(playlist, "?")
|
254
|
258
|
if q != -1 {
|
255
|
|
- playlistId = playlistId[:q]
|
|
259
|
+ playlist = playlist[:q]
|
256
|
260
|
}
|
|
261
|
+ playlistId = spotify.ID(playlist)
|
257
|
262
|
}
|
258
|
263
|
}
|
259
|
264
|
log.Printf("Found playlist %s and tracks %s\n", playlistId, tracks)
|
260
|
265
|
|
261
|
|
- return playlistId, tracks
|
|
266
|
+ return spotify.ID(playlistId), tracks
|
262
|
267
|
}
|
263
|
268
|
|
264
|
|
-func appendPlaylist(wikiText string, playlist *spotify.PlaylistInfo) string {
|
|
269
|
+func appendPlaylist(wikiText string, playlist *spotify.FullPlaylist) string {
|
265
|
270
|
changedText := wikiText + `
|
266
|
|
- [` + playlist.ExternalUrls.Spotify + ` Spotify-soittolista]
|
|
271
|
+ [` + playlist.ExternalURLs["spotify"] + ` Spotify-soittolista]
|
267
|
272
|
`
|
268
|
273
|
return changedText
|
269
|
274
|
}
|
|
@@ -273,6 +278,7 @@ func (app *App) AutomateSection(title string) error {
|
273
|
278
|
|
274
|
279
|
sections, err := wiki.GetWikiPageSections(title)
|
275
|
280
|
if err != nil {
|
|
281
|
+ log.Println("Error while reading page sections")
|
276
|
282
|
return err
|
277
|
283
|
}
|
278
|
284
|
|
|
@@ -292,6 +298,7 @@ func (app *App) AutomateSection(title string) error {
|
292
|
298
|
|
293
|
299
|
wikiText, err := wiki.GetWikiPageSectionText(title, section.index)
|
294
|
300
|
if err != nil {
|
|
301
|
+ log.Println("Error reading text from wiki")
|
295
|
302
|
return err
|
296
|
303
|
}
|
297
|
304
|
message := ""
|
|
@@ -300,41 +307,54 @@ func (app *App) AutomateSection(title string) error {
|
300
|
307
|
message = message + fmt.Sprintf("Calculate averages for week %d. ", weekNumber)
|
301
|
308
|
}
|
302
|
309
|
|
303
|
|
- if app.spotifyClient.HasUserLogin() {
|
304
|
|
- playlistId, tracks := findPlaylist(changedWikiText)
|
305
|
|
- currentTracks, err := app.db.FindPlaylistBySection(section.title)
|
306
|
|
- if len(tracks) > 0 && (err != nil || reflect.DeepEqual(currentTracks, tracks)) {
|
|
310
|
+ if app.spotify.client != nil {
|
|
311
|
+ token, err := app.spotify.client.Token()
|
|
312
|
+ if err != nil {
|
|
313
|
+ log.Println("No token available")
|
|
314
|
+ } else if token.Expiry.Before(time.Now()) {
|
|
315
|
+ log.Println("Token has expired")
|
|
316
|
+ } else {
|
|
317
|
+ log.Println("Checking if playlist needs updating")
|
|
318
|
+ playlistId, tracks := findPlaylist(changedWikiText)
|
|
319
|
+ currentTracks, err := app.db.FindPlaylistBySection(section.title)
|
|
320
|
+ if len(tracks) > 0 && (err != nil || reflect.DeepEqual(currentTracks, tracks)) {
|
|
321
|
+
|
|
322
|
+ if playlistId == "" {
|
|
323
|
+
|
|
324
|
+ info, err := app.spotify.client.CreatePlaylistForUser(app.credentials.SpotifyUser, title+" "+section.title, true)
|
|
325
|
+ if err != nil {
|
|
326
|
+ log.Println("Error creating playlist to Spotify")
|
|
327
|
+ return err
|
|
328
|
+ }
|
|
329
|
+ playlistId = info.ID
|
|
330
|
+ changedWikiText = appendPlaylist(changedWikiText, info)
|
|
331
|
+ message = message + fmt.Sprintf("Added link to Spotify playlist for week %d.", weekNumber)
|
|
332
|
+ }
|
|
333
|
+ err := app.spotify.client.ReplacePlaylistTracks(app.credentials.SpotifyUser, playlistId, tracks...)
|
|
334
|
+ if err != nil {
|
|
335
|
+ log.Println("Error updating playlist to Spotify")
|
|
336
|
+ return err
|
|
337
|
+ }
|
307
|
338
|
|
308
|
|
- spotify := app.spotifyClient
|
309
|
|
- if playlistId == "" {
|
|
339
|
+ stringTracks := make([]string, len(tracks))
|
|
340
|
+ for i, t := range tracks {
|
|
341
|
+ stringTracks[i] = string(t)
|
|
342
|
+ }
|
310
|
343
|
|
311
|
|
- info, err := spotify.NewPlaylist(title+" "+section.title, app.credentials.SpotifyUser)
|
|
344
|
+ _, err = app.db.UpdatePlaylistBySection(section.title, stringTracks)
|
312
|
345
|
if err != nil {
|
313
|
|
- log.Println("Error creating playlist")
|
|
346
|
+ log.Println("Error updating playlist to DB")
|
314
|
347
|
return err
|
315
|
348
|
}
|
316
|
|
- playlistId = info.Id
|
317
|
|
- changedWikiText = appendPlaylist(changedWikiText, info)
|
318
|
|
- message = message + fmt.Sprintf("Added link to Spotify playlist for week %d.", weekNumber)
|
319
|
|
- }
|
320
|
|
- err := spotify.UpdatePlaylist(app.credentials.SpotifyUser, playlistId, tracks)
|
321
|
|
- if err != nil {
|
322
|
|
- log.Println("Error updating playlist")
|
323
|
|
- return err
|
324
|
|
- }
|
325
|
|
- _, err = app.db.UpdatePlaylistBySection(section.title, tracks)
|
326
|
|
- if err != nil {
|
327
|
|
- return err
|
328
|
349
|
}
|
329
|
350
|
}
|
330
|
|
-
|
331
|
351
|
}
|
332
|
352
|
|
333
|
353
|
if message != "" {
|
334
|
354
|
|
335
|
355
|
_, err := wiki.EditWikiPageSection(title, section.index, changedWikiText,
|
336
|
356
|
fmt.Sprintf("Calculate averages for week %d", weekNumber))
|
337
|
|
- err = nil
|
|
357
|
+ //err = errors.New("foo")
|
338
|
358
|
if err != nil {
|
339
|
359
|
return err
|
340
|
360
|
}
|
|
@@ -397,7 +417,7 @@ func main() {
|
397
|
417
|
panic(err)
|
398
|
418
|
}
|
399
|
419
|
|
400
|
|
- a := App{InitDatabase(), creds, nil}
|
|
420
|
+ a := App{InitDatabase(), creds, &AppSpotify{}}
|
401
|
421
|
a.LaunchWeb()
|
402
|
422
|
|
403
|
423
|
gocron.Every(1).Minute().Do(a.AutomateSectionTask)
|