-
Notifications
You must be signed in to change notification settings - Fork 25
/
main.go
235 lines (207 loc) · 5.8 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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package main
import (
"errors"
"flag"
"math/rand"
"net/http"
"net/url"
"os"
"sync"
"time"
"github.com/ChimeraCoder/anaconda"
log "github.com/Sirupsen/logrus"
"github.com/mmcdole/gofeed"
"github.com/spf13/viper"
)
var (
logger = log.New()
)
func randomItemFromFeed(feedURL string) (gofeed.Item, error) {
// Reddit (for example) will return an HTTP 429 if no User-Agent string is provided...
userAgent := "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:58.0) Gecko/20100101 Firefox/58.0"
req, err := http.NewRequest("GET", feedURL, nil)
if err != nil {
return gofeed.Item{}, errors.New("Could not construct http request")
}
req.Header.Add("User-Agent", userAgent)
client := http.Client{}
resp, err := client.Do(req)
fp := gofeed.NewParser()
feed, err := fp.Parse(resp.Body)
resp.Body.Close() // Close response body in order to avoid leaks
if err != nil {
return gofeed.Item{}, errors.New("Could not parse feed")
}
if feed == nil {
return gofeed.Item{}, errors.New("Gofeed returned nil feed!")
}
if len(feed.Items) < 1 {
return gofeed.Item{}, errors.New("Empty feed returned from URL")
}
idx := rand.Intn(len(feed.Items))
return *feed.Items[idx], nil
}
func randomTweetFromFeed(api *anaconda.TwitterApi, feedURL, hashtags string, frequency time.Duration) {
for {
item, err := randomItemFromFeed(feedURL)
if err != nil {
log.Error(err)
continue
}
tweetText := item.Title + " " + item.Link + " " + hashtags
tweet, err := api.PostTweet(tweetText, url.Values{})
if err != nil {
log.Error("Could not post tweet: ", tweet, " ", err)
time.Sleep(60 * time.Second) // In case of failure, don't hammer any sites
continue
}
log.Info("TWEETED ", tweet.Text)
waitTime := time.Duration(rand.Intn(int(frequency.Seconds()))) * time.Second
log.Info("Sleeping ", waitTime, " before tweeting again...")
time.Sleep(waitTime)
}
}
func getTimeline(api *anaconda.TwitterApi) ([]anaconda.Tweet, error) {
args := url.Values{}
args.Add("count", "3200") // Twitter only returns most recent 20 tweets by default, so override
args.Add("include_rts", "true") // When using count argument, RTs are excluded, so include them as recommended
timeline, err := api.GetUserTimeline(args)
if err != nil {
return make([]anaconda.Tweet, 0), err
}
return timeline, nil
}
func deleteTweets(tweets []anaconda.Tweet, api *anaconda.TwitterApi) error {
for _, t := range tweets {
_, err := api.DeleteTweet(t.Id, true)
if err != nil {
return err
}
}
return nil
}
func deleteAllTweets(api *anaconda.TwitterApi) error {
timeline, err := getTimeline(api)
for len(timeline) > 1 {
err = deleteTweets(timeline, api)
if err != nil {
return err
}
timeline, err = getTimeline(api)
if err != nil {
return err
}
}
return nil
}
func deleteFromTimeline(api *anaconda.TwitterApi, ageLimit, sleepTime time.Duration) {
for {
timeline, err := getTimeline(api)
if err != nil {
log.Error("Could not get timeline")
}
for _, t := range timeline {
createdTime, err := t.CreatedAtTime()
if err != nil {
log.Error("Couldn't parse time ", err)
} else {
if time.Since(createdTime) > ageLimit {
_, err := api.DeleteTweet(t.Id, true)
log.Info("DELETED: Age - ", time.Since(createdTime).Round(1*time.Minute), " - ", t.Text)
if err != nil {
log.Error("Failed to delete! ", err)
}
}
}
}
time.Sleep(sleepTime)
}
}
func getDMs(api *anaconda.TwitterApi) ([]anaconda.DirectMessage, error) {
args := url.Values{}
args.Add("count", "200")
dmsRecvd, err := api.GetDirectMessages(args)
if err != nil {
return make([]anaconda.DirectMessage, 0), err
}
dmsSent, err := api.GetDirectMessagesSent(args)
if err != nil {
return make([]anaconda.DirectMessage, 0), err
}
return append(dmsRecvd, dmsSent...), nil
}
func destroyDMs(api *anaconda.TwitterApi) {
dms, err := getDMs(api)
if err != nil {
log.Error(err)
}
if len(dms) > 0 {
for _, dm := range dms {
m, err := api.DeleteDirectMessage(dm.Id, true)
if err != nil {
log.Error("Could not delete DM ", err, m)
}
log.Info("Deleted DM from ", m.CreatedAt, " ", m.Text)
time.Sleep(1 * time.Second)
}
destroyDMs(api)
}
}
type config struct {
CleanTimeline bool
MaxTweetAge time.Duration
CleanTimelineEvery time.Duration
Feeds map[string]feed
}
type feed struct {
URL, Hashtags string
MaxTweetInterval time.Duration
}
func main() {
rand.Seed(time.Now().UnixNano())
var deleteDMs = flag.Bool("deletedm", false, "Only delete direct messages, then exit")
var configPath = flag.String("config", "config.toml", "Path to the configuration file (in TOML format)")
var deleteTimeline = flag.Bool("deletetimeline", false, "Delete all tweets in timeline, then exit")
var botMode = flag.Bool("botmode", false, "Run in bot mode (tweet from feeds, keep timeline groomed, etc.)")
flag.Parse()
viper.SetConfigFile(*configPath)
err := viper.ReadInConfig()
if err != nil {
panic(err)
}
var c config
err = viper.Unmarshal(&c)
if err != nil {
panic(err)
}
viper.AutomaticEnv()
anaconda.SetConsumerKey(viper.GetString("TWITTER_CONSUMER_KEY"))
anaconda.SetConsumerSecret(viper.GetString("TWITTER_CONSUMER_SECRET"))
api := anaconda.NewTwitterApi(viper.GetString("TWITTER_ACCESS_TOKEN"), viper.GetString("TWITTER_ACCESS_TOKEN_SECRET"))
api.SetLogger(anaconda.BasicLogger)
fmter := new(log.TextFormatter)
fmter.FullTimestamp = true
log.SetFormatter(fmter)
log.SetLevel(log.InfoLevel)
if *deleteDMs {
destroyDMs(api)
os.Exit(0)
}
if *deleteTimeline {
deleteAllTweets(api)
os.Exit(0)
}
if *botMode {
if len(c.Feeds) > 0 {
for _, f := range c.Feeds {
go randomTweetFromFeed(api, f.URL, f.Hashtags, f.MaxTweetInterval)
}
}
if c.CleanTimeline {
go deleteFromTimeline(api, c.MaxTweetAge, c.CleanTimelineEvery)
}
var wg sync.WaitGroup
wg.Add(1)
wg.Wait()
}
}