-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
214 lines (170 loc) · 4.47 KB
/
main.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package main
import (
"bufio"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"math/rand"
"net/http"
"os"
"time"
"github.com/aws/aws-lambda-go/lambda"
"github.com/dghubble/go-twitter/twitter"
"github.com/dghubble/oauth1"
)
type Credentials struct {
ConsumerKey string
ConsumerSecret string
AccessToken string
AccessTokenSecret string
}
type Country struct {
Name struct {
Common string `json:"common"`
} `json:"name"`
Capital []string `json:"capital"`
}
func main() {
lambda.Start(executeBot)
}
func getClient(creds *Credentials) (*twitter.Client, error) {
config := oauth1.NewConfig(creds.ConsumerKey, creds.ConsumerSecret)
token := oauth1.NewToken(creds.AccessToken, creds.AccessTokenSecret)
httpClient := config.Client(oauth1.NoContext, token)
client := twitter.NewClient(httpClient)
verifyParams := &twitter.AccountVerifyParams{
SkipStatus: twitter.Bool(true),
IncludeEmail: twitter.Bool(true),
}
user, _, err := client.Accounts.VerifyCredentials(verifyParams)
if err != nil {
return nil, err
}
log.Printf("User's ACCOUNT: %s\n", user.Name)
return client, nil
}
func executeBot() {
log.Printf("Starting CapitaisBot...")
log.Printf("Getting Credentials from environment...")
creds := Credentials{
AccessToken: os.Getenv("ACCESS_TOKEN"),
AccessTokenSecret: os.Getenv("ACCESS_TOKEN_SECRET"),
ConsumerKey: os.Getenv("CONSUMER_KEY"),
ConsumerSecret: os.Getenv("CONSUMER_SECRET"),
}
log.Printf("Preparing request...\n")
url := os.Getenv("COUNTRY_API_URL")
countryclient, req := prepareRequest(url)
log.Printf("Calling countries API...\n")
res := getCountries(countryclient, req)
log.Printf("Parsing Data...\n")
countries := parseResponse(res)
fmt.Println(countries[0])
log.Printf("Getting Twitter client...\n")
client, err := getClient(&creds)
if err != nil {
log.Fatal("--- Error getting Twitter Client, shutting down app :( --- ")
log.Println(err)
}
isCountryAlreadyTweeted := true
countryToTweet := Country{}
for {
countryToTweet = getRandomCountry(countries)
isCountryAlreadyTweeted = stringInSlice(countryToTweet.Name.Common, countries)
if !isCountryAlreadyTweeted {
break
} else {
log.Printf("Country already tweeted, getting another one ;)\n")
}
}
clearRequestData(res)
tweet(client, &countryToTweet)
writeCountryNameInFile(err, countryToTweet)
}
func stringInSlice(a string, list []Country) bool {
for _, b := range list {
if b.Name.Common == a {
return false
}
}
return true
}
func writeCountryNameInFile(err error, countryToTweet Country) {
f, fileErr := os.OpenFile("data/TweetedCountries.txt", os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
if fileErr != nil {
panic(err)
}
defer f.Close()
if _, err = f.WriteString(countryToTweet.Name.Common + "\n"); err != nil {
panic(err)
}
}
func readCountriesFromFile() ([]string, error) {
file, err := os.Open("data/TweetedCountries.txt")
if err != nil {
return nil, err
}
defer file.Close()
var lines []string
scanner := bufio.NewScanner(file)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
return lines, scanner.Err()
}
func getRandomCountry(countries []Country) Country {
num := getRandomInt()
return countries[num]
}
func getRandomInt() int {
rand.Seed(time.Now().UnixNano())
min := 0
max := 248
return rand.Intn(max-min+1) + min
}
func tweet(client *twitter.Client, country *Country) {
tweet, _, err := client.Statuses.Update("Country: "+country.Name.Common+" - Capital City: "+country.Capital[0], nil)
if err != nil {
log.Printf("Error tweeting!")
log.Fatal(err)
}
log.Printf("Success tweeting!")
log.Printf("%+v\n", tweet)
}
func parseResponse(res *http.Response) []Country {
body, readErr := ioutil.ReadAll(res.Body)
if readErr != nil {
log.Fatal(readErr)
}
fmt.Println(res)
countries := []Country{}
jsonErr := json.Unmarshal(body, &countries)
if jsonErr != nil {
log.Fatal(jsonErr)
}
return countries
}
func clearRequestData(res *http.Response) {
if res.Body != nil {
defer res.Body.Close()
}
}
func prepareRequest(url string) (http.Client, *http.Request) {
countryclient := http.Client{
Timeout: time.Second * 2,
}
req, countryerr := http.NewRequest(http.MethodGet, url, nil)
if countryerr != nil {
log.Fatal(countryerr)
}
req.Header.Set("User-Agent", "capitaisBot")
return countryclient, req
}
func getCountries(countryclient http.Client, req *http.Request) *http.Response {
res, getErr := countryclient.Do(req)
if getErr != nil {
log.Fatal(getErr)
}
return res
}