-
Notifications
You must be signed in to change notification settings - Fork 0
/
tweetbot.go
162 lines (134 loc) · 3.9 KB
/
tweetbot.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
package main
import (
"fmt"
"io"
"io/ioutil"
"log"
"os"
"strconv"
"github.com/adam-lavrik/go-imath/ix"
"github.com/dghubble/oauth1"
"github.com/tomasnorre/go-twitter/twitter"
"gopkg.in/yaml.v2"
)
type Configuration struct {
Twitter TwitterConf `yaml:"twitter"`
}
type TwitterConf struct {
OauthAccessToken string `yaml:"oauth_access_token"`
OauthAccessTokenSecret string `yaml:"oauth_access_token_secret"`
ConsumerKey string `yaml:"consumer_key"`
ConsumerSecret string `yaml:"consumer_secret"`
Hash []string `yaml:"hash"`
}
func closeFile(file *os.File) {
err := file.Close()
if err != nil {
log.Printf("could not close file %s: %s", file.Name(), err)
}
}
// WriteToFile will print any string of text to a file safely by
// checking for errors and syncing at the end.
func WriteToFile(filename string, data string) error {
file, err := os.Create(filename)
if err != nil {
return fmt.Errorf("could not open file: %s", err)
}
defer closeFile(file)
_, err = io.WriteString(file, data)
if err != nil {
return fmt.Errorf("could not write string: %s", err)
}
return nil
}
func getLastTweetID() (int64, error) {
lastTweetIdFile, err := os.Open("lastTweetId")
if err != nil {
return 0, fmt.Errorf("could not open lastTweetId: %s", err)
}
defer closeFile(lastTweetIdFile)
lastTweetId, err := ioutil.ReadAll(lastTweetIdFile)
if err != nil {
return 0, fmt.Errorf("could not read lastTweetId content: %s", err)
}
lastTweetIdValue, err := strconv.ParseInt(string(lastTweetId), 10, 64)
if err != nil {
return 0, fmt.Errorf("could not parse lastTweetId content as int64: %s", err)
}
return lastTweetIdValue, nil
}
func main() {
file, err := os.Open("settings.yaml")
if err != nil {
log.Fatalf("could not open file: %s", err)
}
defer closeFile(file)
filecontent, err := ioutil.ReadAll(file)
if err != nil {
log.Fatalf("could not read settings.yaml content: %s", err)
}
var conf Configuration
err = yaml.Unmarshal(filecontent, &conf)
if err != nil {
log.Fatalf("could not unmarshal settings.yaml content: %s", err)
}
var lastTweetID int64
if id, err := getLastTweetID(); err != nil {
log.Printf("could not get last tweet id: %s", err)
} else {
lastTweetID = id
}
config := oauth1.NewConfig(conf.Twitter.ConsumerKey, conf.Twitter.ConsumerSecret)
token := oauth1.NewToken(conf.Twitter.OauthAccessToken, conf.Twitter.OauthAccessTokenSecret)
httpClient := config.Client(oauth1.NoContext, token)
// Twitter client
client := twitter.NewClient(httpClient)
out := make(chan *twitter.Search)
for _, h := range conf.Twitter.Hash {
go func(hash string) {
// Search Tweets
params := &twitter.SearchTweetParams{
Query: hash,
Count: 5,
ResultType: "recent",
}
if lastTweetID != 0 {
params.SinceID = lastTweetID
}
search, _, err := client.Search.Tweets(params)
if err != nil {
log.Printf("could not search tweets for hash '%s': %s", hash, err)
// a search did not execute properly, send an empty object so we don't deadlock
out <- &twitter.Search{}
return
}
out <- search
}(h)
}
var tweetIds []int
for i := 0; i < len(conf.Twitter.Hash); i++ {
search := <-out
for _, tweet := range search.Statuses {
if tweet.RetweetedStatus != nil {
continue
}
tweetIds = append(tweetIds, int(tweet.ID))
fmt.Println(tweet.ID, tweet.Text)
var statusRetweetParam *twitter.StatusRetweetParams
_, _, err = client.Statuses.Retweet(tweet.ID, statusRetweetParam)
if err != nil {
log.Printf("could not retweet for hash '%s': %s", conf.Twitter.Hash[i], err)
continue
}
}
}
close(out)
// Write latestTweetId to know where to start on next execution.
if 0 < len(tweetIds) {
latestTweetId := strconv.Itoa(ix.MaxSlice(tweetIds))
err := WriteToFile("lastTweetId", latestTweetId)
if err != nil {
log.Fatalf("could not write lastTweetId to file: %s", err)
}
}
}