1
0
mirror of https://github.com/mgerb/top-of-reddit synced 2026-01-09 10:02:50 +00:00

fix: update stats script to take in date

This commit is contained in:
2020-02-23 22:00:11 -06:00
parent 5118d89166
commit 67b1cc2411
3 changed files with 25 additions and 4 deletions

1
.gitignore vendored
View File

@@ -1,2 +1,3 @@
/src/reddit.db
**/*.un~

4
src/scripts/.gitignore vendored Normal file
View File

@@ -0,0 +1,4 @@
counts.md
subreddits.txt
reddit.db

View File

@@ -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
})
})