intimate/tasks/openrec/openrec_task2/task_openrec.go
2020-07-09 11:38:51 +08:00

94 lines
2.1 KiB
Go

package main
import (
"database/sql"
"encoding/json"
"intimate"
"log"
"time"
"github.com/474420502/hunter"
"github.com/tidwall/gjson"
)
var targetTypeUser = "openrec_user"
var targetTypeRanking = "openrec_ranking"
var oer *OpenrecExtratorRanking
// store 源存储实例, 为存储源数据的实现. 表格具体参考sql/intimate_source.sql
var store *intimate.Store = intimate.NewStore("source_openrec")
func init() {
oer = &OpenrecExtratorRanking{}
}
// OpenrecExtratorRanking 获取用户信息
type OpenrecExtratorRanking struct {
// Store *intimate.Store
}
// Execute 执行方法
func (oer *OpenrecExtratorRanking) Execute(cxt *hunter.TaskContext) {
for {
source, err := store.Pop(targetTypeRanking)
if err != nil {
log.Println(err)
return
}
if source == nil {
return
}
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{})
wf := cxt.Session().Get("https://www.openrec.tv/user/" + userid)
resp, err := wf.Execute()
source.SetUpdateTime(time.Now())
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)
}
}
}