2020-07-27 11:30:54 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-07-28 10:56:27 +00:00
|
|
|
"database/sql"
|
2020-07-27 11:30:54 +00:00
|
|
|
"encoding/json"
|
|
|
|
"intimate"
|
|
|
|
"log"
|
|
|
|
"regexp"
|
2020-08-11 10:26:17 +00:00
|
|
|
"strings"
|
2020-07-27 11:30:54 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/tebeka/selenium"
|
|
|
|
)
|
|
|
|
|
|
|
|
// sstore 源存储实例, 为存储源数据的实现. 表格具体参考sql/intimate_source.sql
|
|
|
|
var sstore *intimate.StoreSource = intimate.NewStoreSource(string(intimate.STTwitch))
|
|
|
|
|
|
|
|
// estore 解析存储连接实例
|
|
|
|
var estore *intimate.StoreExtractor = intimate.NewStoreExtractor()
|
|
|
|
|
2020-07-31 10:04:10 +00:00
|
|
|
func main() {
|
2020-07-27 11:30:54 +00:00
|
|
|
wd := intimate.GetChromeDriver(3030)
|
2020-07-31 10:04:10 +00:00
|
|
|
ps := intimate.NewPerfectShutdown()
|
|
|
|
|
2020-08-28 11:07:12 +00:00
|
|
|
var count = 0
|
|
|
|
var countlimt = 200
|
2020-07-27 11:30:54 +00:00
|
|
|
|
|
|
|
var lasterr error = nil
|
|
|
|
// var err error
|
|
|
|
|
2020-07-31 10:04:10 +00:00
|
|
|
for !ps.IsClose() {
|
2020-09-02 10:56:20 +00:00
|
|
|
streamer, err := estore.Pop(intimate.Ptwitch)
|
2020-07-27 11:30:54 +00:00
|
|
|
if streamer == nil || err != nil {
|
|
|
|
if err != lasterr {
|
|
|
|
log.Println(err, lasterr)
|
|
|
|
lasterr = err
|
|
|
|
}
|
|
|
|
time.Sleep(time.Second * 2)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
var updateUrl map[string]string
|
|
|
|
json.Unmarshal(streamer.UpdateUrl.([]byte), &updateUrl)
|
|
|
|
liveUrl := updateUrl["live"]
|
2020-08-11 10:26:17 +00:00
|
|
|
liveUrl = strings.Replace(liveUrl, "/watchparty", "", -1)
|
2020-07-27 11:30:54 +00:00
|
|
|
log.Println(liveUrl)
|
|
|
|
|
2020-07-28 10:56:27 +00:00
|
|
|
// err = wd.Get("https://www.twitch.tv/zoe_0601" + "/about")
|
|
|
|
err = wd.Get(liveUrl + "/about")
|
2020-07-27 11:30:54 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
2020-07-31 10:04:10 +00:00
|
|
|
estore.UpdateError(streamer, err)
|
|
|
|
time.Sleep(time.Second * 5)
|
2020-07-27 11:30:54 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-07-28 10:56:27 +00:00
|
|
|
streamer.LiveUrl = sql.NullString{String: liveUrl, Valid: true}
|
|
|
|
clog := &intimate.CollectLog{}
|
|
|
|
clog.UserId = streamer.UserId
|
2020-07-31 10:04:10 +00:00
|
|
|
clog.Gratuity = sql.NullInt64{Int64: 0, Valid: false}
|
2020-07-28 10:56:27 +00:00
|
|
|
|
2020-07-27 11:30:54 +00:00
|
|
|
time.Sleep(time.Millisecond * 500)
|
2020-07-31 10:04:10 +00:00
|
|
|
err = extractUserName(wd, streamer)
|
|
|
|
if err != nil {
|
2020-08-11 10:26:17 +00:00
|
|
|
_, err = wd.FindElement(selenium.ByXPATH, "//a[@data-a-target='browse-channels-button']")
|
|
|
|
if err == nil {
|
|
|
|
log.Println(streamer.UserId, "may be cancell")
|
|
|
|
streamer.Operator = 5
|
|
|
|
streamer.UpdateTime = sql.NullTime{Time: time.Now(), Valid: true}
|
|
|
|
estore.UpdateStreamer(streamer)
|
|
|
|
}
|
2020-07-31 10:04:10 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
err = extractFollowers(wd, clog)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
2020-07-28 10:56:27 +00:00
|
|
|
|
2020-07-31 10:04:10 +00:00
|
|
|
err = extractViews(wd, clog) // views + tags + gratuity
|
2020-07-28 10:56:27 +00:00
|
|
|
if err != nil {
|
|
|
|
// 不直播时提取礼物 gratuity
|
|
|
|
wd.WaitWithTimeout(func(web selenium.WebDriver) (bool, error) {
|
|
|
|
channelchat, err := wd.FindElement(selenium.ByXPATH, `//a[@data-a-target="channel-home-tab-Chat"]`)
|
|
|
|
btn, _ := web.FindElement(selenium.ByXPATH, `//button[@data-test-selector="expand-grabber"]`)
|
|
|
|
if (err == nil && channelchat != nil) || btn != nil {
|
|
|
|
if channelchat != nil {
|
|
|
|
channelchat.Click()
|
|
|
|
}
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
extractGratuity(wd, clog)
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
return false, nil
|
|
|
|
|
|
|
|
}, time.Second*4)
|
|
|
|
}
|
|
|
|
|
|
|
|
streamer.Platform = intimate.Ptwitch
|
2020-08-11 10:26:17 +00:00
|
|
|
clog.Platform = streamer.Platform
|
2020-07-28 10:56:27 +00:00
|
|
|
clog.UpdateTime = sql.NullTime{Time: time.Now(), Valid: true}
|
2020-07-31 10:04:10 +00:00
|
|
|
lastClogId := estore.InsertClog(clog)
|
2020-07-28 10:56:27 +00:00
|
|
|
|
2020-07-31 10:04:10 +00:00
|
|
|
streamer.Operator = 10
|
2020-07-28 10:56:27 +00:00
|
|
|
streamer.LatestLogUid = lastClogId
|
2020-07-31 10:04:10 +00:00
|
|
|
if clog.Tags != nil {
|
|
|
|
streamer.Tags = clog.Tags
|
|
|
|
}
|
2020-07-28 10:56:27 +00:00
|
|
|
|
2020-07-31 10:04:10 +00:00
|
|
|
switch fl := clog.Followers.Int64; {
|
|
|
|
case fl > 100000:
|
|
|
|
streamer.UpdateInterval = 120
|
|
|
|
case fl > 10000:
|
2020-08-28 11:07:12 +00:00
|
|
|
streamer.UpdateInterval = 240 * 2
|
2020-07-31 10:04:10 +00:00
|
|
|
case fl > 1000:
|
2020-08-28 11:07:12 +00:00
|
|
|
streamer.UpdateInterval = 360 * 2
|
2020-07-31 10:04:10 +00:00
|
|
|
case fl > 100:
|
2020-08-28 11:07:12 +00:00
|
|
|
streamer.UpdateInterval = 720 * 2
|
2020-07-31 10:04:10 +00:00
|
|
|
case fl > 0:
|
2020-08-28 11:07:12 +00:00
|
|
|
streamer.UpdateInterval = 1440 * 4
|
2020-07-31 10:04:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
streamer.UpdateTime = clog.UpdateTime
|
|
|
|
estore.UpdateStreamer(streamer)
|
2020-08-28 11:07:12 +00:00
|
|
|
count++
|
|
|
|
if count >= countlimt {
|
|
|
|
count = 0
|
|
|
|
// wd.Quit()
|
|
|
|
wd = intimate.GetChromeDriver(3030)
|
|
|
|
}
|
2020-07-28 10:56:27 +00:00
|
|
|
}
|
2020-07-31 10:04:10 +00:00
|
|
|
|
|
|
|
wd.Close()
|
|
|
|
wd.Quit()
|
2020-07-28 10:56:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func extractUserName(wd selenium.WebDriver, streamer *intimate.Streamer) error {
|
|
|
|
return wd.WaitWithTimeout(func(web selenium.WebDriver) (bool, error) {
|
|
|
|
label, err := web.FindElement(selenium.ByXPATH, "//a[@class='tw-interactive']//h1")
|
|
|
|
if err == nil {
|
2020-07-31 10:04:10 +00:00
|
|
|
if ltxt, err := label.Text(); err == nil && ltxt != "" {
|
|
|
|
// log.Println("label:", ltxt)
|
2020-07-28 10:56:27 +00:00
|
|
|
streamer.UserName = sql.NullString{String: ltxt, Valid: true}
|
|
|
|
return true, nil
|
2020-07-27 11:30:54 +00:00
|
|
|
}
|
2020-07-28 10:56:27 +00:00
|
|
|
}
|
|
|
|
return false, err
|
2020-07-31 10:04:10 +00:00
|
|
|
}, 15*time.Second)
|
2020-07-28 10:56:27 +00:00
|
|
|
}
|
2020-07-27 11:30:54 +00:00
|
|
|
|
2020-07-28 10:56:27 +00:00
|
|
|
func extractFollowers(wd selenium.WebDriver, clog *intimate.CollectLog) error {
|
|
|
|
return wd.WaitWithTimeout(func(web selenium.WebDriver) (bool, error) {
|
|
|
|
efollowers, err := web.FindElement(selenium.ByXPATH, "//div[@data-a-target='about-panel']//div[@class='tw-align-center']")
|
2020-07-27 11:30:54 +00:00
|
|
|
if err != nil {
|
2020-07-28 10:56:27 +00:00
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
followers, err := efollowers.Text()
|
|
|
|
if err != nil || followers == "" {
|
|
|
|
return false, err
|
2020-07-27 11:30:54 +00:00
|
|
|
}
|
2020-07-28 10:56:27 +00:00
|
|
|
followers = regexp.MustCompile(`[\d,]+`).FindString(followers)
|
|
|
|
fint, _ := intimate.ParseNumber(followers)
|
|
|
|
clog.Followers = sql.NullInt64{Int64: int64(fint), Valid: true}
|
2020-07-31 10:04:10 +00:00
|
|
|
// log.Println("followers: ", followers, fint)
|
2020-07-28 10:56:27 +00:00
|
|
|
return true, nil
|
2020-07-31 10:04:10 +00:00
|
|
|
}, 4*time.Second)
|
2020-07-28 10:56:27 +00:00
|
|
|
}
|
2020-07-27 11:30:54 +00:00
|
|
|
|
2020-07-28 10:56:27 +00:00
|
|
|
func extractViews(wd selenium.WebDriver, clog *intimate.CollectLog) error {
|
|
|
|
return wd.WaitWithTimeout(func(web selenium.WebDriver) (bool, error) {
|
|
|
|
views, err := web.FindElement(selenium.ByXPATH, "//a[@data-a-target='home-live-overlay-button']/span")
|
|
|
|
if views != nil {
|
|
|
|
if txt, err := views.Text(); err == nil {
|
|
|
|
|
|
|
|
vint, _ := intimate.ParseNumber(txt)
|
|
|
|
clog.Views = sql.NullInt64{Int64: vint, Valid: true}
|
2020-07-31 10:04:10 +00:00
|
|
|
// log.Println("views:", txt)
|
2020-07-28 10:56:27 +00:00
|
|
|
views.Click()
|
|
|
|
|
|
|
|
extractTags(wd, clog)
|
|
|
|
extractTitle(wd, clog)
|
|
|
|
extractGratuity(wd, clog)
|
|
|
|
|
|
|
|
return true, nil
|
2020-07-27 11:30:54 +00:00
|
|
|
}
|
2020-07-28 10:56:27 +00:00
|
|
|
}
|
|
|
|
return false, err
|
|
|
|
}, time.Second*4)
|
|
|
|
}
|
|
|
|
|
|
|
|
func extractTitle(wd selenium.WebDriver, clog *intimate.CollectLog) error {
|
|
|
|
return wd.WaitWithTimeout(func(web selenium.WebDriver) (bool, error) {
|
|
|
|
title, err := web.FindElement(selenium.ByXPATH, `//h2[@data-a-target='stream-title']`)
|
|
|
|
if err == nil {
|
|
|
|
if txt, err := title.Text(); err == nil {
|
|
|
|
clog.LiveTitle = sql.NullString{String: txt, Valid: true}
|
|
|
|
return true, nil
|
2020-07-27 11:30:54 +00:00
|
|
|
}
|
2020-07-28 10:56:27 +00:00
|
|
|
}
|
|
|
|
return false, err
|
|
|
|
}, time.Second*4)
|
|
|
|
}
|
2020-07-27 11:30:54 +00:00
|
|
|
|
2020-07-28 10:56:27 +00:00
|
|
|
func extractTags(wd selenium.WebDriver, clog *intimate.CollectLog) error {
|
|
|
|
return wd.WaitWithTimeout(func(web selenium.WebDriver) (bool, error) {
|
|
|
|
tags, err := web.FindElements(selenium.ByXPATH, "//a[@aria-label and @data-a-target and @href]/div[@class and text()]")
|
|
|
|
if len(tags) == 0 {
|
|
|
|
return false, err
|
2020-07-27 11:30:54 +00:00
|
|
|
}
|
2020-07-28 10:56:27 +00:00
|
|
|
|
|
|
|
var stags []string
|
|
|
|
for _, tag := range tags {
|
|
|
|
if txt, err := tag.Text(); err == nil {
|
|
|
|
stags = append(stags, txt)
|
|
|
|
} else {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(stags) > 0 {
|
|
|
|
if tagbuf, err := json.Marshal(stags); err == nil {
|
|
|
|
clog.Tags = tagbuf
|
|
|
|
} else {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
2020-07-27 11:30:54 +00:00
|
|
|
}
|
|
|
|
|
2020-07-28 10:56:27 +00:00
|
|
|
return true, nil
|
|
|
|
}, time.Second*4)
|
|
|
|
}
|
|
|
|
|
|
|
|
func extractGratuity(wd selenium.WebDriver, clog *intimate.CollectLog) error {
|
|
|
|
return wd.WaitWithTimeout(func(web selenium.WebDriver) (bool, error) {
|
|
|
|
btn, err := web.FindElement(selenium.ByXPATH, `//button[@data-test-selector="expand-grabber"]`)
|
|
|
|
if err == nil {
|
|
|
|
btn.Click()
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
gifcount, err := web.FindElements(selenium.ByXPATH, `//div[@class="sub-gift-count tw-flex"]/p`)
|
|
|
|
if err == nil {
|
|
|
|
var gratuity int64 = 0
|
|
|
|
for _, gc := range gifcount {
|
|
|
|
if gtxt, err := gc.Text(); err == nil {
|
|
|
|
gint, _ := intimate.ParseNumber(gtxt)
|
|
|
|
gratuity += gint
|
|
|
|
} else {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
clog.Gratuity = sql.NullInt64{Int64: gratuity, Valid: true}
|
|
|
|
}
|
|
|
|
return true, nil
|
2020-07-27 11:30:54 +00:00
|
|
|
}
|
|
|
|
|
2020-07-28 10:56:27 +00:00
|
|
|
return false, err
|
|
|
|
}, time.Second*4)
|
2020-07-27 11:30:54 +00:00
|
|
|
}
|