intimate/tasks/openrec/openrec_task2/task_openrec.go
2020-07-10 12:05:33 +08:00

98 lines
2.2 KiB
Go

package main
import (
"database/sql"
"encoding/json"
"intimate"
"log"
"time"
"github.com/474420502/hunter"
)
var oer *OpenrecExtratorRanking
// store 源存储实例, 为存储源数据的实现. 表格具体参考sql/intimate_source.sql
var store *intimate.SourceStore = intimate.NewSourceStore(string(intimate.STOpenrec))
func init() {
oer = &OpenrecExtratorRanking{}
}
// OpenrecExtratorRanking 获取用户信息
type OpenrecExtratorRanking struct {
// Store *intimate.Store
}
// Execute 执行方法
func (oer *OpenrecExtratorRanking) Execute(cxt *hunter.TaskContext) {
for {
source, err := store.Pop(string(intimate.TTOpenrecUser))
if err != nil {
log.Println(err)
return
}
if source == nil {
return
}
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
}
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)
}
}