|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Michael Plunkett (https://github.com/michplunkett) |
| 3 | + * All rights reserved. |
| 4 | + * Used to delete Reddit user posts and comments older than 2-years-old. |
| 5 | + */ |
| 6 | + |
| 7 | +package main |
| 8 | + |
| 9 | +import ( |
| 10 | + "context" |
| 11 | + "fmt" |
| 12 | + "log" |
| 13 | + "os" |
| 14 | + "time" |
| 15 | + |
| 16 | + "github.com/vartanbeno/go-reddit/v2/reddit" |
| 17 | +) |
| 18 | + |
| 19 | +type envVars struct { |
| 20 | + appID string |
| 21 | + appSecret string |
| 22 | + userName string |
| 23 | + userPassword string |
| 24 | +} |
| 25 | + |
| 26 | +const LIMIT = 100 |
| 27 | + |
| 28 | +var THRESHOLD = time.Now().AddDate(-2, 0, 0) |
| 29 | + |
| 30 | +func arrayHasNoEmptyStrings(envVars []string) bool { |
| 31 | + for _, value := range envVars { |
| 32 | + if value == "" { |
| 33 | + return false |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + return true |
| 38 | +} |
| 39 | + |
| 40 | +func main() { |
| 41 | + ctx := context.Background() |
| 42 | + |
| 43 | + e := envVars{ |
| 44 | + os.Getenv("REDDIT_APP_ID"), |
| 45 | + os.Getenv("REDDIT_SECRET"), |
| 46 | + os.Getenv("REDDIT_USER_ID"), |
| 47 | + os.Getenv("REDDIT_USER_PASSWORD"), |
| 48 | + } |
| 49 | + |
| 50 | + if !arrayHasNoEmptyStrings([]string{e.appID, e.appSecret, e.userName, e.userPassword}) { |
| 51 | + log.Fatal(fmt.Errorf("one of the last.fm environment variables is not present in your system")) |
| 52 | + } |
| 53 | + |
| 54 | + credentials := reddit.Credentials{ID: e.appID, Secret: e.appSecret, Username: e.userName, Password: e.userPassword} |
| 55 | + cli, clientErr := reddit.NewClient(credentials) |
| 56 | + if clientErr != nil { |
| 57 | + log.Fatal(clientErr) |
| 58 | + } |
| 59 | + |
| 60 | + // Get overview of user |
| 61 | + commentService := *cli.Comment |
| 62 | + postService := *cli.Post |
| 63 | + userService := *cli.User |
| 64 | + |
| 65 | + // Get all posts |
| 66 | + fmt.Println("------- Pulling user posts") |
| 67 | + |
| 68 | + lastPostID := "" |
| 69 | + postIds := make([]string, 0) |
| 70 | + postOptions := &reddit.ListUserOverviewOptions{ |
| 71 | + ListOptions: reddit.ListOptions{ |
| 72 | + Limit: LIMIT, |
| 73 | + }, |
| 74 | + Sort: "new", |
| 75 | + Time: "all", |
| 76 | + } |
| 77 | + for { |
| 78 | + if lastPostID != "" { |
| 79 | + postOptions.ListOptions.After = lastPostID |
| 80 | + } |
| 81 | + |
| 82 | + posts, _, err := userService.Posts(ctx, postOptions) |
| 83 | + if err != nil { |
| 84 | + log.Fatal(err) |
| 85 | + } |
| 86 | + |
| 87 | + if len(posts) == 0 { |
| 88 | + break |
| 89 | + } |
| 90 | + |
| 91 | + for _, post := range posts { |
| 92 | + if post.Created.Time.Before(THRESHOLD) || |
| 93 | + post.Created.Time.Equal(THRESHOLD) { |
| 94 | + postIds = append(postIds, post.FullID) |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + lastPostID = posts[len(posts)-1].FullID |
| 99 | + } |
| 100 | + |
| 101 | + // Delete all posts |
| 102 | + fmt.Println("------- Deleting user posts:", len(postIds)) |
| 103 | + for _, pID := range postIds { |
| 104 | + _, err := postService.Delete(ctx, pID) |
| 105 | + if err != nil { |
| 106 | + log.Fatal(err) |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + // Get all comments |
| 111 | + fmt.Println("------- Pulling user comments") |
| 112 | + |
| 113 | + lastCommentID := "" |
| 114 | + commentIds := make([]string, 0) |
| 115 | + commentOptions := &reddit.ListUserOverviewOptions{ |
| 116 | + ListOptions: reddit.ListOptions{ |
| 117 | + Limit: LIMIT, |
| 118 | + }, |
| 119 | + Sort: "new", |
| 120 | + Time: "all", |
| 121 | + } |
| 122 | + for { |
| 123 | + if lastCommentID != "" { |
| 124 | + commentOptions.ListOptions.After = lastCommentID |
| 125 | + } |
| 126 | + |
| 127 | + comments, _, err := userService.Comments(ctx, commentOptions) |
| 128 | + if err != nil { |
| 129 | + log.Fatal(err) |
| 130 | + } |
| 131 | + |
| 132 | + if len(comments) == 0 { |
| 133 | + break |
| 134 | + } |
| 135 | + |
| 136 | + for _, c := range comments { |
| 137 | + if c.Created.Time.Before(THRESHOLD) || |
| 138 | + c.Created.Time.Equal(THRESHOLD) { |
| 139 | + commentIds = append(commentIds, c.FullID) |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + lastCommentID = comments[len(comments)-1].FullID |
| 144 | + } |
| 145 | + |
| 146 | + // Delete all comments |
| 147 | + fmt.Println("------- Deleting user comments:", len(commentIds)) |
| 148 | + |
| 149 | + for _, cID := range commentIds { |
| 150 | + _, err := commentService.Delete(ctx, cID) |
| 151 | + if err != nil { |
| 152 | + log.Fatal(err) |
| 153 | + } |
| 154 | + } |
| 155 | +} |
0 commit comments