-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
196 lines (178 loc) · 4.65 KB
/
main.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package main
import (
"bufio"
"encoding/json"
"errors"
"fmt"
"log"
"os"
"os/exec"
"strings"
"github.com/symmetric-project/symphony/db"
"github.com/symmetric-project/symphony/model"
"github.com/symmetric-project/symphony/utils"
)
type SubmissionRow struct {
Author string `json:"author"`
CreatedUTC int `json:"created_utc"`
Score int `json:"score"`
Subreddit string `json:"subreddit"`
Selftext string `json:"selftext"`
Title string `json:"title"`
Thumbnail string `json:"thumbnail"`
UpvoteRatio float32 `json:"upvote_ratio"`
URL string `json:"url"`
}
func ConvertMarkdownToDraftState(mardown string) (string, error) {
parts := strings.Fields(`node convert-markdown-to-draft-state.js ` + mardown)
cmd := exec.Command(parts[0], parts[1:]...)
output, err := cmd.CombinedOutput()
/* log.Println(string(output)) */
if err != nil {
return "", err
}
return string(output), nil
}
/* func ConvertDraftStateToRawState(draftState string) (string, error) {
parts := strings.Fields(`node convert-draft-state-to-raw-state.js ` + draftState)
cmd := exec.Command(parts[0], parts[1:]...)
output, err := cmd.CombinedOutput()
log.Println(string(output))
if err != nil {
return "", err
}
return string(output), err
} */
/* func ConvertMarkdownToRawState(markdown string) (*string, error) {
draftState, err := ConvertMarkdownToDraftState(markdown)
if err != nil {
utils.StacktraceErrorAndExit(err)
}
rawState, err := ConvertDraftStateToRawState(draftState)
return &rawState, err
} */
func NewPost(subm SubmissionRow, user model.User) (model.Post, error) {
rawState, err := ConvertMarkdownToDraftState(subm.Selftext)
if err != nil {
utils.StacktraceErrorAndExit(err)
}
id := utils.NewOctid()
creationTimestamp := utils.CurrentTimestamp()
slug := utils.Slugify(subm.Title)
post := model.Post{
ID: id,
Title: subm.Title,
RawState: nil,
NodeName: subm.Subreddit,
Slug: slug,
CreationTimestamp: creationTimestamp,
AuthorID: user.ID,
ThumbnaillURL: nil,
ImageURL: nil,
Bases: subm.Score,
}
if len(rawState) > 0 {
post.RawState = &rawState
}
if strings.Contains(subm.Thumbnail, ".jpg") {
healthy := utils.GetUrlHealthy(subm.Thumbnail)
if !healthy {
return post, errors.New("thumbnail url is not healthy")
}
post.ThumbnaillURL = &subm.Thumbnail
}
if strings.Contains(subm.URL, ".jpg") {
healthy := utils.GetUrlHealthy(subm.URL)
if !healthy {
return post, errors.New("image url is not healthy")
}
post.ImageURL = &subm.URL
}
return post, nil
}
func NewNode(subm SubmissionRow) model.Node {
return model.Node{
Name: subm.Subreddit,
Access: model.NodeAccessPublic,
CreatorID: "6da8de06-a065-4831-aa82-8015eece9573",
}
}
func ParseSubreddit(subredditName string) {
file, err := os.Open("./dumps/RS_2021-06")
if err != nil {
utils.StacktraceErrorAndExit(err)
}
defer file.Close()
sc := bufio.NewScanner(file)
const bufCapacity = 64 * 4096
buf := make([]byte, bufCapacity)
sc.Buffer(buf, bufCapacity)
lines := 0
for sc.Scan() {
var subm SubmissionRow
err := json.Unmarshal(sc.Bytes(), &subm)
if err != nil {
utils.StacktraceError(err)
continue
}
if subm.Subreddit != subredditName {
continue
}
var user model.User
userExists, err := db.GetUserExists(subm.Author)
if err != nil {
utils.StacktraceErrorAndExit(err)
}
if !userExists {
user, err = db.AddUser(model.User{Name: subm.Author})
if err != nil {
utils.StacktraceErrorAndExit(err)
} else {
utils.LogSuccess("New user created " + fmt.Sprint(user))
}
} else {
user, err = db.GetUserByName(subm.Author)
if err != nil {
utils.StacktraceErrorAndExit(err)
}
}
/* var node model.Node */
nodeExists, err := db.GetNodeExists(subm.Subreddit)
if err != nil {
utils.StacktraceErrorAndExit(err)
}
if !nodeExists {
creationTimestamp := utils.CurrentTimestamp()
node := model.Node{
Name: subm.Subreddit,
Access: model.NodeAccessPublic,
Nsfw: false,
CreationTimestamp: creationTimestamp,
CreatorID: "6da8de06-a065-4831-aa82-8015eece9573",
}
_, err = db.AddNode(node)
if err != nil {
utils.StacktraceErrorAndExit(err)
} else {
utils.LogSuccess("New node created " + fmt.Sprint(node))
}
}
post, err := NewPost(subm, user)
if err != nil {
utils.StacktraceError(err)
continue
}
err = db.AddPost(post)
if err != nil {
utils.StacktraceErrorAndExit(err)
}
lines++
log.Println(lines)
/* if lines > 1000 {
break
} */
}
}
func main() {
ParseSubreddit("NoNewNormal")
}