Skip to content
This repository has been archived by the owner on Aug 7, 2023. It is now read-only.

Commit

Permalink
Add new category and other minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
dl-tg committed Mar 22, 2023
1 parent 4e731c4 commit 91d61e3
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"time"
)

var categoryID = [4]string{"top", "controversial", "hot", "rising"}
var categoryID = [5]string{"new", "top", "controversial", "hot", "rising"}

// I don't want to set f*ckton of headers for every request...
// http.NewRequest wrapper with additional headers
Expand Down Expand Up @@ -99,6 +99,7 @@ func subredditValid(subreddit string) bool {
If it doesn't exist, it won't have the Subscribers field, thus it will return false */

return data.Data.Subscribers >= 0 && data.Data.SubredditType != "private"

}

func fetchPosts(subreddit string, id, limit int, export_comments bool) {
Expand All @@ -110,7 +111,7 @@ func fetchPosts(subreddit string, id, limit int, export_comments bool) {
checkError(err)

/* Sends an HTTP request and returns an HTTP response, following policy
(such as redirects, cookies, auth) as configured on the client. */
(such as redirects, cookies, auth) as configured on the client. */

res, err := client.Do(req)
checkError(err)
Expand Down Expand Up @@ -138,13 +139,11 @@ func fetchPosts(subreddit string, id, limit int, export_comments bool) {
log.Fatal(err)
}

// Create an empty slice of maps to hold each post's data
posts := []map[string]interface{}{}
// Extract data field for the posts
posts := make([]map[string]interface{}, len(data.Data.Children))

// Iterate over each child in the data struct's Children slice, extract its data field and append it to the posts slice
for _, child := range data.Data.Children {
post := child.Data
posts = append(posts, post)
for i := range data.Data.Children {
posts[i] = data.Data.Children[i].Data
}

exportPosts(posts, subreddit, id, export_comments)
Expand Down Expand Up @@ -240,7 +239,7 @@ func main() {

flag.StringVar(&subreddit, "subreddit", "programming", "Subreddit to fetch posts from")
flag.IntVar(&limit, "limit", 5, "Amount of posts to fetch")
flag.IntVar(&id, "categoryID", 0, "Category of posts to fetch\n0 - top\n1 - controversial\n2 - hot\n3 - rising")
flag.IntVar(&id, "categoryID", 0, "Category of posts to fetch\n0 - new\n1 - top\n2 - controversial\n3 - hot\nr4 - rising")
flag.BoolVar(&export_comments, "exportComments", true, "Toggle comment exporting")

flag.Parse()
Expand Down

0 comments on commit 91d61e3

Please sign in to comment.