mirror of
https://github.com/taigrr/wtf
synced 2025-01-18 04:03:14 -08:00
WTF-400 Twitter extracted to new config format
This commit is contained in:
parent
4b5045a0bb
commit
eaa8825aa5
3
main.go
3
main.go
@ -304,7 +304,8 @@ func makeWidget(app *tview.Application, pages *tview.Pages, widgetName string) w
|
||||
settings := trello.NewSettingsFromYAML(wtf.Config)
|
||||
widget = trello.NewWidget(app, settings)
|
||||
case "twitter":
|
||||
widget = twitter.NewWidget(app, pages)
|
||||
settings := twitter.NewSettingsFromYAML(wtf.Config)
|
||||
widget = twitter.NewWidget(app, pages, settings)
|
||||
case "victorops":
|
||||
widget = victorops.NewWidget(app)
|
||||
case "weather":
|
||||
|
@ -3,10 +3,7 @@ package twitter
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
|
||||
"github.com/wtfutil/wtf/wtf"
|
||||
)
|
||||
|
||||
/* NOTE: Currently single application ONLY
|
||||
@ -22,15 +19,14 @@ type Client struct {
|
||||
}
|
||||
|
||||
// NewClient creates and returns a new Twitter client
|
||||
func NewClient() *Client {
|
||||
func NewClient(settings *Settings) *Client {
|
||||
client := Client{
|
||||
apiBase: "https://api.twitter.com/1.1/",
|
||||
count: wtf.Config.UInt("wtf.mods.twitter.count", 5),
|
||||
count: settings.count,
|
||||
screenName: "",
|
||||
bearerToken: settings.bearerToken,
|
||||
}
|
||||
|
||||
client.loadAPICredentials()
|
||||
|
||||
return &client
|
||||
}
|
||||
|
||||
@ -65,10 +61,3 @@ func (client *Client) tweets() (tweets []Tweet, err error) {
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (client *Client) loadAPICredentials() {
|
||||
client.bearerToken = wtf.Config.UString(
|
||||
"wtf.mods.twitter.bearerToken",
|
||||
os.Getenv("WTF_TWITTER_BEARER_TOKEN"),
|
||||
)
|
||||
}
|
||||
|
@ -13,8 +13,7 @@ func Request(bearerToken string, apiURL string) ([]byte, error) {
|
||||
}
|
||||
|
||||
// Expected authorization format for single-application twitter dev accounts
|
||||
req.Header.Add("Authorization",
|
||||
fmt.Sprintf("Bearer %s", bearerToken))
|
||||
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", bearerToken))
|
||||
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
|
30
modules/twitter/settings.go
Normal file
30
modules/twitter/settings.go
Normal file
@ -0,0 +1,30 @@
|
||||
package twitter
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"github.com/olebedev/config"
|
||||
"github.com/wtfutil/wtf/cfg"
|
||||
)
|
||||
|
||||
type Settings struct {
|
||||
common *cfg.Common
|
||||
|
||||
bearerToken string
|
||||
count int
|
||||
screenNames []interface{}
|
||||
}
|
||||
|
||||
func NewSettingsFromYAML(ymlConfig *config.Config) *Settings {
|
||||
localConfig, _ := ymlConfig.Get("wtf.mods.twitter")
|
||||
|
||||
settings := Settings{
|
||||
common: cfg.NewCommonSettingsFromYAML(ymlConfig),
|
||||
|
||||
bearerToken: localConfig.UString("bearerToken", os.Getenv("WTF_TWITTER_BEARER_TOKEN")),
|
||||
count: localConfig.UInt("count", 5),
|
||||
screenNames: localConfig.UList("screenName"),
|
||||
}
|
||||
|
||||
return &settings
|
||||
}
|
@ -29,16 +29,18 @@ type Widget struct {
|
||||
|
||||
client *Client
|
||||
idx int
|
||||
settings *Settings
|
||||
sources []string
|
||||
}
|
||||
|
||||
func NewWidget(app *tview.Application, pages *tview.Pages) *Widget {
|
||||
func NewWidget(app *tview.Application, pages *tview.Pages, settings *Settings) *Widget {
|
||||
widget := Widget{
|
||||
HelpfulWidget: wtf.NewHelpfulWidget(app, pages, HelpText),
|
||||
MultiSourceWidget: wtf.NewMultiSourceWidget("twitter", "screenName", "screenNames"),
|
||||
TextWidget: wtf.NewTextWidget(app, "Twitter", "twitter", true),
|
||||
|
||||
idx: 0,
|
||||
settings: settings,
|
||||
}
|
||||
|
||||
widget.HelpfulWidget.SetView(widget.View)
|
||||
@ -46,7 +48,7 @@ func NewWidget(app *tview.Application, pages *tview.Pages) *Widget {
|
||||
widget.LoadSources()
|
||||
widget.SetDisplayFunction(widget.display)
|
||||
|
||||
widget.client = NewClient()
|
||||
widget.client = NewClient(settings)
|
||||
|
||||
widget.View.SetBorderPadding(1, 1, 1, 1)
|
||||
widget.View.SetWrap(true)
|
||||
@ -163,6 +165,4 @@ func (widget *Widget) keyboardIntercept(event *tcell.EventKey) *tcell.EventKey {
|
||||
default:
|
||||
return event
|
||||
}
|
||||
|
||||
return event
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user