package main import ( "log" "regexp" "github.com/tidwall/gjson" crontab "474420502.top/eson/crontabex" "474420502.top/eson/imitater" "474420502.top/test/logdb" ) func main() { imitater.Register("article", &TaskEx{}) person := imitater.NewPerson() person.Config("task.yaml") person.Execute() } // TaskEx 任务相关类 type TaskEx struct { imitater.Task db *logdb.LogDB } // Init 初始化函数 func (te *TaskEx) Init() { te.db = logdb.New("../logdb.yaml") } // Execute 执行过程的方法 func (te *TaskEx) Execute(glocal map[string]interface{}) imitater.ITask { resp, err := te.Request() if err != nil { log.Println(err) return te } var adDataList []string rule := regexp.MustCompile(`\\"label\\"\:\\"广告\\"|\\"label_style\\"\:3`) if rule.MatchString(resp.Content()) { if gjson.Valid(resp.Content()) { P := gjson.Parse(resp.Content()) gADDate := P.Get(`data.ordered_info.#.ad_data`) if gADDate.Exists() { for _, result := range gADDate.Array() { adDataList = append(adDataList, result.String()) } } } else { log.Println("be careful:", resp.Content()) } } if imitater.ADDataSave(te, te.db, adDataList) { te.GetCrontab().SetStatus(crontab.SExecuteOK, true) } else { te.GetCrontab().SetStatus(crontab.SExecuteOK, false) } return te }