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)
|
settings := trello.NewSettingsFromYAML(wtf.Config)
|
||||||
widget = trello.NewWidget(app, settings)
|
widget = trello.NewWidget(app, settings)
|
||||||
case "twitter":
|
case "twitter":
|
||||||
widget = twitter.NewWidget(app, pages)
|
settings := twitter.NewSettingsFromYAML(wtf.Config)
|
||||||
|
widget = twitter.NewWidget(app, pages, settings)
|
||||||
case "victorops":
|
case "victorops":
|
||||||
widget = victorops.NewWidget(app)
|
widget = victorops.NewWidget(app)
|
||||||
case "weather":
|
case "weather":
|
||||||
|
@ -3,10 +3,7 @@ package twitter
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"github.com/wtfutil/wtf/wtf"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
/* NOTE: Currently single application ONLY
|
/* NOTE: Currently single application ONLY
|
||||||
@ -22,15 +19,14 @@ type Client struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// NewClient creates and returns a new Twitter client
|
// NewClient creates and returns a new Twitter client
|
||||||
func NewClient() *Client {
|
func NewClient(settings *Settings) *Client {
|
||||||
client := Client{
|
client := Client{
|
||||||
apiBase: "https://api.twitter.com/1.1/",
|
apiBase: "https://api.twitter.com/1.1/",
|
||||||
count: wtf.Config.UInt("wtf.mods.twitter.count", 5),
|
count: settings.count,
|
||||||
screenName: "",
|
screenName: "",
|
||||||
|
bearerToken: settings.bearerToken,
|
||||||
}
|
}
|
||||||
|
|
||||||
client.loadAPICredentials()
|
|
||||||
|
|
||||||
return &client
|
return &client
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -65,10 +61,3 @@ func (client *Client) tweets() (tweets []Tweet, err error) {
|
|||||||
|
|
||||||
return
|
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
|
// Expected authorization format for single-application twitter dev accounts
|
||||||
req.Header.Add("Authorization",
|
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", bearerToken))
|
||||||
fmt.Sprintf("Bearer %s", bearerToken))
|
|
||||||
|
|
||||||
client := &http.Client{}
|
client := &http.Client{}
|
||||||
resp, err := client.Do(req)
|
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
|
||||||
|
}
|
@ -27,18 +27,20 @@ type Widget struct {
|
|||||||
wtf.MultiSourceWidget
|
wtf.MultiSourceWidget
|
||||||
wtf.TextWidget
|
wtf.TextWidget
|
||||||
|
|
||||||
client *Client
|
client *Client
|
||||||
idx int
|
idx int
|
||||||
sources []string
|
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{
|
widget := Widget{
|
||||||
HelpfulWidget: wtf.NewHelpfulWidget(app, pages, HelpText),
|
HelpfulWidget: wtf.NewHelpfulWidget(app, pages, HelpText),
|
||||||
MultiSourceWidget: wtf.NewMultiSourceWidget("twitter", "screenName", "screenNames"),
|
MultiSourceWidget: wtf.NewMultiSourceWidget("twitter", "screenName", "screenNames"),
|
||||||
TextWidget: wtf.NewTextWidget(app, "Twitter", "twitter", true),
|
TextWidget: wtf.NewTextWidget(app, "Twitter", "twitter", true),
|
||||||
|
|
||||||
idx: 0,
|
idx: 0,
|
||||||
|
settings: settings,
|
||||||
}
|
}
|
||||||
|
|
||||||
widget.HelpfulWidget.SetView(widget.View)
|
widget.HelpfulWidget.SetView(widget.View)
|
||||||
@ -46,7 +48,7 @@ func NewWidget(app *tview.Application, pages *tview.Pages) *Widget {
|
|||||||
widget.LoadSources()
|
widget.LoadSources()
|
||||||
widget.SetDisplayFunction(widget.display)
|
widget.SetDisplayFunction(widget.display)
|
||||||
|
|
||||||
widget.client = NewClient()
|
widget.client = NewClient(settings)
|
||||||
|
|
||||||
widget.View.SetBorderPadding(1, 1, 1, 1)
|
widget.View.SetBorderPadding(1, 1, 1, 1)
|
||||||
widget.View.SetWrap(true)
|
widget.View.SetWrap(true)
|
||||||
@ -163,6 +165,4 @@ func (widget *Widget) keyboardIntercept(event *tcell.EventKey) *tcell.EventKey {
|
|||||||
default:
|
default:
|
||||||
return event
|
return event
|
||||||
}
|
}
|
||||||
|
|
||||||
return event
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user