-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemp.go
107 lines (93 loc) · 2.44 KB
/
temp.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package main
import (
"encoding/json"
"fmt"
"io"
"os"
"sort"
"time"
)
type Post struct {
ID string `json:"id"`
Name string `json:"name"`
CreatedUTC time.Time `json:"created_utc"`
Permalink string `json:"permalink"`
Title string `json:"title"`
Category string `json:"category"`
Selftext string `json:"selftext"`
Score int `json:"score"`
UpvoteRatio float64 `json:"upvote_ratio"`
NumComments int `json:"num_comments"`
Subreddit string `json:"subreddit"`
SubredditID string `json:"subreddit_id"`
SubredditSubscribers int `json:"subreddit_subscribers"`
Author string `json:"author"`
AuthorFullname string `json:"author_fullname"`
}
type PostsWrapper struct {
Posts []Post `json:"posts"`
}
func main() {
jsonFile, err := os.Open("topMollywood.json")
if err != nil {
fmt.Println(err)
return
}
defer jsonFile.Close()
byteValue, err := io.ReadAll(jsonFile)
if err != nil {
fmt.Println(err)
return
}
var postsWrapper PostsWrapper
err = json.Unmarshal(byteValue, &postsWrapper)
if err != nil {
fmt.Println(err)
return
}
AddCategory(&postsWrapper, "top")
SortPosts(postsWrapper)
count := 1
date := postsWrapper.Posts[0].CreatedUTC
for _, post := range postsWrapper.Posts {
createdDate := post.CreatedUTC.Day()
dateDate := date.Day()
if createdDate != dateDate {
fmt.Printf("%s. %d\n", post.CreatedUTC, count)
date = post.CreatedUTC
count = 1
}
count++
}
}
func AddCategory(postsWrapper *PostsWrapper, category string) {
for i := range postsWrapper.Posts {
postsWrapper.Posts[i].Category = category
}
}
func SortPosts(postsWrapper PostsWrapper) {
sort.Slice(postsWrapper.Posts, func(i, j int) bool {
dateI := postsWrapper.Posts[i].CreatedUTC.Truncate(24 * time.Hour)
dateJ := postsWrapper.Posts[j].CreatedUTC.Truncate(24 * time.Hour)
if dateI.Equal(dateJ) {
return postsWrapper.Posts[i].Score > postsWrapper.Posts[j].Score
}
return dateI.After(dateJ)
})
sortedFile, err := os.Create("sortedTopMollywood.json")
if err != nil {
fmt.Println(err)
return
}
defer sortedFile.Close()
sortedBytes, err := json.MarshalIndent(postsWrapper, "", " ")
if err != nil {
fmt.Println(err)
return
}
_, err = sortedFile.Write(sortedBytes)
if err != nil {
fmt.Println(err)
return
}
}