mirror of
https://github.com/mgerb/top-of-reddit
synced 2026-01-10 18:42:50 +00:00
fix: update stats script to take in date
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,2 +1,3 @@
|
|||||||
/src/reddit.db
|
/src/reddit.db
|
||||||
|
**/*.un~
|
||||||
|
|
||||||
|
|||||||
4
src/scripts/.gitignore
vendored
Normal file
4
src/scripts/.gitignore
vendored
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
counts.md
|
||||||
|
subreddits.txt
|
||||||
|
reddit.db
|
||||||
|
|
||||||
@@ -17,12 +17,22 @@ import (
|
|||||||
var conn *bolt.DB
|
var conn *bolt.DB
|
||||||
|
|
||||||
func init() {
|
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() {
|
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 {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
@@ -43,7 +53,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// get posts from database file
|
// get posts from database file
|
||||||
func getAllPosts() ([]model.RedditPost, error) {
|
func getAllPosts(year int) ([]model.RedditPost, error) {
|
||||||
posts := []model.RedditPost{}
|
posts := []model.RedditPost{}
|
||||||
|
|
||||||
err := conn.View(func(tx *bolt.Tx) error {
|
err := conn.View(func(tx *bolt.Tx) error {
|
||||||
@@ -59,7 +69,13 @@ func getAllPosts() ([]model.RedditPost, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
postTime := time.Unix(int64(post.Created), 0)
|
||||||
|
|
||||||
|
if postTime.Year() == year {
|
||||||
posts = append(posts, post)
|
posts = append(posts, post)
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user