finish tag slice

This commit is contained in:
eson
2020-08-11 18:26:17 +08:00
parent 23fa32b4ae
commit b82b4f5b77
13 changed files with 1317 additions and 211 deletions

View File

@@ -0,0 +1,140 @@
package main
import (
"database/sql"
"intimate"
"log"
"regexp"
"strconv"
"strings"
"time"
"github.com/474420502/extractor"
"github.com/474420502/requests"
)
// sstore 源存储实例, 为存储源数据的实现. 表格具体参考sql/intimate_source.sql
var sstore *intimate.StoreSource = intimate.NewStoreSource(string(intimate.STOpenrec))
// estore 解析存储连接实例
var estore *intimate.StoreExtractor = intimate.NewStoreExtractor()
type LiveData struct {
UserName string `exp:"//span[@class='tw-live-author__info-username']" method:"Text"`
Follower string `exp:"(//span[@class='tw-user-nav-list-count'])[2]" method:"Text"`
MaxViews string `exp:"//span[@id='max_viewer_count']" method:"Text"`
LiveTitle string `exp:"//meta[@property='og:title']" method:"AttributeValue,content"`
LiveStart string `exp:"//span[@id='updatetimer']" method:"AttributeValue,data-started-at"`
LiveDuration string `exp:"//span[@id='updatetimer']" method:"AttributeValue,data-duration"`
Tags []string `exp:"//div[@class='tw-live-author__commandbox--tags']//a[@class='tag tag-info']"`
}
func main() {
ps := intimate.NewPerfectShutdown()
for !ps.IsClose() {
streamer, err := estore.Pop(intimate.Ptwitcasting)
if err != nil {
log.Println(err)
}
ses := requests.NewSession()
resp, err := ses.Get("https://twitcasting.tv/kyunenee09").Execute()
if err != nil {
log.Panic(err)
}
var ldata *LiveData
etor := extractor.ExtractXml(resp.Content())
ldata = etor.GetObjectByTag(LiveData{}).(*LiveData)
ldata.MaxViews = regexp.MustCompile("\\d+").FindString(ldata.MaxViews)
ldata.LiveStart = ldata.LiveStart[:len(ldata.LiveStart)-3]
ldata.LiveDuration = ldata.LiveDuration[:len(ldata.LiveDuration)-3]
// log.Println(etor.GetObjectByTag(LiveData{}))
coincount := 0
for i := 0; ; i++ {
giverurl := "https://twitcasting.tv/kyunenee09/backers/" + strconv.Itoa(i)
resp, err = ses.Get(giverurl).Execute()
if err != nil {
log.Panic(err)
}
etor := extractor.ExtractXml(resp.Content())
xp, err := etor.XPaths("//td[@class='tw-memorial-table-recent-point']")
if err != nil {
log.Panic(err)
}
coins := xp.GetTexts()
for _, cointxt := range coins {
scointxt := strings.Split(cointxt, "/")
if len(scointxt) == 2 {
coin := strings.Trim(scointxt[1], " ")
c, err := strconv.Atoi(coin)
if err == nil {
coincount += c
}
log.Println(coin, coincount)
} else {
log.Println("coin error: ", cointxt)
}
}
if len(coins) < 20 {
break
}
}
streamer.Platform = intimate.Ptwitcasting
streamer.UpdateTime = sql.NullTime{Time: time.Now(), Valid: true}
streamer.UpdateInterval = 60
streamer.UserName = sql.NullString{String: ldata.UserName, Valid: true}
streamer.Operator = 0
clog := &intimate.CollectLog{}
clog.UserId = streamer.UserId
clog.Gratuity = sql.NullInt64{Int64: int64(coincount), Valid: true}
clog.Platform = streamer.Platform
clog.UpdateTime = streamer.UpdateTime
clog.LiveTitle = sql.NullString{String: ldata.LiveTitle, Valid: true}
fl, err := strconv.Atoi(ldata.Follower)
if err == nil {
clog.Followers = sql.NullInt64{Int64: int64(fl), Valid: true}
} else {
log.Println(err)
}
views, err := strconv.Atoi(ldata.MaxViews)
if err == nil {
clog.Views = sql.NullInt64{Int64: int64(views), Valid: true}
} else {
log.Println(err)
}
st, err := strconv.Atoi(ldata.LiveStart)
if err == nil {
startTime := time.Unix(int64(st), 0)
clog.LiveStartTime = sql.NullTime{Time: startTime, Valid: true}
dt, err := strconv.Atoi(ldata.LiveDuration)
if err == nil {
endTime := startTime.Add((time.Duration)(dt) * time.Second)
clog.LiveEndTime = sql.NullTime{Time: endTime, Valid: true}
} else {
log.Println(err)
}
} else {
log.Println(err)
}
streamer.LatestLogUid = estore.InsertClog(clog)
estore.UpdateStreamer(streamer)
break
}
}

View File

@@ -0,0 +1,12 @@
package main
import "testing"
// type LiveData struct {
// UserName string `exp:".//span[@class='tw-live-author__info-username']" method:"Text"`
// Follower string `exp:".//span[@class='tw-user-nav-list-count']" method:"Text"`
// }
func TestMain(t *testing.T) {
main()
}