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"
|
"golang.org/x/oauth2/clientcredentials"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Client contains state that allows stats to be fetched about a list of Twitter users
|
||||||
type Client struct {
|
type Client struct {
|
||||||
apiBase string
|
|
||||||
httpClient *http.Client
|
httpClient *http.Client
|
||||||
screenNames []string
|
screenNames []string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TwitterStats Represents a stats snapshot for a single Twitter user at a point in time
|
||||||
type TwitterStats struct {
|
type TwitterStats struct {
|
||||||
followerCount int64
|
followerCount int64
|
||||||
tweetCount int64
|
tweetCount int64
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
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 {
|
func NewClient(settings *Settings) *Client {
|
||||||
usernames := make([]string, len(settings.screenNames))
|
usernames := make([]string, len(settings.screenNames))
|
||||||
for i, username := range settings.screenNames {
|
for i, username := range settings.screenNames {
|
||||||
@ -45,12 +47,9 @@ func NewClient(settings *Settings) *Client {
|
|||||||
ClientSecret: settings.consumerSecret,
|
ClientSecret: settings.consumerSecret,
|
||||||
TokenURL: "https://api.twitter.com/oauth2/token",
|
TokenURL: "https://api.twitter.com/oauth2/token",
|
||||||
}
|
}
|
||||||
|
|
||||||
// token, err := conf.Token(oauth2.NoContext)
|
|
||||||
httpClient := conf.Client(oauth2.NoContext)
|
httpClient := conf.Client(oauth2.NoContext)
|
||||||
|
|
||||||
client := Client{
|
client := Client{
|
||||||
apiBase: "https://api.twitter.com/1.1/",
|
|
||||||
httpClient: httpClient,
|
httpClient: httpClient,
|
||||||
screenNames: usernames,
|
screenNames: usernames,
|
||||||
}
|
}
|
||||||
@ -58,6 +57,8 @@ func NewClient(settings *Settings) *Client {
|
|||||||
return &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 {
|
func (client *Client) GetStats() []TwitterStats {
|
||||||
stats := make([]TwitterStats, len(client.screenNames))
|
stats := make([]TwitterStats, len(client.screenNames))
|
||||||
|
|
||||||
@ -67,7 +68,7 @@ func (client *Client) GetStats() []TwitterStats {
|
|||||||
tweetCount: 0,
|
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 {
|
if err != nil {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
@ -17,9 +17,6 @@ type Settings struct {
|
|||||||
|
|
||||||
consumerKey string
|
consumerKey string
|
||||||
consumerSecret string
|
consumerSecret string
|
||||||
accessToken string
|
|
||||||
accessTokenSecret string
|
|
||||||
|
|
||||||
screenNames []interface{}
|
screenNames []interface{}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -29,8 +26,6 @@ func NewSettingsFromYAML(name string, ymlConfig *config.Config, globalConfig *co
|
|||||||
|
|
||||||
consumerKey: ymlConfig.UString("consumerKey", os.Getenv("WTF_TWITTER_CONSUMER_KEY")),
|
consumerKey: ymlConfig.UString("consumerKey", os.Getenv("WTF_TWITTER_CONSUMER_KEY")),
|
||||||
consumerSecret: ymlConfig.UString("consumerSecret", os.Getenv("WTF_TWITTER_CONSUMER_SECRET")),
|
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"),
|
screenNames: ymlConfig.UList("screenNames"),
|
||||||
}
|
}
|
||||||
|
@ -8,25 +8,17 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Widget struct {
|
type Widget struct {
|
||||||
view.KeyboardWidget
|
|
||||||
view.TextWidget
|
view.TextWidget
|
||||||
|
|
||||||
client *Client
|
client *Client
|
||||||
idx int
|
|
||||||
settings *Settings
|
|
||||||
sources []string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewWidget(app *tview.Application, pages *tview.Pages, settings *Settings) *Widget {
|
func NewWidget(app *tview.Application, pages *tview.Pages, settings *Settings) *Widget {
|
||||||
widget := Widget{
|
widget := Widget{
|
||||||
TextWidget: view.NewTextWidget(app, settings.common),
|
TextWidget: view.NewTextWidget(app, settings.common),
|
||||||
|
client: NewClient(settings),
|
||||||
idx: 0,
|
|
||||||
settings: settings,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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)
|
||||||
widget.View.SetWordWrap(true)
|
widget.View.SetWordWrap(true)
|
||||||
@ -43,7 +35,7 @@ func (widget *Widget) content() (string, string, bool) {
|
|||||||
stats := widget.client.GetStats()
|
stats := widget.client.GetStats()
|
||||||
|
|
||||||
// Add header row
|
// 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
|
// Add rows for each of the followed usernames
|
||||||
for i, username := range usernames {
|
for i, username := range usernames {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user