package datacollector import ( "fmt" "hwcollector/server/models" "io/ioutil" "os" "path/filepath" "strconv" "strings" "time" "github.com/tidwall/gjson" "github.com/tidwall/sjson" ) func init() { processes["clanWarGetInfo"] = new(DoClanWarGetInfo) } const ( IdentClanWarGetInfo = "clanWarGetInfo" ) // DoClanWarGetInfo ... type DoClanWarGetInfo struct { data []byte result []byte } // Start ... func (x *DoClanWarGetInfo) Start() { } // SetJSONByte ... func (x *DoClanWarGetInfo) SetJSONByte(b []byte) { x.data = b } // SetJSON ... func (x *DoClanWarGetInfo) SetJSON(s string) { x.data = []byte(s) } // GetUserDetails ... func (x *DoClanWarGetInfo) GetUserDetails() ([]*models.User, error) { var users []*models.User result := gjson.GetBytes(x.data, "response.result.response.enemies.#.user") for _, jsonValue := range result.Array() { user := new(models.User) if err := user.UnmarshalUserJSON([]byte(jsonValue.Raw)); err != nil { return nil, err } users = append(users, user) } return users, nil } // GetObject ... func (x *DoClanWarGetInfo) GetObject() []byte { date := gjson.GetBytes(x.data, `date`) if !date.Exists() { fmt.Printf("GetObject - error: %s\n", "can't find date") return []byte("") } dat := gjson.GetBytes(x.data, `response.result.response`) if !dat.Exists() { fmt.Printf("GetObject - error: %s\n", "can't find results") return []byte("") } warEnd := dat.Get("endTime") ts, err := strconv.Atoi(warEnd.String()) if err != nil { return []byte("") } tsunix := time.Unix(int64(ts), 0) tsunix = tsunix.Add(-12 * time.Hour) dat2, err := sjson.SetRawBytes([]byte(dat.Raw), "warDate", []byte(fmt.Sprintf("%d", tsunix.Unix()))) if err != nil { fmt.Printf("GetObject - error: %s\n", err.Error()) return []byte("") } return dat2 } // GenerateArrayFromDir ... func (x *DoClanWarGetInfo) GenerateArrayFromDir(ident string) { dirPath, _ := os.Getwd() dirPathHWtools := filepath.Join(dirPath, "hwtools", "data", ident) dirPath = filepath.Join(dirPath, "data", ident) // fmt.Printf("%s\n", dirPath) returnData := []byte("[]") err := filepath.Walk(dirPath, func(path string, info os.FileInfo, err error) error { if err != nil { return err } if info.IsDir() { return nil } //fmt.Printf("%s\n", path) if strings.HasSuffix(info.Name(), ".json") { x.data, err = readFile(path) if err != nil { fmt.Println(err) } returnData, err = sjson.SetRawBytes(returnData, "-1", x.GetObject()) if err != nil { fmt.Println(err) } } return nil }) if err != nil { fmt.Println(err) } mdir := filepath.Base(dirPathHWtools) os.MkdirAll(mdir, os.ModePerm) returnData = runJQUniqe(returnData) err = ioutil.WriteFile(dirPathHWtools+".json", returnData, 0644) if err != nil { fmt.Println(err) } return } // GetJSONResult ... func (x *DoClanWarGetInfo) GetJSONResult() []byte { return x.result }