mirror of
https://github.com/taigrr/wtf
synced 2025-01-18 04:03:14 -08:00
commit
ada434b3c3
@ -55,6 +55,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"
|
||||||
@ -243,6 +244,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)
|
||||||
|
@ -1,9 +1,14 @@
|
|||||||
package twitter
|
package twitter
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
|
"golang.org/x/oauth2"
|
||||||
|
"golang.org/x/oauth2/clientcredentials"
|
||||||
)
|
)
|
||||||
|
|
||||||
/* NOTE: Currently single application ONLY
|
/* NOTE: Currently single application ONLY
|
||||||
@ -13,18 +18,36 @@ import (
|
|||||||
// Client represents the data required to connect to the Twitter API
|
// Client represents the data required to connect to the Twitter API
|
||||||
type Client struct {
|
type Client struct {
|
||||||
apiBase string
|
apiBase string
|
||||||
bearerToken string
|
|
||||||
count int
|
count int
|
||||||
screenName string
|
screenName string
|
||||||
|
httpClient *http.Client
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewClient creates and returns a new Twitter client
|
// NewClient creates and returns a new Twitter client
|
||||||
func NewClient(settings *Settings) *Client {
|
func NewClient(settings *Settings) *Client {
|
||||||
|
var httpClient *http.Client
|
||||||
|
// If a bearer token is supplied, use that directly. Otherwise, let the Oauth client fetch a token
|
||||||
|
// using the consumer key and secret.
|
||||||
|
if settings.bearerToken == "" {
|
||||||
|
conf := &clientcredentials.Config{
|
||||||
|
ClientID: settings.consumerKey,
|
||||||
|
ClientSecret: settings.consumerSecret,
|
||||||
|
TokenURL: "https://api.twitter.com/oauth2/token",
|
||||||
|
}
|
||||||
|
httpClient = conf.Client(oauth2.NoContext)
|
||||||
|
} else {
|
||||||
|
ctx := context.Background()
|
||||||
|
httpClient = oauth2.NewClient(ctx, oauth2.StaticTokenSource(&oauth2.Token{
|
||||||
|
AccessToken: settings.bearerToken,
|
||||||
|
TokenType: "Bearer",
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
client := Client{
|
client := Client{
|
||||||
apiBase: "https://api.twitter.com/1.1/",
|
apiBase: "https://api.twitter.com/1.1/",
|
||||||
count: settings.count,
|
count: settings.count,
|
||||||
screenName: "",
|
screenName: "",
|
||||||
bearerToken: settings.bearerToken,
|
httpClient: httpClient,
|
||||||
}
|
}
|
||||||
|
|
||||||
return &client
|
return &client
|
||||||
@ -53,7 +76,7 @@ func (client *Client) tweets() (tweets []Tweet, err error) {
|
|||||||
strconv.Itoa(client.count),
|
strconv.Itoa(client.count),
|
||||||
)
|
)
|
||||||
|
|
||||||
data, err := Request(client.bearerToken, apiURL)
|
data, err := Request(client.httpClient, apiURL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return tweets, err
|
return tweets, err
|
||||||
}
|
}
|
||||||
|
@ -2,21 +2,11 @@ package twitter
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Request(bearerToken string, apiURL string) ([]byte, error) {
|
func Request(httpClient *http.Client, apiURL string) ([]byte, error) {
|
||||||
req, err := http.NewRequest("GET", apiURL, nil)
|
resp, err := httpClient.Get(apiURL)
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Expected authorization format for single-application twitter dev accounts
|
|
||||||
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", bearerToken))
|
|
||||||
|
|
||||||
client := &http.Client{}
|
|
||||||
resp, err := client.Do(req)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -16,6 +16,8 @@ type Settings struct {
|
|||||||
common *cfg.Common
|
common *cfg.Common
|
||||||
|
|
||||||
bearerToken string
|
bearerToken string
|
||||||
|
consumerKey string
|
||||||
|
consumerSecret string
|
||||||
count int
|
count int
|
||||||
screenNames []interface{}
|
screenNames []interface{}
|
||||||
}
|
}
|
||||||
@ -25,6 +27,8 @@ func NewSettingsFromYAML(name string, ymlConfig *config.Config, globalConfig *co
|
|||||||
common: cfg.NewCommonSettingsFromModule(name, defaultTitle, defaultFocusable, ymlConfig, globalConfig),
|
common: cfg.NewCommonSettingsFromModule(name, defaultTitle, defaultFocusable, ymlConfig, globalConfig),
|
||||||
|
|
||||||
bearerToken: ymlConfig.UString("bearerToken", os.Getenv("WTF_TWITTER_BEARER_TOKEN")),
|
bearerToken: ymlConfig.UString("bearerToken", os.Getenv("WTF_TWITTER_BEARER_TOKEN")),
|
||||||
|
consumerKey: ymlConfig.UString("consumerKey", os.Getenv("WTF_TWITTER_CONSUMER_KEY")),
|
||||||
|
consumerSecret: ymlConfig.UString("consumerSecret", os.Getenv("WTF_TWITTER_CONSUMER_SECRET")),
|
||||||
count: ymlConfig.UInt("count", 5),
|
count: ymlConfig.UInt("count", 5),
|
||||||
screenNames: ymlConfig.UList("screenName"),
|
screenNames: ymlConfig.UList("screenName"),
|
||||||
}
|
}
|
||||||
|
104
modules/twitterstats/client.go
Normal file
104
modules/twitterstats/client.go
Normal file
@ -0,0 +1,104 @@
|
|||||||
|
package twitterstats
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"golang.org/x/oauth2"
|
||||||
|
"golang.org/x/oauth2/clientcredentials"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Client contains state that allows stats to be fetched about a list of Twitter users
|
||||||
|
type Client struct {
|
||||||
|
httpClient *http.Client
|
||||||
|
screenNames []string
|
||||||
|
bearerToken string
|
||||||
|
}
|
||||||
|
|
||||||
|
// TwitterStats Represents a stats snapshot for a single Twitter user at a point in time
|
||||||
|
type TwitterStats struct {
|
||||||
|
FollowerCount int64 `json:"followers_count"`
|
||||||
|
TweetCount int64 `json:"statuses_count"`
|
||||||
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
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 {
|
||||||
|
var ok bool
|
||||||
|
if usernames[i], ok = username.(string); !ok {
|
||||||
|
log.Fatalf("All `screenName`s in twitterstats config must be of type string")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var httpClient *http.Client
|
||||||
|
// If a bearer token is supplied, use that directly. Otherwise, let the Oauth client fetch a token
|
||||||
|
// using the consumer key and secret.
|
||||||
|
if settings.bearerToken == "" {
|
||||||
|
conf := &clientcredentials.Config{
|
||||||
|
ClientID: settings.consumerKey,
|
||||||
|
ClientSecret: settings.consumerSecret,
|
||||||
|
TokenURL: "https://api.twitter.com/oauth2/token",
|
||||||
|
}
|
||||||
|
httpClient = conf.Client(oauth2.NoContext)
|
||||||
|
} else {
|
||||||
|
ctx := context.Background()
|
||||||
|
httpClient = oauth2.NewClient(ctx, oauth2.StaticTokenSource(&oauth2.Token{
|
||||||
|
AccessToken: settings.bearerToken,
|
||||||
|
TokenType: "Bearer",
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
|
client := Client{
|
||||||
|
httpClient: httpClient,
|
||||||
|
screenNames: usernames,
|
||||||
|
}
|
||||||
|
|
||||||
|
return &client
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetStatsForUser Fetches stats for a single user. If there is an error fetching or parsing the response
|
||||||
|
// from the Twitter API, an empty stats struct will be returned.
|
||||||
|
func (client *Client) GetStatsForUser(username string) TwitterStats {
|
||||||
|
stats := TwitterStats{
|
||||||
|
FollowerCount: 0,
|
||||||
|
TweetCount: 0,
|
||||||
|
}
|
||||||
|
|
||||||
|
url := fmt.Sprintf("%s?screen_name=%s", userTimelineURL, username)
|
||||||
|
res, err := client.httpClient.Get(url)
|
||||||
|
if err != nil {
|
||||||
|
return stats
|
||||||
|
}
|
||||||
|
defer res.Body.Close()
|
||||||
|
|
||||||
|
body, err := ioutil.ReadAll(res.Body)
|
||||||
|
if err != nil {
|
||||||
|
return stats
|
||||||
|
}
|
||||||
|
|
||||||
|
// If there is an error while parsing, just discard the error and return the empty stats
|
||||||
|
json.Unmarshal(body, &stats)
|
||||||
|
|
||||||
|
return stats
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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))
|
||||||
|
|
||||||
|
for i, username := range client.screenNames {
|
||||||
|
stats[i] = client.GetStatsForUser(username)
|
||||||
|
}
|
||||||
|
|
||||||
|
return stats
|
||||||
|
}
|
36
modules/twitterstats/settings.go
Normal file
36
modules/twitterstats/settings.go
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
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
|
||||||
|
|
||||||
|
bearerToken string
|
||||||
|
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),
|
||||||
|
|
||||||
|
bearerToken: ymlConfig.UString("bearerToken", os.Getenv("WTF_TWITTER_BEARER_TOKEN")),
|
||||||
|
consumerKey: ymlConfig.UString("consumerKey", os.Getenv("WTF_TWITTER_CONSUMER_KEY")),
|
||||||
|
consumerSecret: ymlConfig.UString("consumerSecret", os.Getenv("WTF_TWITTER_CONSUMER_SECRET")),
|
||||||
|
|
||||||
|
screenNames: ymlConfig.UList("screenNames"),
|
||||||
|
}
|
||||||
|
|
||||||
|
return &settings
|
||||||
|
}
|
49
modules/twitterstats/widget.go
Normal file
49
modules/twitterstats/widget.go
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
package twitterstats
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/rivo/tview"
|
||||||
|
"github.com/wtfutil/wtf/view"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Widget struct {
|
||||||
|
view.TextWidget
|
||||||
|
|
||||||
|
client *Client
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewWidget(app *tview.Application, pages *tview.Pages, settings *Settings) *Widget {
|
||||||
|
widget := Widget{
|
||||||
|
TextWidget: view.NewTextWidget(app, settings.common),
|
||||||
|
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
|
||||||
|
stats := widget.client.GetStats()
|
||||||
|
|
||||||
|
// 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 := stats[i].FollowerCount
|
||||||
|
tweetCount := stats[i].TweetCount
|
||||||
|
|
||||||
|
str += fmt.Sprintf("%-16s %8d %8d\n", username, followerCount, tweetCount)
|
||||||
|
}
|
||||||
|
|
||||||
|
return "Twitter Stats", str, true
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user