Skip to content

Commit

Permalink
Merge pull request #3 from sw00/master
Browse files Browse the repository at this point in the history
Add a way to whitelist tweets
  • Loading branch information
Vicky Lai authored Oct 19, 2018
2 parents 17ecbec + 7e1430b commit 862b84d
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package main
import (
"net/url"
"os"
"strconv"
"strings"
"time"

"log"
Expand All @@ -17,6 +19,7 @@ var (
accessToken = getenv("TWITTER_ACCESS_TOKEN")
accessTokenSecret = getenv("TWITTER_ACCESS_TOKEN_SECRET")
maxTweetAge = getenv("MAX_TWEET_AGE")
whitelist = getWhitelist()
)

func getenv(name string) string {
Expand All @@ -27,6 +30,16 @@ func getenv(name string) string {
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
Expand All @@ -38,6 +51,17 @@ func getTimeline(api *anaconda.TwitterApi) ([]anaconda.Tweet, error) {
return timeline, nil
}

func isWhitelisted(id int64) bool {
tweetId := strconv.FormatInt(id, 10)

for _, w := range whitelist {
if w == tweetId {
return true
}
}
return false
}

func deleteFromTimeline(api *anaconda.TwitterApi, ageLimit time.Duration) {
timeline, err := getTimeline(api)

Expand All @@ -49,7 +73,7 @@ func deleteFromTimeline(api *anaconda.TwitterApi, ageLimit time.Duration) {
if err != nil {
log.Print("could not parse time ", err)
} else {
if time.Since(createdTime) > ageLimit {
if time.Since(createdTime) > ageLimit && !isWhitelisted(t.Id) {
_, err := api.DeleteTweet(t.Id, true)
log.Print("DELETED ID ", t.Id)
log.Print("TWEET ", createdTime, " - ", t.Text)
Expand Down

0 comments on commit 862b84d

Please sign in to comment.