1
0
mirror of https://github.com/taigrr/wtf synced 2025-01-18 04:03:14 -08:00

Implement initial twitterstats module

* Create module skeleton based off of the existing twitter module
  * Strip out unused pieces and try to make it as minimal as possible
 * Implement settings parsing, converting the untyped `screenNames` slice into an slice of strings
 * Implement initial minimal display, showing a table with all usernames and their follower count + # of tweets (using dummy metrics for now)
This commit is contained in:
Casey Primozic 2019-10-22 11:58:20 -07:00 committed by Casey Primozic
parent d7b0669796
commit 4c9990bafd
No known key found for this signature in database
GPG Key ID: 2A02222DA3425B99
4 changed files with 149 additions and 0 deletions

View File

@ -54,6 +54,7 @@ import (
"github.com/wtfutil/wtf/modules/travisci" "github.com/wtfutil/wtf/modules/travisci"
"github.com/wtfutil/wtf/modules/trello" "github.com/wtfutil/wtf/modules/trello"
"github.com/wtfutil/wtf/modules/twitter" "github.com/wtfutil/wtf/modules/twitter"
"github.com/wtfutil/wtf/modules/twitterstats"
"github.com/wtfutil/wtf/modules/unknown" "github.com/wtfutil/wtf/modules/unknown"
"github.com/wtfutil/wtf/modules/victorops" "github.com/wtfutil/wtf/modules/victorops"
"github.com/wtfutil/wtf/modules/weatherservices/arpansagovau" "github.com/wtfutil/wtf/modules/weatherservices/arpansagovau"
@ -239,6 +240,9 @@ func MakeWidget(
case "twitter": case "twitter":
settings := twitter.NewSettingsFromYAML(moduleName, moduleConfig, config) settings := twitter.NewSettingsFromYAML(moduleName, moduleConfig, config)
widget = twitter.NewWidget(app, pages, settings) widget = twitter.NewWidget(app, pages, settings)
case "twitterstats":
settings := twitterstats.NewSettingsFromYAML(moduleName, moduleConfig, config)
widget = twitterstats.NewWidget(app, pages, settings)
case "victorops": case "victorops":
settings := victorops.NewSettingsFromYAML(moduleName, moduleConfig, config) settings := victorops.NewSettingsFromYAML(moduleName, moduleConfig, config)
widget = victorops.NewWidget(app, settings) widget = victorops.NewWidget(app, settings)

View File

@ -0,0 +1,48 @@
package twitterstats
import (
"log"
)
type Client struct {
apiBase string
consumerKey string
consumerSecret string
accessToken string
accessTokenSecret string
screenNames []string
}
func NewClient(settings *Settings) *Client {
usernames := make([]string, len(settings.screenNames))
for i, username := range settings.screenNames {
switch username.(type) {
default:
{
log.Fatalf("All `screenName`s in twitterstats config must be of type string")
}
case string:
usernames[i] = username.(string)
}
}
client := Client{
apiBase: "https://api.twitter.com/1.1/",
consumerKey: settings.consumerKey,
consumerSecret: settings.consumerSecret,
accessToken: settings.accessToken,
accessTokenSecret: settings.accessTokenSecret,
screenNames: usernames,
}
return &client
}
func (client *Client) GetFollowerCounts() []int64 {
return []int64{0, 0, 0, 0, 0} // TODO
}
func (client *Client) GetTweetCounts() []int64 {
return []int64{0, 0, 0, 0, 0} // TODO
}

View File

@ -0,0 +1,39 @@
package twitterstats
import (
"os"
"github.com/olebedev/config"
"github.com/wtfutil/wtf/cfg"
)
const (
defaultFocusable = true
defaultTitle = "Twitter Stats"
)
type Settings struct {
common *cfg.Common
consumerKey string
consumerSecret string
accessToken string
accessTokenSecret string
screenNames []interface{}
}
func NewSettingsFromYAML(name string, ymlConfig *config.Config, globalConfig *config.Config) *Settings {
settings := Settings{
common: cfg.NewCommonSettingsFromModule(name, defaultTitle, defaultFocusable, ymlConfig, globalConfig),
consumerKey: ymlConfig.UString("consumerKey", os.Getenv("WTF_TWITTER_CONSUMER_KEY")),
consumerSecret: ymlConfig.UString("consumerSecret", os.Getenv("WTF_TWITTER_CONSUMER_SECRET")),
accessToken: ymlConfig.UString("accessToken", os.Getenv("WTF_TWITTER_ACCESS_TOKEN")),
accessTokenSecret: ymlConfig.UString("accessTokenSecret", os.Getenv("WTF_TWITTER_ACCESS_TOKEN_SECRET")),
screenNames: ymlConfig.UList("screenNames"),
}
return &settings
}

View File

@ -0,0 +1,58 @@
package twitterstats
import (
"fmt"
"github.com/rivo/tview"
"github.com/wtfutil/wtf/view"
)
type Widget struct {
view.KeyboardWidget
view.TextWidget
client *Client
idx int
settings *Settings
sources []string
}
func NewWidget(app *tview.Application, pages *tview.Pages, settings *Settings) *Widget {
widget := Widget{
TextWidget: view.NewTextWidget(app, settings.common),
idx: 0,
settings: settings,
}
widget.client = NewClient(settings)
widget.View.SetBorderPadding(1, 1, 1, 1)
widget.View.SetWrap(true)
widget.View.SetWordWrap(true)
return &widget
}
func (widget *Widget) Refresh() {
widget.Redraw(widget.content)
}
func (widget *Widget) content() (string, string, bool) {
usernames := widget.client.screenNames
followerCounts := widget.client.GetFollowerCounts()
tweetCounts := widget.client.GetTweetCounts()
// Add header row
str := fmt.Sprintf("%-16s %-8s %-8s\n", "Username", "Followers", "# Tweets")
// Add rows for each of the followed usernames
for i, username := range usernames {
followerCount := followerCounts[i]
tweetCount := tweetCounts[i]
str += fmt.Sprintf("%-16s %8d %8d\n", username, followerCount, tweetCount)
}
return "Twitter Stats", str, true
}