add: 更新数据ext参考例子

This commit is contained in:
eson
2020-07-09 11:38:51 +08:00
parent 0a2a134511
commit a027861f5a
6 changed files with 90 additions and 34 deletions

View File

@@ -1 +1 @@
task2
openrec_task2

View File

@@ -1,8 +1,11 @@
package main
import (
"database/sql"
"encoding/json"
"intimate"
"log"
"time"
"github.com/474420502/hunter"
"github.com/tidwall/gjson"
@@ -27,35 +30,64 @@ type OpenrecExtratorRanking struct {
// Execute 执行方法
func (oer *OpenrecExtratorRanking) Execute(cxt *hunter.TaskContext) {
source, err := store.Pop(targetTypeRanking)
if err != nil {
log.Println(err)
return
}
for {
source, err := store.Pop(targetTypeRanking)
if err != nil {
log.Println(err)
return
}
if source == nil {
return
}
if source != nil {
result := gjson.Parse(source.GetSource().String)
if result.IsArray() {
for _, User := range result.Array() {
userid := User.Get("channel.id").String()
ext := make(map[string]interface{})
openrecUser := &OpenrecUser{}
openrecUser.PreGetUrl = hunter.PreGetUrl("https://www.openrec.tv/user/" + userid + "/supporters")
wf := cxt.Session().Get("https://www.openrec.tv/user/" + userid)
resp, err := wf.Execute()
source.SetUpdateTime(time.Now())
cxt.AddParentTask(openrecUser)
if err != nil {
log.Println(err)
source.SetOperator(int32(intimate.OperatorError))
source.SetErrorMsg(sql.NullString{String: err.Error(), Valid: true})
continue
}
ext["user"] = string(resp.Content())
wf = cxt.Session().Get("https://www.openrec.tv/user/" + userid + "/supporters")
resp, err = wf.Execute()
if err != nil {
log.Println(err)
source.SetOperator(int32(intimate.OperatorError))
source.SetErrorMsg(sql.NullString{String: err.Error(), Valid: true})
continue
}
ext["user_supporters"] = string(resp.Content())
extJsonBytes, err := json.Marshal(ext)
if err != nil {
log.Println(err)
source.SetOperator(int32(intimate.OperatorError))
source.SetErrorMsg(sql.NullString{String: err.Error(), Valid: true})
continue
}
source.SetOperator(int32(intimate.OperatorOK))
source.SetExt(string(extJsonBytes))
store.Update(source)
}
} else {
log.Println("array error:", result.Str)
}
}
}
// OpenrecUser 获取用户信息
type OpenrecUser struct {
hunter.PreGetUrl
}
// Execute 执行方法
func (oer *OpenrecUser) Execute(cxt *hunter.TaskContext) {
}