-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmlcuration.go
175 lines (161 loc) · 3.38 KB
/
mlcuration.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
package main
import (
"bufio"
"encoding/json"
"io"
"math/rand"
"os"
"os/exec"
"path"
)
type CCommand struct {
Id int `json:"id"`
Command string `json:"command"`
Arguments map[string]interface{} `json:"arguments"`
}
type Result struct {
Id int `json:"id"`
Result interface{} `json:"result"`
Error string `json:"error"`
}
type MLCurator struct {
cmd *exec.Cmd
in io.WriteCloser
scanner *bufio.Scanner
}
func (c *MLCurator) Init() error {
ex, err := os.Executable()
if err != nil {
return err
}
exPath := path.Dir(ex)
c.cmd = exec.Command(exPath + "/dist/main")
c.in, err = c.cmd.StdinPipe()
if err != nil {
return err
}
out, err := c.cmd.StdoutPipe()
if err != nil {
return err
}
c.scanner = bufio.NewScanner(out)
err = c.cmd.Start()
if err != nil {
Error.Println(err)
}
return err
}
func (c *MLCurator) OnPostAdded(obj *Post, isWhitelabeled bool) bool {
res, _ := c.sendCommand(CCommand{
rand.Int(), "on_post_added", map[string]interface{}{
"obj": obj,
"isWhitelabeled": isWhitelabeled,
},
})
if res.Error != "" {
Error.Println(res.Error)
return false
}
r, ok := res.Result.(bool)
if !ok {
Error.Println("Result is not a bool")
return false
}
return r
}
func (c *MLCurator) OnCommentAdded(obj *Comment, isWhitelabeled bool) bool {
res, _ := c.sendCommand(CCommand{
rand.Int(), "on_comment_added", map[string]interface{}{
"obj": obj,
"isWhitelabeled": isWhitelabeled,
},
})
if res.Error != "" {
Error.Println(res.Error)
return false
}
r, ok := res.Result.(bool)
if !ok {
Error.Println("Result is not a bool")
return false
}
return r
}
func (c *MLCurator) GetContent(params map[string]interface{}) []string {
res, _ := c.sendCommand(CCommand{
rand.Int(), "get_content", map[string]interface{}{
"params": params,
},
})
if res.Error == "" {
r := res.Result.([]interface{})
if len(r) > 0 {
retval := make([]string, len(r))
for i, v := range r {
retval[i] = v.(string)
}
return retval
}
return nil
}
return nil
}
func (c *MLCurator) FlagContent(hash string, isFlagged bool) {
c.sendCommand(CCommand{
rand.Int(), "flag_content", map[string]interface{}{
"hash": hash,
"isFlagged": isFlagged,
},
})
}
func (c *MLCurator) UpvoteContent(hash string) {
c.sendCommand(CCommand{
rand.Int(), "upvote_content", map[string]interface{}{
"hash": hash,
},
})
}
func (c *MLCurator) DownvoteContent(hash string) {
c.sendCommand(CCommand{
rand.Int(), "downvote_content", map[string]interface{}{
"hash": hash,
},
})
}
func (c *MLCurator) Close() error {
_, err := c.sendCommand(CCommand{
rand.Int(), "quit", nil,
})
return err
}
// send command to pipe and read corresponding response
func (c *MLCurator) sendCommand(command CCommand) (*Result, error) {
obj, err := json.Marshal(command)
var res Result
if err != nil {
res = Result{
command.Id, nil, "Marshal: " + err.Error(),
}
return &res, err
}
_, err = io.WriteString(c.in, string(obj)+"\n")
if err != nil {
res = Result{
command.Id, nil, "I/O: " + err.Error(),
}
return &res, err
}
for c.scanner.Scan() {
if len(c.scanner.Text()) > 0 {
break
}
}
err = json.Unmarshal([]byte(c.scanner.Text()), &res)
if err != nil {
res = Result{
command.Id, nil, "I/O: " + err.Error(),
}
return &res, err
}
return &res, nil
}