intimate/extractor/openrec_extractor/openrec_extractor.go

168 lines
4.5 KiB
Go
Raw Normal View History

package main
import (
"database/sql"
"encoding/json"
2020-08-17 05:10:29 +00:00
"errors"
"intimate"
"log"
"time"
2020-08-14 11:26:03 +00:00
"github.com/474420502/extractor"
"github.com/tidwall/gjson"
)
2020-07-22 12:00:02 +00:00
var estore = intimate.NewStoreExtractor()
var sstore = intimate.NewStoreSource(string(intimate.STOpenrec))
2020-08-17 05:10:29 +00:00
//UserInfo 提取信息的结构体
2020-08-14 11:26:03 +00:00
type UserInfo struct {
UserName string `exp:"//p[ contains(@class, 'c-global__user__profile__list__name__text')]"`
2020-08-17 05:10:29 +00:00
Followers int64 `exp:"//p[@class='c-global__user__count__row__right js-userCountFollowers']" mth:"r:ParseNumber"`
Views int64 `exp:"//ul[@class='c-contents']//p[@class='c-thumbnailVideo__footer__liveCount']" mth:"r:ExtractNumber"`
2020-08-14 11:26:03 +00:00
}
2020-08-17 05:10:29 +00:00
//UserLive 提取信息的结构体
type UserLive struct {
Title string `exp:"//h1[contains(@class,'MovieTitle__Title')]"`
LiveStartTime string `exp:"//meta[@itemprop='uploadDate']/@content"`
LiveEndTime string `exp:"//meta[@itemprop='duration']/@content"`
Tags []string `exp:"//a[contains(@class,'TagButton')]"`
}
// Execute 执行
func Execute() {
2020-07-16 10:31:13 +00:00
ps := intimate.NewPerfectShutdown()
2020-07-16 10:31:13 +00:00
var lasterr error = nil
for !ps.IsClose() {
2020-07-22 12:00:02 +00:00
var err error
source, err := sstore.Pop(intimate.TOpenrecUser, 0)
2020-07-16 10:31:13 +00:00
if err != nil {
if err != lasterr {
log.Println(err, lasterr)
lasterr = err
}
2020-07-31 10:04:10 +00:00
time.Sleep(time.Second * 5)
continue
2020-07-16 10:31:13 +00:00
}
2020-08-17 05:10:29 +00:00
lasterr = nil
2020-07-16 10:31:13 +00:00
sdata := source.Ext.([]byte)
datamap := gjson.ParseBytes(sdata).Map()
source.Operator = int32(intimate.OperatorError)
userId := datamap["var_user_id"].String()
2020-07-16 10:31:13 +00:00
streamer := &intimate.Streamer{}
streamer.UserId = userId
2020-08-17 05:10:29 +00:00
// streamer.Platform = intimate.Popenrec 不需要更新字段
2020-07-16 10:31:13 +00:00
2020-07-22 12:00:02 +00:00
htmlUser := datamap["html_user"]
2020-07-16 10:31:13 +00:00
2020-08-14 11:26:03 +00:00
userEtor := extractor.ExtractHtmlString(htmlUser.String())
2020-08-17 05:10:29 +00:00
ui, ok1 := userEtor.GetObjectByTag(UserInfo{}).(*UserInfo)
2020-08-14 11:26:03 +00:00
2020-07-22 12:00:02 +00:00
htmlLive := datamap["html_live"]
2020-08-17 05:10:29 +00:00
liveEtor := extractor.ExtractHtmlString(htmlLive.String())
ul, ok2 := liveEtor.GetObjectByTag(UserLive{}).(*UserLive)
2020-07-16 10:31:13 +00:00
2020-07-22 12:00:02 +00:00
jsonSupporters := datamap["json_supporters"]
clog := &intimate.CollectLog{}
2020-07-16 10:31:13 +00:00
2020-08-17 05:10:29 +00:00
if ok1 {
clog.Followers = sql.NullInt64{Int64: ui.Followers, Valid: true}
clog.Views = sql.NullInt64{Int64: ui.Views, Valid: true}
if ui.Views != 0 {
clog.IsLiveStreaming = true
}
streamer.UserName = sql.NullString{String: ui.UserName, Valid: true}
giverjson := jsonSupporters
var givers []interface{}
var gratuity int64 = 0
for _, v := range giverjson.Array() {
giverSource := gjson.Parse(v.String())
for _, item := range giverSource.Get("data.items").Array() {
givers = append(givers, item.Map())
gratuity += item.Get("total_yells").Int()
}
}
giversbytes, err := json.Marshal(givers)
if err != nil {
log.Println(err)
clog.ErrorMsg = sql.NullString{String: err.Error(), Valid: true}
} else {
clog.Giver = giversbytes
}
clog.Gratuity = sql.NullInt64{Int64: gratuity, Valid: true}
} else {
log.Println("UserInfo may be not exists")
estore.UpdateError(streamer, errors.New("UserInfo may be not exists"))
continue
}
//log.Println(ul)
if ok2 {
clog.LiveTitle = sql.NullString{String: ul.Title, Valid: true}
startTime, err := time.ParseInLocation("2006-01-02T15:04:05Z07:00", ul.LiveStartTime, time.Local)
if err != nil {
log.Println(err)
} else {
clog.LiveStartTime = sql.NullTime{Time: startTime.Local(), Valid: true}
duration, err := intimate.ParseDuration(ul.LiveEndTime)
if err != nil {
log.Println(err)
} else {
endTime := startTime.Add(duration)
clog.LiveStartTime = sql.NullTime{Time: endTime.Local(), Valid: true}
}
}
2020-07-16 10:31:13 +00:00
2020-08-17 05:10:29 +00:00
if tags, err := json.Marshal(ul.Tags); err == nil {
clog.Tags = tags
} else {
log.Println("json error", ul.Tags, clog.Tags)
}
}
2020-07-16 10:31:13 +00:00
streamer.Uid = source.StreamerId.Int64
streamer.UpdateTime = source.UpdateTime
2020-07-31 10:04:10 +00:00
streamer.Tags = clog.Tags
2020-07-16 10:31:13 +00:00
2020-08-11 10:26:17 +00:00
clog.Platform = intimate.Popenrec
clog.UserId = userId
clog.UpdateTime = source.UpdateTime
2020-08-17 05:10:29 +00:00
clog.StreamerUid = streamer.Uid
2020-07-16 10:31:13 +00:00
2020-07-31 10:04:10 +00:00
logUid := estore.InsertClog(clog)
2020-07-16 10:31:13 +00:00
LiveUrl := "https://www.openrec.tv/live/" + userId
streamer.LiveUrl = sql.NullString{String: LiveUrl, Valid: true}
streamer.LatestLogUid = logUid
// streamer.Operator = 0
log.Println(streamer.UserId)
estore.Update(streamer,
"user_name", streamer.UserName,
"user_id", streamer.UserId,
"live_url", streamer.LiveUrl,
"latest_log_uid", streamer.LatestLogUid,
"update_time", streamer.UpdateTime,
"tags", streamer.Tags,
)
2020-07-16 10:31:13 +00:00
source.Operator = int32(intimate.OperatorExtractorOK)
sstore.UpdateOperator(source)
2020-07-22 12:00:02 +00:00
}
2020-07-16 10:31:13 +00:00
}