2020-07-24 10:48:33 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"encoding/json"
|
|
|
|
"intimate"
|
|
|
|
"log"
|
2020-07-26 16:35:41 +00:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
2020-07-24 10:48:33 +00:00
|
|
|
"regexp"
|
2020-07-26 16:35:41 +00:00
|
|
|
"sync/atomic"
|
|
|
|
"syscall"
|
2020-07-24 10:48:33 +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()
|
|
|
|
|
|
|
|
// 获取类型的所有频道链接
|
|
|
|
|
|
|
|
// UserList 频道链接
|
|
|
|
type UserList struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
// Execute 执行任务
|
|
|
|
func (cl *UserList) Execute() {
|
|
|
|
// DELETE FROM source_twitch WHERE uid NOT IN (SELECT MAX(s.uid) FROM (SELECT uid, source FROM source_twitch ) s GROUP BY s.source) ;
|
|
|
|
//article//a[@data-a-target='preview-card-title-link']
|
2020-07-26 16:35:41 +00:00
|
|
|
|
2020-07-24 10:48:33 +00:00
|
|
|
wd := intimate.GetChromeDriver(3030)
|
2020-07-27 11:30:54 +00:00
|
|
|
defer wd.Close()
|
2020-07-24 10:48:33 +00:00
|
|
|
|
2020-07-26 16:35:41 +00:00
|
|
|
var loop int32 = 1
|
2020-07-28 10:56:27 +00:00
|
|
|
var count = 0
|
2020-07-24 10:48:33 +00:00
|
|
|
|
2020-07-26 16:35:41 +00:00
|
|
|
go func() {
|
|
|
|
signalchan := make(chan os.Signal)
|
|
|
|
signal.Notify(signalchan, syscall.SIGKILL, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGSTOP)
|
|
|
|
log.Println("accept stop command:", <-signalchan)
|
|
|
|
atomic.StoreInt32(&loop, 0)
|
|
|
|
}()
|
|
|
|
|
|
|
|
for atomic.LoadInt32(&loop) > 0 {
|
2020-07-24 10:48:33 +00:00
|
|
|
|
2020-07-26 16:35:41 +00:00
|
|
|
var err error
|
|
|
|
sourceChannel, err := sstore.Pop(intimate.TTwitchChannel)
|
2020-07-24 10:48:33 +00:00
|
|
|
if err != nil {
|
2020-07-26 16:35:41 +00:00
|
|
|
panic(err)
|
2020-07-24 10:48:33 +00:00
|
|
|
}
|
|
|
|
|
2020-07-26 16:35:41 +00:00
|
|
|
weburl := sourceChannel.Source.String + "?sort=VIEWER_COUNT"
|
|
|
|
err = wd.Get(weburl)
|
2020-07-24 10:48:33 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2020-07-26 16:35:41 +00:00
|
|
|
wd.WaitWithTimeout(func(wd selenium.WebDriver) (bool, error) {
|
|
|
|
_, err := wd.FindElement(selenium.ByXPATH, "(//div/p[@class=''])[last()]")
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}, time.Second*10)
|
2020-07-24 10:48:33 +00:00
|
|
|
|
2020-07-26 16:35:41 +00:00
|
|
|
btn, err := wd.FindElement(selenium.ByXPATH, "//button[@data-a-target='browse-sort-menu']")
|
2020-07-24 10:48:33 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
continue
|
|
|
|
}
|
2020-07-26 16:35:41 +00:00
|
|
|
btn.Click()
|
|
|
|
|
|
|
|
var elements []selenium.WebElement
|
|
|
|
var liveurls = 0
|
2020-07-27 11:30:54 +00:00
|
|
|
var delayerror = 2
|
2020-07-26 16:35:41 +00:00
|
|
|
for i := 0; i < 200 && atomic.LoadInt32(&loop) > 0; i++ {
|
|
|
|
elements, err = wd.FindElements(selenium.ByXPATH, "(//div/p[@class=''])[last()]")
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
break
|
|
|
|
}
|
2020-07-27 11:30:54 +00:00
|
|
|
time.Sleep(time.Millisecond * 500)
|
2020-07-26 16:35:41 +00:00
|
|
|
wd.KeyDown(selenium.EndKey)
|
|
|
|
wd.KeyUp(selenium.EndKey)
|
2020-07-27 11:30:54 +00:00
|
|
|
time.Sleep(time.Millisecond * 1500)
|
2020-07-26 16:35:41 +00:00
|
|
|
if len(elements) == liveurls {
|
2020-07-28 10:56:27 +00:00
|
|
|
delayerror--
|
2020-07-26 16:35:41 +00:00
|
|
|
if delayerror <= 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
} else {
|
2020-07-27 11:30:54 +00:00
|
|
|
delayerror = 2
|
2020-07-26 16:35:41 +00:00
|
|
|
}
|
|
|
|
liveurls = len(elements)
|
2020-07-24 10:48:33 +00:00
|
|
|
}
|
2020-07-26 16:35:41 +00:00
|
|
|
elements, err = wd.FindElements(selenium.ByXPATH, "//article//a[@data-a-target='preview-card-title-link' and @href]")
|
2020-07-24 10:48:33 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-07-26 16:35:41 +00:00
|
|
|
for _, e := range elements {
|
|
|
|
|
|
|
|
attr, err := e.GetAttribute("href")
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
streamer := &intimate.Streamer{}
|
|
|
|
|
|
|
|
matches := regexp.MustCompile(`https://www.twitch.tv/(\w+)`).FindStringSubmatch(attr)
|
|
|
|
if len(matches) == 2 {
|
|
|
|
streamer.UserId = matches[1]
|
|
|
|
} else {
|
|
|
|
log.Println(attr)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
streamer.Platform = intimate.Ptwitch
|
|
|
|
|
|
|
|
updateUrl := make(map[string]string)
|
|
|
|
updateUrl["live"] = attr
|
|
|
|
streamer.LiveUrl = sql.NullString{String: attr, Valid: true}
|
|
|
|
data, err := json.Marshal(updateUrl)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
streamer.UpdateUrl = data
|
|
|
|
streamer.Operator = 0
|
|
|
|
|
|
|
|
estore.InsertStreamer(streamer)
|
|
|
|
}
|
|
|
|
log.Println("streamer insert", len(elements))
|
2020-07-28 10:56:27 +00:00
|
|
|
count++
|
|
|
|
if count >= 100 {
|
|
|
|
wd.Close()
|
|
|
|
wd = intimate.GetChromeDriver(3030)
|
|
|
|
count = 0
|
|
|
|
}
|
2020-07-24 10:48:33 +00:00
|
|
|
}
|
|
|
|
}
|