/
tweet.go
60 lines (49 loc) · 1.24 KB
/
tweet.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
package main
import (
"fmt"
"os"
"strings"
"github.com/ChimeraCoder/anaconda"
)
func initializeClient(ckey string, csecret string, atoken string, asecret string) anaconda.TwitterApi {
anaconda.SetConsumerKey(ckey)
anaconda.SetConsumerSecret(csecret)
api := anaconda.NewTwitterApi(atoken, asecret)
return *api
}
func isTweeted(subject string, api anaconda.TwitterApi) bool {
tweets, err := api.GetUserTimeline(nil)
if err != nil {
fmt.Println("Getting User timeline failed! Error : ", err)
os.Exit(1)
}
for _, tweet := range tweets {
fmt.Println(tweet.Text)
fmt.Println("searching subject :" + subject)
found := strings.Contains(tweet.Text, subject)
if found {
return true
}
}
return false
}
func updateStatus(subject string, api anaconda.TwitterApi) bool {
_, err := api.PostTweet(subject, nil)
if err != nil {
fmt.Println("Posting Tweet failed! Error : ", err)
os.Exit(1)
}
return true
}
func initiateTweet(ckey string, csecret string, atoken string, asecret string, subject string) {
api := initializeClient(ckey, csecret, atoken, asecret)
tweeted := isTweeted(subject, api)
fmt.Print(tweeted)
subjectPosted := true
if !tweeted {
subjectPosted = updateStatus(subject, api)
}
if !subjectPosted {
os.Exit(1)
}
}