toutiao/article/article.go

73 lines
1.4 KiB
Go
Raw Permalink Normal View History

2018-12-23 14:31:50 +00:00
package main
import (
"log"
2019-01-04 08:25:27 +00:00
"474420502.top/eson/gjson"
2018-12-23 14:31:50 +00:00
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
2018-12-27 19:29:34 +00:00
if gjson.Valid(resp.Content()) {
P := gjson.Parse(resp.Content())
data := P.Get(`data`)
if data.Exists() {
adData := data.Get(`ordered_info.#.ad_data`)
if adData.Exists() {
for _, result := range adData.Array() {
2018-12-23 14:31:50 +00:00
adDataList = append(adDataList, result.String())
}
}
2018-12-27 19:29:34 +00:00
relatedVideo := data.Get(`related_video_toutiao.#[show_tag = "广告"]#`)
if adData.Exists() {
for _, result := range relatedVideo.Array() {
adDataList = append(adDataList, result.String())
}
}
2018-12-23 14:31:50 +00:00
}
2018-12-27 19:29:34 +00:00
} else {
log.Println("be careful:", resp.Content())
2018-12-23 14:31:50 +00:00
}
if imitater.ADDataSave(te, te.db, adDataList) {
te.GetCrontab().SetStatus(crontab.SExecuteOK, true)
} else {
te.GetCrontab().SetStatus(crontab.SExecuteOK, false)
}
return te
}