-
Notifications
You must be signed in to change notification settings - Fork 0
/
user_data.go
123 lines (100 loc) · 3.19 KB
/
user_data.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package main
import (
"encoding/json"
"fmt"
"net/http"
"time"
"golang.org/x/oauth2"
"golang.org/x/oauth2/clientcredentials"
)
type userLookup struct {
Id string `json:"id"`
Name string `json:"name"`
Username string `json:"username"`
}
type publicMetrics struct {
FollowerCount int `json:"followers_count"`
FollowingCount int `json:"following_count"`
TweetCount int `json:"tweet_count"`
ListedCount int `json:"listed_count"`
}
type user struct {
PublicMetrics publicMetrics `json:"public_metrics"`
Location string
}
func getTwitterOauth2Client(consumerKey string, consumerSecret string) *http.Client {
config := &clientcredentials.Config{
ClientID: consumerKey,
ClientSecret: consumerSecret,
TokenURL: "https://api.twitter.com/oauth2/token",
}
return config.Client(oauth2.NoContext)
}
func fetchIdForUsername(client *http.Client, username string) (string, error) {
req, err := http.NewRequest("GET", "https://api.twitter.com/2/users/by/username/"+username, nil)
if err != nil {
return "", err
}
resp, err := client.Do(req)
if err != nil {
return "", err
}
dec := json.NewDecoder(resp.Body)
var data = make(map[string]userLookup)
err = dec.Decode(&data)
if err != nil {
return "", err
}
return data["data"].Id, nil
}
func fetchUserData(client *http.Client, userId string) (map[string]user, error) {
req, err := http.NewRequest("GET", "https://api.twitter.com/2/users/"+userId+"?user.fields=public_metrics,location", nil)
if err != nil {
return make(map[string]user), err
}
resp, err := client.Do(req)
if err != nil {
return make(map[string]user), err
}
dec := json.NewDecoder(resp.Body)
userData := make(map[string]user)
err = dec.Decode(&userData)
if err != nil {
return make(map[string]user), err
}
return userData, nil
}
func GetTextLines(metrics map[string]string, promotionalLine string) []string {
lines := []string{
fmt.Sprintf("%s Following", metrics["following_count"]),
fmt.Sprintf("%s Followers", metrics["followers_count"]),
fmt.Sprintf("%s Tweets", metrics["tweet_count"]),
fmt.Sprintf("In %s lists", metrics["listed_count"]),
}
if metrics["location"] != "" {
lines = append(lines, fmt.Sprintf("Located in %s", metrics["location"]))
}
lines = append(lines, fmt.Sprintf("%s", time.Now().UTC().Format("2006-01-02 15:04 MST")))
if promotionalLine != "" {
lines = append(lines, promotionalLine)
}
return lines
}
func GetTwitterUserData(consumerKey string, consumerSecret string, username string) (map[string]string, error) {
client := getTwitterOauth2Client(consumerKey, consumerSecret)
userId, err := fetchIdForUsername(client, username)
if err != nil {
return make(map[string]string), err
}
userData, err := fetchUserData(client, userId)
if err != nil {
return make(map[string]string), err
}
return map[string]string{
"followers_count": fmt.Sprintf("%d", userData["data"].PublicMetrics.FollowerCount),
"following_count": fmt.Sprintf("%d", userData["data"].PublicMetrics.FollowingCount),
"tweet_count": fmt.Sprintf("%d", userData["data"].PublicMetrics.TweetCount),
"listed_count": fmt.Sprintf("%d", userData["data"].PublicMetrics.ListedCount),
"location": userData["data"].Location,
}, nil
}