-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
402 lines (337 loc) · 9.87 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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"strconv"
"strings"
"time"
)
type AccessTokenResponse struct {
AccessToken string `json:"access_token"`
ExpiresIn int `json:"expires_in"`
Scope string `json:"scope"`
TokenType string `json:"token_type"`
Error string `json:"error,omitempty"`
ErrorDesc string `json:"error_description,omitempty"`
}
type Config struct {
Username string
Password string
ClientID string
ClientSecret string
UserAgent string
SkipCommentIDs []string
SkipSubreddits []string
Before time.Time
MaxScore int
ReplacementComment string
DryRun bool
}
type RawConfig struct {
Username string `json:"username"`
Password string `json:"password"`
ClientID string `json:"ClientID"`
ClientSecret string `json:"ClientSecret"`
UserAgent string `json:"UserAgent"`
SkipCommentIDs []string `json:"SkipCommentIDs"`
SkipSubreddits []string `json:"SkipSubreddits"`
Before string `json:"Before"`
MaxScore int `json:"MaxScore"`
ReplacementComment string `json:"ReplacementComment"`
DryRun bool `json:"DryRun"`
}
type Comment struct {
ID string
Body string
Permalink string
Subreddit string
Source Source
}
type Source struct {
Score int64
CreatedUTC float64
CanGild bool
Date time.Time
}
type ResponseData struct {
Children []Child
After string
Before string
}
type Child struct {
Data Comment
}
type Response struct {
Data ResponseData
}
// EnvVar or Json Value
func getEnvOrDefault(envVar, defaultValue string) string {
if value, exists := os.LookupEnv(envVar); exists {
return value
}
return defaultValue
}
func configLoader(filePath string) (*Config, error) {
file, err := os.Open(filePath)
if err != nil {
return nil, fmt.Errorf("failed to open config file: %v", err)
}
defer file.Close()
var rawConfig RawConfig
if err := json.NewDecoder(file).Decode(&rawConfig); err != nil {
return nil, fmt.Errorf("failed to decode config file: %v", err)
}
before, err := time.Parse(time.RFC3339, rawConfig.Before)
if err != nil {
return nil, fmt.Errorf("failed to parse 'Before' date: %v", err)
}
config := &Config{
Username: rawConfig.Username,
Password: getEnvOrDefault("REDDIT_PASSWORD", rawConfig.Password),
ClientID: rawConfig.ClientID,
ClientSecret: getEnvOrDefault("REDDIT_CLIENT_SECRET", rawConfig.ClientSecret),
UserAgent: rawConfig.UserAgent,
SkipCommentIDs: rawConfig.SkipCommentIDs,
SkipSubreddits: rawConfig.SkipSubreddits,
Before: before,
MaxScore: rawConfig.MaxScore,
ReplacementComment: rawConfig.ReplacementComment,
DryRun: rawConfig.DryRun,
}
return config, nil
}
func (c *Comment) Created() time.Time {
return time.Unix(int64(c.Source.CreatedUTC), 0)
}
func (c *Comment) Fullname() string {
return "t1_" + c.ID
}
func (c *Comment) ShouldSkip(config *Config) bool {
for _, id := range config.SkipCommentIDs {
if id == c.ID {
fmt.Printf("Skipping due to `skip_comment_ids` filter\n")
return true
}
}
for _, subreddit := range config.SkipSubreddits {
fmt.Printf("Subreddit: %v\n", subreddit)
if subreddit == c.Subreddit {
fmt.Printf("Skipping due to `skip_subreddits` filter\n")
return true
}
}
if c.Created().After(config.Before) {
fmt.Printf("Skipping due to `before` filter (%s)\n", config.Before)
return true
}
if c.Source.Score > int64(config.MaxScore) {
fmt.Printf("Skipping due to `max_score` filter (%d)\n", config.MaxScore)
return true
}
return false
}
func LoadConfig(filename string) (*Config, error) {
// Open File
file, err := os.Open(filename)
if err != nil {
fmt.Printf("Failed to open config file: %v", err)
return nil, err
}
defer file.Close()
// Read File
byteValue, err := ioutil.ReadAll(file)
if err != nil {
fmt.Printf("Failed to read config file: %v", err)
return nil, err
}
var config Config
// Unmarshal JSON into config struct
if err := json.Unmarshal(byteValue, &config); err != nil {
return nil, fmt.Errorf("Failed to unmarshal config into struct: %v", err)
}
// Override sensitive fields with environment variables
if password := os.Getenv("REDDIT_PASSWORD"); password != "" {
config.Password = password
} else {
return nil, fmt.Errorf("Reddit password not set")
}
if clientSecret := os.Getenv("REDDIT_CLIENT_SECRET"); clientSecret != "" {
config.ClientSecret = clientSecret
} else {
return nil, fmt.Errorf("Reddit Client Secret not set")
}
// Handle yearsBack from environment variable
yearsBackStr := os.Getenv("REDDIT_YEARS_BACK")
yearsBack, err := strconv.Atoi(yearsBackStr)
if err != nil {
if yearsBackStr != "" {
fmt.Printf("Invalid value for REDDIT_YEARS_BACK: %v", err)
return nil, err
}
yearsBack = 11 // Default to 11 if not set or invalid
}
config.Before = time.Now().AddDate(-yearsBack, 0, 0)
// Handle DryRun
config.DryRun = os.Getenv("REDDIT_DRY_RUN") == "true"
return &config, nil
}
func (c *Comment) Delete(client *http.Client, accessToken string, config *Config) {
if c.ShouldSkip(config) || config.DryRun {
fmt.Println("dryrun set or item set to be skipped, skipping deletion.")
return
}
fmt.Println("Deleting...")
data := url.Values{}
data.Set("id", c.Fullname())
req, err := http.NewRequest("POST", "https://oauth.reddit.com/api/del", strings.NewReader(data.Encode()))
if err != nil {
fmt.Printf("Failed to send delete request: %v\n", err)
return
}
req.Header.Set("Authorization", "Bearer "+accessToken)
req.Header.Set("User-Agent", config.UserAgent)
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
resp, err := client.Do(req)
if err != nil {
fmt.Printf("Failed to delete comment: %v\n", err)
return
}
defer resp.Body.Close()
}
func (c *Comment) Edit(client *http.Client, accessToken string, config *Config) {
if c.ShouldSkip(config) || config.DryRun {
return
}
fmt.Println("Editing...")
data := url.Values{}
data.Set("thing_id", c.Fullname())
data.Set("text", config.ReplacementComment)
req, err := http.NewRequest("POST", "https://oauth.reddit.com/api/editusertext?raw_json=1", strings.NewReader(data.Encode()))
if err != nil {
fmt.Printf("Failed to create request: %v\n", err)
return
}
req.Header.Set("Authorization", "Bearer "+accessToken)
req.Header.Set("User-Agent", config.UserAgent)
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
resp, err := client.Do(req)
if err != nil {
fmt.Printf("Failed to edit comment: %v\n", err)
return
}
defer resp.Body.Close()
var res map[string]interface{}
if err := json.NewDecoder(resp.Body).Decode(&res); err != nil {
fmt.Printf("Failed to decode response: %v\n", err)
return
}
if _, ok := res["jquery"]; ok {
fmt.Printf("Edited successfully.\n")
} else {
fmt.Printf("Failed to edit: %v\n", res)
}
}
func List(client *http.Client, config *Config) <-chan Comment {
out := make(chan Comment)
go func() {
defer close(out)
fmt.Println("Fetching comments...")
var lastSeen string
for {
queryParams := ""
if lastSeen != "" {
queryParams = "?after=" + lastSeen
}
uri := fmt.Sprintf("https://reddit.com/user/%s/comments.json%s", config.Username, queryParams)
req, err := http.NewRequest("GET", uri, nil)
if err != nil {
fmt.Printf("Failed to create request: %v", err)
return
}
req.Header.Set("User-Agent", config.UserAgent)
resp, err := client.Do(req)
if err != nil {
fmt.Printf("Failed to fetch comments: %v", err)
return
}
defer resp.Body.Close()
var res Response
if err := json.NewDecoder(resp.Body).Decode(&res); err != nil {
fmt.Printf("Failed to decode response: %v", err)
return
}
for _, child := range res.Data.Children {
out <- child.Data
}
if len(res.Data.Children) == 0 || res.Data.After == "" {
break
}
lastSeen = res.Data.After
}
}()
return out
}
func newAccessToken(config *Config) (string, error) {
// Prepare form data
form := url.Values{}
form.Add("grant_type", "password")
form.Add("username", config.Username)
form.Add("password", config.Password)
// Prepare request
req, err := http.NewRequest("POST", "https://www.reddit.com/api/v1/access_token", strings.NewReader(form.Encode()))
if err != nil {
return "", err
}
req.SetBasicAuth(config.ClientID, config.ClientSecret)
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("User-Agent", config.UserAgent)
// Send request
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
// Read the response body
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", fmt.Errorf("failed to read response body: %v", err)
}
// Try to decode response as JSON
var res AccessTokenResponse
if err := json.Unmarshal(body, &res); err != nil {
// Log the response body for further inspection
return "", fmt.Errorf("unable to decode response: %v\nResponse body: %s", err, body)
}
// Check for errors in the response
if res.Error != "" {
return "", fmt.Errorf("error in the response: %s", res.ErrorDesc)
}
return res.AccessToken, nil
}
func main() {
// Load config
config, err := configLoader("config.json")
if err != nil {
fmt.Errorf("Error loading config: %v\n", err)
}
// Get access token
accessToken, err := newAccessToken(config)
if err != nil {
fmt.Errorf("Failed to obtain access token: %v\n", err)
}
client := &http.Client{}
// List and process comments
for comment := range List(client, config) {
// Two-step approach to cleaning: edit first, then delete
comment.Edit(client, accessToken, config)
comment.Delete(client, accessToken, config)
// Sleep to avoid throttling
//fmt.Print("Sleeping 15 seconds\n")
//time.Sleep(15 * time.Second)
}
}