From b4868a54e6ced351dbb47183f87a1209de0e4b4d Mon Sep 17 00:00:00 2001 From: Chris Cummer Date: Tue, 16 Apr 2019 02:54:10 -0700 Subject: [PATCH] WTF-400 SpotifyWeb extracted to new config format --- main.go | 3 ++- modules/spotifyweb/settings.go | 30 ++++++++++++++++++++++++++++ modules/spotifyweb/widget.go | 36 ++++++++++++---------------------- 3 files changed, 44 insertions(+), 25 deletions(-) create mode 100644 modules/spotifyweb/settings.go diff --git a/main.go b/main.go index 6a627e11..0789a372 100644 --- a/main.go +++ b/main.go @@ -280,7 +280,8 @@ func makeWidget(app *tview.Application, pages *tview.Pages, widgetName string) w settings := spotify.NewSettingsFromYAML(wtf.Config) widget = spotify.NewWidget(app, pages, settings) case "spotifyweb": - widget = spotifyweb.NewWidget(app, pages) + settings := spotifyweb.NewSettingsFromYAML(wtf.Config) + widget = spotifyweb.NewWidget(app, pages, settings) case "status": widget = status.NewWidget(app) case "system": diff --git a/modules/spotifyweb/settings.go b/modules/spotifyweb/settings.go new file mode 100644 index 00000000..ffe6421d --- /dev/null +++ b/modules/spotifyweb/settings.go @@ -0,0 +1,30 @@ +package spotifyweb + +import ( + "os" + + "github.com/olebedev/config" + "github.com/wtfutil/wtf/cfg" +) + +type Settings struct { + common *cfg.Common + + callbackPort string + clientID string + secretKey string +} + +func NewSettingsFromYAML(ymlConfig *config.Config) *Settings { + localConfig, _ := ymlConfig.Get("wtf.mods.spotifyweb") + + settings := Settings{ + common: cfg.NewCommonSettingsFromYAML(ymlConfig), + + callbackPort: localConfig.UString("callbackPort", "8080"), + clientID: localConfig.UString("clientID", os.Getenv("SPOTIFY_ID")), + secretKey: localConfig.UString("secretKey", os.Getenv("SPOTIFY_SECRET")), + } + + return &settings +} diff --git a/modules/spotifyweb/widget.go b/modules/spotifyweb/widget.go index 02723ffc..98af1863 100644 --- a/modules/spotifyweb/widget.go +++ b/modules/spotifyweb/widget.go @@ -4,7 +4,6 @@ import ( "errors" "fmt" "net/http" - "os" "time" "github.com/gdamore/tcell" @@ -47,10 +46,12 @@ type Info struct { type Widget struct { wtf.HelpfulWidget wtf.TextWidget + Info - clientChan chan *spotify.Client client *spotify.Client + clientChan chan *spotify.Client playerState *spotify.PlayerState + settings *Settings } var ( @@ -79,27 +80,12 @@ func authHandler(w http.ResponseWriter, r *http.Request) { tempClientChan <- &client } -func clientID() string { - return wtf.Config.UString( - "wtf.mods.spotifyweb.clientID", - os.Getenv("SPOTIFY_ID"), - ) -} - -func secretKey() string { - return wtf.Config.UString( - "wtf.mods.spotifyweb.secretKey", - os.Getenv("SPOTIFY_SECRET"), - ) -} - // NewWidget creates a new widget for WTF -func NewWidget(app *tview.Application, pages *tview.Pages) *Widget { - callbackPort = wtf.Config.UString("wtf.mods.spotifyweb.callbackPort", "8080") - redirectURI = "http://localhost:" + callbackPort + "/callback" +func NewWidget(app *tview.Application, pages *tview.Pages, settings *Settings) *Widget { + redirectURI = "http://localhost:" + settings.callbackPort + "/callback" auth = spotify.NewAuthenticator(redirectURI, spotify.ScopeUserReadCurrentlyPlaying, spotify.ScopeUserReadPlaybackState, spotify.ScopeUserModifyPlaybackState) - auth.SetAuthInfo(clientID(), secretKey()) + auth.SetAuthInfo(settings.clientID, settings.secretKey) authURL = auth.AuthURL(state) var client *spotify.Client @@ -108,10 +94,12 @@ func NewWidget(app *tview.Application, pages *tview.Pages) *Widget { widget := Widget{ HelpfulWidget: wtf.NewHelpfulWidget(app, pages, HelpText), TextWidget: wtf.NewTextWidget(app, "SpotifyWeb", "spotifyweb", true), - Info: Info{}, - clientChan: tempClientChan, - client: client, - playerState: playerState, + + Info: Info{}, + client: client, + clientChan: tempClientChan, + playerState: playerState, + settings: settings, } http.HandleFunc("/callback", authHandler)