From 67b1cc2411d9bbadab753d7b026ca375843584bf Mon Sep 17 00:00:00 2001 From: Mitchell Date: Sun, 23 Feb 2020 22:00:11 -0600 Subject: [PATCH] fix: update stats script to take in date --- .gitignore | 1 + src/scripts/.gitignore | 4 ++++ src/scripts/generate-stats.go | 24 ++++++++++++++++++++---- 3 files changed, 25 insertions(+), 4 deletions(-) create mode 100644 src/scripts/.gitignore diff --git a/.gitignore b/.gitignore index 705731e4..dc5e0868 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /src/reddit.db +**/*.un~ diff --git a/src/scripts/.gitignore b/src/scripts/.gitignore new file mode 100644 index 00000000..f97ad323 --- /dev/null +++ b/src/scripts/.gitignore @@ -0,0 +1,4 @@ +counts.md +subreddits.txt +reddit.db + diff --git a/src/scripts/generate-stats.go b/src/scripts/generate-stats.go index d5d43802..4c13fd6b 100644 --- a/src/scripts/generate-stats.go +++ b/src/scripts/generate-stats.go @@ -17,12 +17,22 @@ import ( var conn *bolt.DB func init() { - conn, _ = bolt.Open("../reddit.db", 0600, &bolt.Options{Timeout: 1 * time.Second}) + conn, _ = bolt.Open("./reddit.db", 0600, &bolt.Options{Timeout: 1 * time.Second}) } func main() { - posts, err := getAllPosts() + if len(os.Args) < 2 { + log.Fatal("Please specify year argument e.g. go run prog.go 2020") + } + + year, err := strconv.Atoi(os.Args[1]) + + if err != nil { + log.Fatal(err) + } + + posts, err := getAllPosts(year) if err != nil { log.Fatal(err) @@ -43,7 +53,7 @@ func main() { } // get posts from database file -func getAllPosts() ([]model.RedditPost, error) { +func getAllPosts(year int) ([]model.RedditPost, error) { posts := []model.RedditPost{} err := conn.View(func(tx *bolt.Tx) error { @@ -59,7 +69,13 @@ func getAllPosts() ([]model.RedditPost, error) { if err != nil { return err } - posts = append(posts, post) + + postTime := time.Unix(int64(post.Created), 0) + + if postTime.Year() == year { + posts = append(posts, post) + } + return nil }) })