intimate/tasks/openrec/openrec_task2/task_openrec.go

100 lines
2.2 KiB
Go
Raw Normal View History

package main
import (
2020-07-09 03:38:51 +00:00
"database/sql"
"encoding/json"
"intimate"
"log"
2020-07-09 03:38:51 +00:00
"time"
"github.com/474420502/hunter"
)
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) {
2020-07-09 03:38:51 +00:00
for {
2020-07-09 09:09:46 +00:00
source, err := store.Pop(targetTypeUser)
2020-07-09 03:38:51 +00:00
if err != nil {
log.Println(err)
return
}
if source == nil {
return
}
2020-07-09 09:09:46 +00:00
userSource := &intimate.Source{}
userid := source.GetSource().String
userUrl := "https://www.openrec.tv/user/" + userid
userSource.SetUrl(userUrl)
wf := cxt.Session().Get(userUrl)
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 := make(map[string]interface{})
ext["user"] = string(resp.Content())
wf = cxt.Session().Get(userUrl + "/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
}
2020-07-09 09:09:46 +00:00
ext["user_supporters"] = string(resp.Content())
wf = cxt.Session().Get("https://www.openrec.tv/live/" + userid)
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_live"] = 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)
2020-07-09 03:38:51 +00:00
}
2020-07-09 09:09:46 +00:00
}