2020-07-08 07:02:55 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"intimate"
|
|
|
|
"log"
|
2020-07-16 10:31:13 +00:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
2020-07-08 07:02:55 +00:00
|
|
|
"strconv"
|
2020-07-16 10:31:13 +00:00
|
|
|
"sync/atomic"
|
|
|
|
"syscall"
|
2020-07-08 07:02:55 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/474420502/hunter"
|
2020-07-09 09:09:46 +00:00
|
|
|
"github.com/tidwall/gjson"
|
2020-07-08 07:02:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var openrecRanking *OpenrecRanking
|
|
|
|
|
|
|
|
// store 源存储实例, 为存储源数据的实现. 表格具体参考sql/intimate_source.sql
|
2020-07-10 04:05:33 +00:00
|
|
|
var store *intimate.SourceStore = intimate.NewSourceStore(string(intimate.STOpenrec))
|
2020-07-08 07:02:55 +00:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
|
|
|
|
openrecRanking = &OpenrecRanking{}
|
|
|
|
openrecRanking.PreCurlUrl = `curl 'https://public.openrec.tv/external/api/v5/channel-ranks?period=monthly&date=&tag=&page=1' \
|
|
|
|
-H 'authority: public.openrec.tv' \
|
|
|
|
-H 'accept: application/json, text/javascript, */*; q=0.01' \
|
|
|
|
-H 'user-agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36' \
|
|
|
|
-H 'origin: https://www.openrec.tv' \
|
|
|
|
-H 'sec-fetch-site: same-site' \
|
|
|
|
-H 'sec-fetch-mode: cors' \
|
|
|
|
-H 'sec-fetch-dest: empty' \
|
|
|
|
-H 'referer: https://www.openrec.tv/ranking' \
|
|
|
|
-H 'accept-language: zh-CN,zh;q=0.9' \
|
|
|
|
-H 'if-none-match: W/"25edb-aUYBdmLqZcr6DW4ZWKX9r2aqolg"' \
|
|
|
|
--compressed`
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// OpenrecRanking 获取排名任务
|
|
|
|
type OpenrecRanking struct {
|
|
|
|
hunter.PreCurlUrl
|
|
|
|
}
|
|
|
|
|
|
|
|
// Execute 执行方法
|
|
|
|
func (or *OpenrecRanking) Execute(cxt *hunter.TaskContext) {
|
|
|
|
|
2020-07-16 10:31:13 +00:00
|
|
|
var loop int32 = 1
|
|
|
|
|
|
|
|
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-08 07:02:55 +00:00
|
|
|
|
|
|
|
resp, err := cxt.Hunt()
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
2020-07-17 11:20:08 +00:00
|
|
|
time.Sleep(time.Second * 2)
|
|
|
|
continue
|
2020-07-08 07:02:55 +00:00
|
|
|
}
|
|
|
|
|
2020-07-17 10:21:38 +00:00
|
|
|
tp := cxt.Temporary()
|
2020-07-08 07:02:55 +00:00
|
|
|
|
|
|
|
content := resp.Content()
|
2020-07-17 10:21:38 +00:00
|
|
|
if len(content) <= 200 { // 末页退出
|
|
|
|
finishpoint := time.Now()
|
|
|
|
log.Println("任务结束休眠, 下次启动时间:", finishpoint.Add(time.Minute*30))
|
2020-07-17 11:20:08 +00:00
|
|
|
for time.Now().Sub(finishpoint) < time.Minute*60 {
|
2020-07-17 10:21:38 +00:00
|
|
|
time.Sleep(time.Second)
|
|
|
|
if atomic.LoadInt32(&loop) > 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
continue
|
2020-07-08 07:02:55 +00:00
|
|
|
}
|
|
|
|
|
2020-07-09 09:09:46 +00:00
|
|
|
result := gjson.ParseBytes(content)
|
|
|
|
if result.IsArray() {
|
|
|
|
for _, User := range result.Array() {
|
|
|
|
data := &intimate.Source{}
|
|
|
|
userid := User.Get("channel.id").String()
|
|
|
|
|
2020-07-17 10:21:38 +00:00
|
|
|
data.Source = sql.NullString{String: userid, Valid: len(userid) > 0}
|
|
|
|
data.Url = tp.GetRawURL()
|
|
|
|
data.TargetType = string(intimate.TTOpenrecUser)
|
2020-07-09 09:09:46 +00:00
|
|
|
store.Insert(data)
|
|
|
|
}
|
|
|
|
}
|
2020-07-08 07:02:55 +00:00
|
|
|
|
2020-07-17 10:21:38 +00:00
|
|
|
querys := tp.GetQuery()
|
2020-07-08 07:02:55 +00:00
|
|
|
page, err := strconv.Atoi(querys.Get("page"))
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
return
|
|
|
|
}
|
2020-07-17 10:21:38 +00:00
|
|
|
|
2020-07-08 07:02:55 +00:00
|
|
|
page++
|
|
|
|
querys.Set("page", strconv.Itoa(page))
|
2020-07-17 10:21:38 +00:00
|
|
|
tp.SetQuery(querys)
|
2020-07-08 07:02:55 +00:00
|
|
|
time.Sleep(time.Second * 2)
|
|
|
|
}
|
|
|
|
}
|