Skip to content

Commit 26596e8

Browse files
committed
Add most of deletion logic
1 parent 4b692ea commit 26596e8

File tree

1 file changed

+108
-1
lines changed

1 file changed

+108
-1
lines changed

cmd/deleteRedditHistory/main.go

Lines changed: 108 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77
package main
88

99
import (
10+
"context"
1011
"fmt"
1112
"log"
1213
"os"
14+
"time"
1315

1416
"github.com/vartanbeno/go-reddit/v2/reddit"
1517
)
@@ -21,6 +23,10 @@ type envVars struct {
2123
userPassword string
2224
}
2325

26+
const LIMIT = 100
27+
28+
var THRESHOLD = time.Now().AddDate(-2, 0, 0)
29+
2430
func arrayHasNoEmptyStrings(envVars []string) bool {
2531
for _, value := range envVars {
2632
if value == "" {
@@ -32,6 +38,8 @@ func arrayHasNoEmptyStrings(envVars []string) bool {
3238
}
3339

3440
func main() {
41+
ctx := context.Background()
42+
3543
e := envVars{
3644
os.Getenv("REDDIT_APP_ID"),
3745
os.Getenv("REDDIT_SECRET"),
@@ -44,9 +52,108 @@ func main() {
4452
}
4553

4654
credentials := reddit.Credentials{ID: e.appID, Secret: e.appSecret, Username: e.userName, Password: e.userPassword}
47-
_, clientErr := reddit.NewClient(credentials)
55+
cli, clientErr := reddit.NewClient(credentials)
4856

4957
if clientErr != nil {
5058
log.Fatal(clientErr)
5159
}
60+
61+
// Get overview of user
62+
commentService := *cli.Comment
63+
postService := *cli.Post
64+
userService := *cli.User
65+
66+
// Get all posts
67+
fmt.Println("------- Pulling user posts. -------")
68+
69+
lastPostID := ""
70+
postIds := make([]string, 0)
71+
postOptions := &reddit.ListUserOverviewOptions{
72+
ListOptions: reddit.ListOptions{
73+
Limit: LIMIT,
74+
},
75+
Sort: "new",
76+
Time: "all",
77+
}
78+
for {
79+
if lastPostID != "" {
80+
postOptions.ListOptions.After = lastPostID
81+
}
82+
83+
fmt.Println("Pulling more posts.")
84+
posts, _, err := userService.Posts(ctx, postOptions)
85+
if err != nil {
86+
log.Fatal(err)
87+
}
88+
89+
if len(posts) == 0 {
90+
break
91+
}
92+
93+
for _, post := range posts {
94+
if post.Created.Time.Before(THRESHOLD) ||
95+
post.Created.Time.Equal(THRESHOLD) {
96+
fmt.Println(post.Created)
97+
postIds = append(postIds, post.FullID)
98+
}
99+
}
100+
101+
lastPostID = posts[len(posts)-1].FullID
102+
}
103+
104+
// Delete all posts
105+
fmt.Println("------- Deleting user posts. -------")
106+
for _, pID := range postIds {
107+
fmt.Println(pID)
108+
//_, err := postService.Delete(ctx, pID)
109+
//if err != nil {
110+
// log.Fatal(err)
111+
//}
112+
}
113+
114+
// Get all comments
115+
fmt.Println("------- Pulling user comments. -------")
116+
lastCommentID := ""
117+
commentIds := make([]string, 0)
118+
commentOptions := &reddit.ListUserOverviewOptions{
119+
ListOptions: reddit.ListOptions{
120+
Limit: LIMIT,
121+
},
122+
Sort: "new",
123+
Time: "all",
124+
}
125+
for {
126+
if lastCommentID != "" {
127+
commentOptions.ListOptions.After = lastCommentID
128+
}
129+
130+
fmt.Println("Pulling more comments.")
131+
comments, _, err := userService.Comments(ctx, commentOptions)
132+
if err != nil {
133+
log.Fatal(err)
134+
}
135+
136+
if len(comments) == 0 {
137+
break
138+
}
139+
140+
for _, c := range comments {
141+
if c.Created.Time.Before(THRESHOLD) ||
142+
c.Created.Time.Equal(THRESHOLD) {
143+
commentIds = append(commentIds, c.FullID)
144+
}
145+
}
146+
147+
lastCommentID = comments[len(comments)-1].FullID
148+
}
149+
150+
// Delete all comments
151+
fmt.Println("------- Deleting user comments. -------")
152+
for _, cID := range commentIds {
153+
fmt.Println(cID)
154+
//_, err := commentService.Delete(ctx, cID)
155+
//if err != nil {
156+
// log.Fatal(err)
157+
//}
158+
}
52159
}

0 commit comments

Comments
 (0)