mirror of
https://github.com/taigrr/wtf
synced 2025-01-18 04:03:14 -08:00
Improve styling + remove unused code
* Got rid of unused struct fields, unused settings, added some comments to functions + structs
This commit is contained in:
parent
0be63a404c
commit
3c95d8e39d
@ -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
|
||||
}
|
||||
|
@ -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"),
|
||||
}
|
||||
|
@ -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 {
|
||||
|
Loading…
x
Reference in New Issue
Block a user