mirror of
https://github.com/taigrr/wtf
synced 2025-01-18 04:03:14 -08:00
Implement Twitter API fetching for twitterstats
* Create Oauth2 client configured for Twitter and create a HTTP client out of that * Fetch user stats for each of the users, parse out of JSON, and return stats as stats structs
This commit is contained in:
parent
4c9990bafd
commit
0be63a404c
@ -1,18 +1,31 @@
|
|||||||
package twitterstats
|
package twitterstats
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"golang.org/x/oauth2"
|
||||||
|
"golang.org/x/oauth2/clientcredentials"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Client struct {
|
type Client struct {
|
||||||
apiBase string
|
apiBase string
|
||||||
consumerKey string
|
httpClient *http.Client
|
||||||
consumerSecret string
|
screenNames []string
|
||||||
accessToken string
|
|
||||||
accessTokenSecret string
|
|
||||||
screenNames []string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type TwitterStats struct {
|
||||||
|
followerCount int64
|
||||||
|
tweetCount int64
|
||||||
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
userTimelineUrl = "https://api.twitter.com/1.1/users/show.json"
|
||||||
|
)
|
||||||
|
|
||||||
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 {
|
||||||
@ -27,22 +40,55 @@ func NewClient(settings *Settings) *Client {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
conf := &clientcredentials.Config{
|
||||||
|
ClientID: settings.consumerKey,
|
||||||
|
ClientSecret: settings.consumerSecret,
|
||||||
|
TokenURL: "https://api.twitter.com/oauth2/token",
|
||||||
|
}
|
||||||
|
|
||||||
|
// token, err := conf.Token(oauth2.NoContext)
|
||||||
|
httpClient := conf.Client(oauth2.NoContext)
|
||||||
|
|
||||||
client := Client{
|
client := Client{
|
||||||
apiBase: "https://api.twitter.com/1.1/",
|
apiBase: "https://api.twitter.com/1.1/",
|
||||||
consumerKey: settings.consumerKey,
|
httpClient: httpClient,
|
||||||
consumerSecret: settings.consumerSecret,
|
screenNames: usernames,
|
||||||
accessToken: settings.accessToken,
|
|
||||||
accessTokenSecret: settings.accessTokenSecret,
|
|
||||||
screenNames: usernames,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return &client
|
return &client
|
||||||
}
|
}
|
||||||
|
|
||||||
func (client *Client) GetFollowerCounts() []int64 {
|
func (client *Client) GetStats() []TwitterStats {
|
||||||
return []int64{0, 0, 0, 0, 0} // TODO
|
stats := make([]TwitterStats, len(client.screenNames))
|
||||||
}
|
|
||||||
|
|
||||||
func (client *Client) GetTweetCounts() []int64 {
|
for i, username := range client.screenNames {
|
||||||
return []int64{0, 0, 0, 0, 0} // TODO
|
stats[i] = TwitterStats{
|
||||||
|
followerCount: 0,
|
||||||
|
tweetCount: 0,
|
||||||
|
}
|
||||||
|
|
||||||
|
res, err := client.httpClient.Get(fmt.Sprintf("%s?screen_name=%s", userTimelineUrl, username))
|
||||||
|
if err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
defer res.Body.Close()
|
||||||
|
|
||||||
|
body, err := ioutil.ReadAll(res.Body)
|
||||||
|
if err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
var parsed map[string]interface{}
|
||||||
|
err = json.Unmarshal(body, &parsed)
|
||||||
|
if err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
stats[i] = TwitterStats{
|
||||||
|
followerCount: int64(parsed["followers_count"].(float64)),
|
||||||
|
tweetCount: int64(parsed["statuses_count"].(float64)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return stats
|
||||||
}
|
}
|
||||||
|
@ -40,16 +40,15 @@ func (widget *Widget) Refresh() {
|
|||||||
|
|
||||||
func (widget *Widget) content() (string, string, bool) {
|
func (widget *Widget) content() (string, string, bool) {
|
||||||
usernames := widget.client.screenNames
|
usernames := widget.client.screenNames
|
||||||
followerCounts := widget.client.GetFollowerCounts()
|
stats := widget.client.GetStats()
|
||||||
tweetCounts := widget.client.GetTweetCounts()
|
|
||||||
|
|
||||||
// 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 {
|
||||||
followerCount := followerCounts[i]
|
followerCount := stats[i].followerCount
|
||||||
tweetCount := tweetCounts[i]
|
tweetCount := stats[i].tweetCount
|
||||||
|
|
||||||
str += fmt.Sprintf("%-16s %8d %8d\n", username, followerCount, tweetCount)
|
str += fmt.Sprintf("%-16s %8d %8d\n", username, followerCount, tweetCount)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user