forked from adamdrake/harold
-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
main.go
executable file
·125 lines (103 loc) · 2.77 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
package main
import (
"net/url"
"os"
"strconv"
"strings"
"time"
"log"
"github.com/ChimeraCoder/anaconda"
"github.com/aws/aws-lambda-go/lambda"
"flag"
)
var (
consumerKey = getenv("TWITTER_CONSUMER_KEY")
consumerSecret = getenv("TWITTER_CONSUMER_SECRET")
accessToken = getenv("TWITTER_ACCESS_TOKEN")
accessTokenSecret = getenv("TWITTER_ACCESS_TOKEN_SECRET")
maxTweetAge = getenv("MAX_TWEET_AGE")
whitelist = getWhitelist()
)
// MyResponse for AWS SAM
type MyResponse struct {
StatusCode string `json:"StatusCode"`
Message string `json:"Body"`
}
func getenv(name string) string {
v := os.Getenv(name)
if v == "" {
panic("missing required environment variable " + name)
}
return v
}
func getWhitelist() []string {
v := os.Getenv("WHITELIST")
if v == "" {
return make([]string, 0)
}
return strings.Split(v, ":")
}
func getTimeline(api *anaconda.TwitterApi) ([]anaconda.Tweet, error) {
args := url.Values{}
args.Add("count", "200") // 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 isWhitelisted(id int64, text string) bool {
tweetID := strconv.FormatInt(id, 10)
for _, w := range whitelist {
if w == tweetID || strings.Contains(text, w) {
return true
}
}
return false
}
func deleteFromTimeline(api *anaconda.TwitterApi, ageLimit time.Duration) {
timeline, err := getTimeline(api)
if err != nil {
log.Print("could not get timeline", err)
}
for _, t := range timeline {
createdTime, err := t.CreatedAtTime()
if err != nil {
log.Print("could not parse time ", err)
} else {
if time.Since(createdTime) > ageLimit && !isWhitelisted(t.Id, t.Text) {
_, err := api.DeleteTweet(t.Id, true)
log.Printf("DELETED TWEET (was %vh old): %v #%d - %s\n", time.Since(createdTime).Hours(), createdTime, t.Id, t.Text)
if err != nil {
log.Print("failed to delete: ", err)
}
}
}
}
log.Print("no more tweets to delete")
}
func ephemeral() (MyResponse, error) {
anaconda.SetConsumerKey(consumerKey)
anaconda.SetConsumerSecret(consumerSecret)
api := anaconda.NewTwitterApi(accessToken, accessTokenSecret)
api.SetLogger(anaconda.BasicLogger)
h, _ := time.ParseDuration(maxTweetAge)
deleteFromTimeline(api, h)
return MyResponse{
Message: "no more tweets to delete",
StatusCode: "200",
}, nil
}
func main() {
local := flag.Bool("local", false, "Specify to run in local script mode on the command line.")
flag.Parse()
if *local {
_, err := ephemeral()
if err != nil {
log.Fatal(err)
}
} else {
lambda.Start(ephemeral)
}
}