diff --git a/modules/twitterstats/client.go b/modules/twitterstats/client.go index 247017ac..d3cd059b 100644 --- a/modules/twitterstats/client.go +++ b/modules/twitterstats/client.go @@ -11,21 +11,23 @@ import ( "golang.org/x/oauth2/clientcredentials" ) +// Client contains state that allows stats to be fetched about a list of Twitter users type Client struct { - apiBase string httpClient *http.Client screenNames []string } +// TwitterStats Represents a stats snapshot for a single Twitter user at a point in time type TwitterStats struct { followerCount int64 tweetCount int64 } const ( - userTimelineUrl = "https://api.twitter.com/1.1/users/show.json" + userTimelineURL = "https://api.twitter.com/1.1/users/show.json" ) +// NewClient creates a new twitterstats client that contains an OAuth2 HTTP client which can be used func NewClient(settings *Settings) *Client { usernames := make([]string, len(settings.screenNames)) for i, username := range settings.screenNames { @@ -45,12 +47,9 @@ func NewClient(settings *Settings) *Client { ClientSecret: settings.consumerSecret, TokenURL: "https://api.twitter.com/oauth2/token", } - - // token, err := conf.Token(oauth2.NoContext) httpClient := conf.Client(oauth2.NoContext) client := Client{ - apiBase: "https://api.twitter.com/1.1/", httpClient: httpClient, screenNames: usernames, } @@ -58,6 +57,8 @@ func NewClient(settings *Settings) *Client { return &client } +// GetStats Returns a slice of `TwitterStats` structs for each username in `client.screenNames` in the same +// order of `client.screenNames` func (client *Client) GetStats() []TwitterStats { stats := make([]TwitterStats, len(client.screenNames)) @@ -67,7 +68,7 @@ func (client *Client) GetStats() []TwitterStats { tweetCount: 0, } - res, err := client.httpClient.Get(fmt.Sprintf("%s?screen_name=%s", userTimelineUrl, username)) + res, err := client.httpClient.Get(fmt.Sprintf("%s?screen_name=%s", userTimelineURL, username)) if err != nil { continue } diff --git a/modules/twitterstats/settings.go b/modules/twitterstats/settings.go index 9229a59d..cd2122af 100644 --- a/modules/twitterstats/settings.go +++ b/modules/twitterstats/settings.go @@ -15,22 +15,17 @@ const ( type Settings struct { common *cfg.Common - consumerKey string - consumerSecret string - accessToken string - accessTokenSecret string - - screenNames []interface{} + consumerKey string + consumerSecret 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")), + consumerKey: ymlConfig.UString("consumerKey", os.Getenv("WTF_TWITTER_CONSUMER_KEY")), + consumerSecret: ymlConfig.UString("consumerSecret", os.Getenv("WTF_TWITTER_CONSUMER_SECRET")), screenNames: ymlConfig.UList("screenNames"), } diff --git a/modules/twitterstats/widget.go b/modules/twitterstats/widget.go index 26f65a7f..672915ec 100644 --- a/modules/twitterstats/widget.go +++ b/modules/twitterstats/widget.go @@ -8,25 +8,17 @@ import ( ) type Widget struct { - view.KeyboardWidget view.TextWidget - client *Client - idx int - settings *Settings - sources []string + client *Client } func NewWidget(app *tview.Application, pages *tview.Pages, settings *Settings) *Widget { widget := Widget{ TextWidget: view.NewTextWidget(app, settings.common), - - idx: 0, - settings: settings, + client: NewClient(settings), } - widget.client = NewClient(settings) - widget.View.SetBorderPadding(1, 1, 1, 1) widget.View.SetWrap(true) widget.View.SetWordWrap(true) @@ -43,7 +35,7 @@ func (widget *Widget) content() (string, string, bool) { stats := widget.client.GetStats() // Add header row - str := fmt.Sprintf("%-16s %-8s %-8s\n", "Username", "Followers", "# Tweets") + str := fmt.Sprintf("%-16s %8s %8s\n", "Username", "Followers", "Tweets") // Add rows for each of the followed usernames for i, username := range usernames {