Skip to content

Instantly share code, notes, and snippets.

@askovpen

askovpen/main.go Secret

Created October 21, 2018 19:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save askovpen/2910d26295c6ee8ddbfe6ab31b83e24b to your computer and use it in GitHub Desktop.
Save askovpen/2910d26295c6ee8ddbfe6ab31b83e24b to your computer and use it in GitHub Desktop.
package main
import (
"github.com/askovpen/goated/pkg/config"
"github.com/askovpen/goated/pkg/areasconfig"
"github.com/askovpen/goated/pkg/msgapi"
"log"
"gopkg.in/cheggaaa/pb.v2"
"errors"
"regexp"
"strings"
"encoding/json"
"io/ioutil"
)
type Node struct {
Id string `json:"id"`
Name string `json:"label"` // `json:"name"`
}
type Edge struct {
Source string `json:"from"` // `json:"source"`
Target string `json:"to"` // `json:"target"`
// Weight int `json:"weight"`
}
type JS struct {
Nodes []Node `json:"nodes"`
Edges []Edge `json:"edges"`
}
func (j *JS) ExistsEdge(nn string, pnn string) bool {
for _,e:=range j.Edges {
if e.Source==nn && e.Target==pnn {
return true
}
}
return false
}
func (j *JS) ExistsNode(nn string) bool {
for _,n:=range j.Nodes {
if n.Id==nn {
return true
}
}
return false
}
func main() {
wrongYear:=0
msgs:=0
res:=JS{}
err := config.Read()
if err != nil {
log.Fatal(err)
}
err = areasconfig.Read()
if err != nil {
log.Fatal(err)
}
for _, a:=range msgapi.Areas {
if a.GetCount()==0 {
continue
}
if a.GetType()!=msgapi.EchoAreaTypeEcho {
continue
}
log.Printf("Processing %s", a.GetName())
bar := pb.StartNew(int(a.GetCount()))
for i:=uint32(1);i<=a.GetCount();i++ {
bar.Increment()
msgs++
m,err:=a.GetMsg(i)
if err!=nil {
continue
}
if m.DateArrived.Year()!=2018 {
wrongYear++
continue
}
if m.FromAddr.GetPoint()!=0 {
//continue
}
p, err:=getPath(m.Body)
if err!=nil {
continue
}
for i:=len(p)-1;i>=0;i-- {
if res.ExistsNode(p[i])==false {
res.Nodes=append(res.Nodes,Node{Id:p[i],Name:p[i]})
}
if i>0 {
if res.ExistsEdge(p[i],p[i-1])==false {
// res.Edges=append(res.Edges,Edge{Source:p[i],Target:p[i-1],Weight: 1})
res.Edges=append(res.Edges,Edge{Source:p[i],Target:p[i-1]})
}
}
}
log.Printf("%q",p)
}
bar.Finish()
// break
}
log.Printf("%v",res)
j, _ := json.Marshal(res)
ioutil.WriteFile("output.json", j, 0644)
}
func getPath(b string) ([]string, error) {
res := regexp.MustCompile("\x01PATH: (.*?)[\x0d\x0a]").FindStringSubmatch(b)
if len(res)>0 {
//log.Printf("%s", res[1])
//return res[1], nil
arr:=strings.Split(res[1]," ")
if len(arr)==1 {
return nil, errors.New("err")
}
for i:=0;i<len(arr);i++ {
nf:=strings.Split(arr[i],"/")
if len(nf)==1 {
pnf:=strings.Split(arr[i-1],"/")
arr[i]=pnf[0]+"/"+arr[i]
}
}
return arr,nil
}
return nil, errors.New("err")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment