2019-01-04 08:25:27 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
|
2019-01-04 09:44:23 +00:00
|
|
|
"github.com/Pallinder/go-randomdata"
|
|
|
|
|
2019-01-04 08:25:27 +00:00
|
|
|
"474420502.top/eson/gjson"
|
|
|
|
|
|
|
|
crontab "474420502.top/eson/crontabex"
|
|
|
|
"474420502.top/eson/imitater"
|
|
|
|
"474420502.top/test/logdb"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
imitater.Register("toutiao", &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 {
|
|
|
|
|
2019-01-04 09:44:23 +00:00
|
|
|
wf := te.Workflow(true)
|
2019-01-04 09:55:32 +00:00
|
|
|
kw := randomdata.StringSample("手游", "游戏推荐", "赚钱游戏", "武侠游戏", "战略游戏", "网游", "游戏", "单机游戏")
|
2019-01-04 09:50:40 +00:00
|
|
|
wf.GetQuery().Set("keyword", kw)
|
|
|
|
log.Println(kw)
|
2019-01-04 09:44:23 +00:00
|
|
|
|
|
|
|
resp, err := wf.Execute()
|
2019-01-04 08:25:27 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
return te
|
|
|
|
}
|
|
|
|
|
|
|
|
var adDataList []string
|
|
|
|
|
|
|
|
if gjson.Valid(resp.Content()) {
|
|
|
|
P := gjson.Parse(resp.Content())
|
|
|
|
data := P.Get(`data`)
|
|
|
|
if data.Exists() {
|
|
|
|
adData := data.Get(`#[label = "广告"]#`)
|
|
|
|
if adData.Exists() {
|
|
|
|
for _, result := range adData.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
|
|
|
|
}
|