imitater/task.go

158 lines
3.6 KiB
Go
Raw Permalink Normal View History

2018-12-07 10:21:23 +00:00
package imitater
2018-11-27 09:46:29 +00:00
import (
"encoding/json"
"log"
2018-12-06 10:34:10 +00:00
"time"
"474420502.top/test/logdb"
2018-12-07 11:09:38 +00:00
"474420502.top/eson/structure/circular_linked"
uuid "github.com/satori/go.uuid"
2018-12-07 11:09:38 +00:00
2018-12-07 10:21:23 +00:00
"474420502.top/eson/crontabex"
2018-11-27 09:46:29 +00:00
"474420502.top/eson/curl2info"
"474420502.top/eson/requests"
)
// Task 任务
type Task struct {
2018-11-30 10:46:49 +00:00
ITask
2018-12-07 10:21:23 +00:00
name string
2018-12-06 10:34:10 +00:00
crontab *crontab.Crontab
2018-11-30 10:46:49 +00:00
curl *curl2info.CURL
workflow *requests.Workflow
session *requests.Session
2018-12-07 11:09:38 +00:00
proxies clinked.CircularLinked
2018-12-18 07:33:57 +00:00
adinfo ADInfo
2018-11-27 09:46:29 +00:00
}
2018-12-18 06:38:59 +00:00
// Init 初始化, 会被Persion默认调用, 可以用来覆盖
func (task *Task) Init() {
}
2018-12-07 10:21:23 +00:00
// SetName 任务的名字
func (task *Task) SetName(name string) {
task.name = name
}
// GetName 获取任务的名字
func (task *Task) GetName() string {
return task.name
}
// SetCurl 设置任务的curl信息类
2018-12-06 10:34:10 +00:00
func (task *Task) SetCurl(curl *curl2info.CURL) {
task.curl = curl
}
2018-11-27 09:46:29 +00:00
// GetCurl 获取任务的curl信息类
2018-12-07 10:21:23 +00:00
func (task *Task) GetCurl() *curl2info.CURL {
return task.curl
}
2018-11-27 09:46:29 +00:00
2018-12-07 11:09:38 +00:00
// AddProxies 添加代理集合
func (task *Task) AddProxies(proxy string) {
2018-12-09 13:39:44 +00:00
task.proxies.Append(proxy)
2018-12-06 10:34:10 +00:00
}
// GetProxies 获取代理的字符串
2018-12-07 11:09:38 +00:00
func (task *Task) GetProxies() *clinked.CircularLinked {
return &task.proxies
2018-12-06 10:34:10 +00:00
}
2018-12-18 07:33:57 +00:00
// SetADInfo 设置广告的信息结构
func (task *Task) SetADInfo(adinfo ADInfo) {
task.adinfo = adinfo
}
// GetADInfo 获取广告的信息结构
func (task *Task) GetADInfo() *ADInfo {
return &task.adinfo
}
// SetCrontab 设置crontab的控制规则字符串
2018-12-06 10:34:10 +00:00
func (task *Task) SetCrontab(cron string) {
task.crontab = crontab.NewCrontab(cron)
}
// GetCrontab 设置上次执行的状态 成功true 失败false
func (task *Task) GetCrontab() *crontab.Crontab {
return task.crontab
}
// TimeUp 判断是否到了下次执行的时间点
2018-12-06 20:47:22 +00:00
func (task *Task) TimeUp() bool {
return task.crontab.TimeUp()
}
// NextTime 下次执行的时间点
2018-12-06 20:47:22 +00:00
func (task *Task) NextTime() time.Time {
return task.crontab.NextTime()
}
2018-12-07 10:21:23 +00:00
// Workflow 根据persistent 是否返回现有session(or 新建 session),创建Workflow的信息, 便于设置或者更改参数, Session持久化
func (task *Task) Workflow(persistent bool) *requests.Workflow {
2018-12-06 20:47:22 +00:00
if persistent {
2018-12-09 13:39:44 +00:00
task.workflow = task.curl.CreateWorkflow(task.Session())
return task.workflow
}
2018-12-07 11:09:38 +00:00
proxies := task.GetProxies()
ses := task.curl.CreateSession()
if proxies.Size() > 0 {
2018-12-09 13:39:44 +00:00
proxy := proxies.Cursor().GetValue().(string)
ses.SetConfig(requests.CProxy, proxy)
2018-12-07 11:09:38 +00:00
proxies.MoveNext()
}
2018-12-09 13:39:44 +00:00
return task.curl.CreateWorkflow(ses)
}
// Request 根据curl信息执行,没持久化
func (task *Task) Request() (*requests.Response, error) {
return task.Workflow(false).Execute()
2018-12-06 20:47:22 +00:00
}
// Session 获取Session的信息 只保留session的数据和url参数.
func (task *Task) Session() *requests.Session {
if task.session == nil {
task.session = task.curl.CreateSession()
}
2018-12-07 11:09:38 +00:00
if task.proxies.Size() > 0 {
2018-12-09 13:39:44 +00:00
proxy := task.proxies.Cursor().GetValue().(string)
task.session.SetConfig(requests.CProxy, proxy)
2018-12-07 11:09:38 +00:00
task.proxies.MoveNext()
}
return task.session
}
// ADDataSave 公众存储数据的接口
2018-12-21 20:13:21 +00:00
func ADDataSave(task ITask, db *logdb.LogDB, adDataList []string) bool {
if len(adDataList) != 0 {
adDataStore, err := json.Marshal(adDataList)
if err != nil {
panic(err)
}
// log.Println(string(adDataStore))
uid, err := uuid.NewV4()
if err != nil {
panic(err)
}
log.Println(uid.String())
adinfo := task.GetADInfo()
db.ADInsert(uid.String(), adinfo.Device, adinfo.Platform, adinfo.AreaCC, "",
string(adDataStore), adinfo.SpiderID, adinfo.Channel, adinfo.Media, adinfo.CatchAccountID, 0, adinfo.Priority, time.Now())
2018-12-21 20:13:21 +00:00
return true
}
2018-12-21 20:13:21 +00:00
return false
}