This repository has been archived by the owner on Mar 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DAMT.go
223 lines (198 loc) · 5.13 KB
/
DAMT.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
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"strconv"
"strings"
"time"
"github.com/kurrik/oauth1a"
"github.com/kurrik/twittergo"
"github.com/manifoldco/promptui"
)
type Config struct {
APIKey string `json:"APIKey"`
APISecretKey string `json:"APISecretKey"`
AccessToken string `json:"AccessToken"`
AccessTokenSecret string `json:"AccessTokenSecret"`
Spare []string `json:"Spare,omitempty"`
DeleteReplies bool `json:"DeleteReplies"`
}
func LoadConfigFrom(ConfigFile string) (client *twittergo.Client, config *Config, err error) {
credentials, err := ioutil.ReadFile(ConfigFile)
if err != nil {
return
}
if err = json.Unmarshal(credentials, &config); err != nil {
return
}
UserConfig := oauth1a.NewAuthorizedConfig(config.AccessToken, config.AccessTokenSecret)
ClientConfig := &oauth1a.ClientConfig{
ConsumerKey: config.APIKey,
ConsumerSecret: config.APISecretKey,
}
client = twittergo.NewClient(ClientConfig, UserConfig)
return
}
type Args struct {
Days int
ConfigFile string
Help bool
Test bool
}
func parseArgs() *Args {
a := &Args{}
flag.StringVar(&a.ConfigFile, "config", "", "JSON Config File")
flag.IntVar(&a.Days, "days", 0, "Tweets older than this value will be deleted")
flag.BoolVar(&a.Help, "help", false, "Prints this message")
flag.BoolVar(&a.Test, "test", false, "Do not delete anything")
flag.Parse()
return a
}
// VerifyCredentials from the given client and return a User
// TODO Rate limit
func VerifyCredentials(client *twittergo.Client) (user *twittergo.User, err error) {
var (
req *http.Request
resp *twittergo.APIResponse
)
req, err = http.NewRequest("GET", "/1.1/account/verify_credentials.json", nil)
resp, err = client.SendRequest(req)
if err != nil {
return
}
user = &twittergo.User{}
err = resp.Parse(user)
return
}
func main() {
var (
err error
client *twittergo.Client
req *http.Request
resp *twittergo.APIResponse
args *Args
max_id uint64
query url.Values
results *twittergo.Timeline
config *Config
)
args = parseArgs()
if client, config, err = LoadConfigFrom(args.ConfigFile); err != nil {
Usage()
log.Fatalf("Could not parse config file: %v\n", err)
}
if args.Days == 0 {
fmt.Println("This will delete all of your tweets! Are you sure?")
if yesNo() != true {
Usage()
os.Exit(0)
}
}
user, err := VerifyCredentials(client)
if err != nil {
log.Println(err)
}
const (
count int = 200
urltmpl = "/1.1/statuses/user_timeline.json?%v"
minwait = time.Duration(10) * time.Second
)
query = url.Values{}
query.Set("count", fmt.Sprintf("%v", count))
query.Set("screen_name", user.ScreenName())
if config.DeleteReplies {
query.Set("exclude_replies", "false")
}
total := 0
for {
if max_id != 0 {
query.Set("max_id", fmt.Sprintf("%v", max_id))
}
endpoint := fmt.Sprintf(urltmpl, query.Encode())
if req, err = http.NewRequest("GET", endpoint, nil); err != nil {
log.Fatalf("Could not parse request: %v\n", err)
}
if resp, err = client.SendRequest(req); err != nil {
log.Fatalf("Could not send request: %v\n", err)
}
results = &twittergo.Timeline{}
if err = resp.Parse(results); err != nil {
if rle, ok := err.(twittergo.RateLimitError); ok {
dur := rle.Reset.Sub(time.Now()) + time.Second
if dur < minwait {
// Don't wait less than minwait.
dur = minwait
}
msg := "Rate limited. Reset at %v. Waiting for %v\n"
fmt.Printf(msg, rle.Reset, dur)
time.Sleep(dur)
continue // Retry request.
} else {
fmt.Printf("Problem parsing response: %v\n", err)
}
}
batch := len(*results)
if batch == 0 {
break
}
for _, tweet := range *results {
if contains(config.Spare, tweet.IdStr()) {
fmt.Printf("Skipping: %v\n", tweet.IdStr())
max_id = tweet.Id() - 1
total++
continue;
}
var days int = int(time.Since(tweet.CreatedAt()).Hours() / 24)
if days >= args.Days {
fmt.Printf("Tweet: %v\tCreated at: %v\tDays Since Creation:%v\n", tweet.IdStr(), tweet.CreatedAt(), days)
if !args.Test {
endpoint := "/1.1/statuses/destroy/" + strconv.FormatUint(tweet.Id(), 10) + ".json"
data := url.Values{}
data.Set("id", strconv.FormatUint(tweet.Id(), 10))
body := strings.NewReader(data.Encode())
req, err = http.NewRequest("POST", endpoint, body)
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
if err != nil {
log.Fatalln(err)
}
resp, err = client.SendRequest(req)
if err != nil {
log.Fatalf("Could not send request: %v\n", err)
}
fmt.Printf("Deleted: %v\n", tweet.IdStr())
}
}
max_id = tweet.Id() - 1
total++
}
}
}
var Usage = func() {
fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
flag.PrintDefaults()
}
func contains(s []string, e string) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}
func yesNo() bool {
prompt := promptui.Select{
Label: "Select[Yes/No]",
Items: []string{"Yes", "No"},
}
_, result, err := prompt.Run()
if err != nil {
log.Fatalf("Prompt failed %v\n", err)
}
return result == "Yes"
}