Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/audit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ on:
jobs:

audit:
runs-on: ubuntu-20.04
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2

- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.22
go-version: 1.23

- name: Verify dependencies
run: go mod verify
Expand Down
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ lint:
go vet ./...
staticcheck ./...

.PHONY: delete-reddit-history
delete-reddit-history:
go run ./cmd/deleteRedditHistory/main.go

.PHONY: parse-slack-data
parse-slack-data:
go run ./cmd/slackMessageParser/main.go
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,17 @@ Various scripts written in Golang.

## Scripts

### Reddit Comment and Post Deleter
This script deletes all comments for a given user that are older than 2 years old. To properly configure, please
create the following environment variables: `REDDIT_USER_ID`, `REDDIT_USER_PASSWORD`, `REDDIT_APP_ID`, `REDDIT_SECRET`.

For information regarding the acquisition of values for the above environment variables, please
check here: [Link](https://github.com/reddit-archive/reddit/wiki/OAuth2-Quick-Start-Example#first-steps).

### Slack Message Parser
This tool is used to parse the `json`-formatted data that comes from [exporting Slack workspace data](https://slack.com/help/articles/201658943-Export-your-workspace-data) into a `csv` file named: `slack_records.csv`.
This script is used to parse the `json`-formatted data that comes from
[exporting Slack workspace data](https://slack.com/help/articles/201658943-Export-your-workspace-data) into a `csv`
file named: `slack_records.csv`.

| TimeStamp | UserID | UserName | RealName | MessageType | Text | Attachments | Files |
|:---------:|:------:|:--------:|:--------:|:-----------:|:------:|:-----------:|:--------:|
Expand Down
155 changes: 155 additions & 0 deletions cmd/deleteRedditHistory/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
/*
* Copyright (c) 2025 Michael Plunkett (https://github.com/michplunkett)
* All rights reserved.
* Used to delete Reddit user posts and comments older than 2-years-old.
*/

package main

import (
"context"
"fmt"
"log"
"os"
"time"

"github.com/vartanbeno/go-reddit/v2/reddit"
)

type envVars struct {
appID string
appSecret string
userName string
userPassword string
}

const LIMIT = 100

var THRESHOLD = time.Now().AddDate(-2, 0, 0)

func arrayHasNoEmptyStrings(envVars []string) bool {
for _, value := range envVars {
if value == "" {
return false
}
}

return true
}

func main() {
ctx := context.Background()

e := envVars{
os.Getenv("REDDIT_APP_ID"),
os.Getenv("REDDIT_SECRET"),
os.Getenv("REDDIT_USER_ID"),
os.Getenv("REDDIT_USER_PASSWORD"),
}

if !arrayHasNoEmptyStrings([]string{e.appID, e.appSecret, e.userName, e.userPassword}) {
log.Fatal(fmt.Errorf("one of the last.fm environment variables is not present in your system"))
}

credentials := reddit.Credentials{ID: e.appID, Secret: e.appSecret, Username: e.userName, Password: e.userPassword}
cli, clientErr := reddit.NewClient(credentials)
if clientErr != nil {
log.Fatal(clientErr)
}

// Get overview of user
commentService := *cli.Comment
postService := *cli.Post
userService := *cli.User

// Get all posts
fmt.Println("------- Pulling user posts")

lastPostID := ""
postIds := make([]string, 0)
postOptions := &reddit.ListUserOverviewOptions{
ListOptions: reddit.ListOptions{
Limit: LIMIT,
},
Sort: "new",
Time: "all",
}
for {
if lastPostID != "" {
postOptions.ListOptions.After = lastPostID
}

posts, _, err := userService.Posts(ctx, postOptions)
if err != nil {
log.Fatal(err)
}

if len(posts) == 0 {
break
}

for _, post := range posts {
if post.Created.Time.Before(THRESHOLD) ||
post.Created.Time.Equal(THRESHOLD) {
postIds = append(postIds, post.FullID)
}
}

lastPostID = posts[len(posts)-1].FullID
}

// Delete all posts
fmt.Println("------- Deleting user posts:", len(postIds))
for _, pID := range postIds {
_, err := postService.Delete(ctx, pID)
if err != nil {
log.Fatal(err)
}
}

// Get all comments
fmt.Println("------- Pulling user comments")

lastCommentID := ""
commentIds := make([]string, 0)
commentOptions := &reddit.ListUserOverviewOptions{
ListOptions: reddit.ListOptions{
Limit: LIMIT,
},
Sort: "new",
Time: "all",
}
for {
if lastCommentID != "" {
commentOptions.ListOptions.After = lastCommentID
}

comments, _, err := userService.Comments(ctx, commentOptions)
if err != nil {
log.Fatal(err)
}

if len(comments) == 0 {
break
}

for _, c := range comments {
if c.Created.Time.Before(THRESHOLD) ||
c.Created.Time.Equal(THRESHOLD) {
commentIds = append(commentIds, c.FullID)
}
}

lastCommentID = comments[len(comments)-1].FullID
}

// Delete all comments
fmt.Println("------- Deleting user comments:", len(commentIds))

for _, cID := range commentIds {
_, err := commentService.Delete(ctx, cID)
if err != nil {
log.Fatal(err)
}
}
}
9 changes: 9 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,12 @@ module github.com/michplunkett/golang-scripting
go 1.22.4

require github.com/gocarina/gocsv v0.0.0-20240520201108-78e41c74b4b1

require (
github.com/golang/protobuf v1.2.0 // indirect
github.com/google/go-querystring v1.0.0 // indirect
github.com/vartanbeno/go-reddit/v2 v2.0.1
golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e // indirect
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d // indirect
google.golang.org/appengine v1.4.0 // indirect
)
27 changes: 27 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,29 @@
cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gocarina/gocsv v0.0.0-20240520201108-78e41c74b4b1 h1:FWNFq4fM1wPfcK40yHE5UO3RUdSNPaBC+j3PokzA6OQ=
github.com/gocarina/gocsv v0.0.0-20240520201108-78e41c74b4b1/go.mod h1:5YoVOkjYAQumqlV356Hj3xeYh4BdZuLE0/nRkf2NKkI=
github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/google/go-querystring v1.0.0 h1:Xkwi/a1rcvNg1PPYe5vI8GbeBY/jrVuDX5ASuANWTrk=
github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/vartanbeno/go-reddit/v2 v2.0.1 h1:P6ITpf5YHjdy7DHZIbUIDn/iNAoGcEoDQnMa+L4vutw=
github.com/vartanbeno/go-reddit/v2 v2.0.1/go.mod h1:758/S10hwZSLm43NPtwoNQdZFSg3sjB5745Mwjb0ANI=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e h1:bRhVy7zSSasaqNksaRZiA5EEI+Ei4I1nO5Jh72wfHlg=
golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d h1:TzXSXBo42m9gQenoE3b9BGiEpg5IG2JkU5FkPIawgtw=
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4 h1:YUO/7uOKsKeq9UokNS62b8FYywz3ker1l1vDZRCRefw=
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
google.golang.org/appengine v1.4.0 h1:/wp5JvzpHIxhs/dumFmF7BXTf3Z+dd4uXta4kVyO508=
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=