EuroTrackProcesser.go 6.2 KB
Newer Older
M
monomania 已提交
1 2 3
package proc

import (
4
	"encoding/json"
M
monomania 已提交
5
	"github.com/PuerkitoBio/goquery"
S
shi.zeyuan 已提交
6 7 8
	"github.com/hu17889/go_spider/core/common/page"
	"github.com/hu17889/go_spider/core/pipeline"
	"github.com/hu17889/go_spider/core/spider"
M
monomania 已提交
9 10
	"strconv"
	"strings"
11
	"tesou.io/platform/foot-parent/foot-api/common/base"
12 13
	"tesou.io/platform/foot-parent/foot-api/module/match/pojo"
	entity3 "tesou.io/platform/foot-parent/foot-api/module/odds/pojo"
S
shi.zeyuan 已提交
14 15 16
	"tesou.io/platform/foot-parent/foot-core/module/elem/service"
	service2 "tesou.io/platform/foot-parent/foot-core/module/odds/service"
	"tesou.io/platform/foot-parent/foot-spider/module/win007"
17
	"tesou.io/platform/foot-parent/foot-spider/module/win007/down"
M
monomania 已提交
18 19 20
	"time"
)

21
type EuroTrackProcesser struct {
S
shi.zeyuan 已提交
22 23 24
	service.CompService
	service2.EuroLastService
	service2.EuroHisService
25
	service2.EuroTrackService
M
monomania 已提交
26
	//入参
S
1.xx  
shi.zeyuan 已提交
27 28
	//是否是单线程
	SingleThread bool
29
	MatchLastList []*pojo.MatchLast
M
monomania 已提交
30 31
	//博彩公司对应的win007id
	CompWin007Ids      []string
32
	Win007idMatchidMap map[string]string
M
monomania 已提交
33 34
}

35
func GetEuroTrackProcesser() *EuroTrackProcesser {
M
monomania 已提交
36 37 38
	processer := &EuroTrackProcesser{}
	processer.Init()
	return processer
M
monomania 已提交
39 40
}

M
monomania 已提交
41 42
func (this *EuroTrackProcesser) Init() {
	//初始化参数值
43
	this.Win007idMatchidMap = map[string]string{}
M
monomania 已提交
44
}
M
monomania 已提交
45

M
monomania 已提交
46 47 48 49 50
func (this *EuroTrackProcesser) Setup(temp *EuroTrackProcesser) {
	//设置参数值
	this.CompWin007Ids = temp.CompWin007Ids
}

M
monomania 已提交
51
func (this *EuroTrackProcesser) Startup() {
M
monomania 已提交
52

M
monomania 已提交
53
	var newSpider *spider.Spider
S
1.xx  
shi.zeyuan 已提交
54 55
	processer := this
	newSpider = spider.NewSpider(processer, "EuroTrackProcesser")
M
monomania 已提交
56 57
	for i, v := range this.MatchLastList {

M
1.xxx  
monomania 已提交
58
		if !this.SingleThread &&i%1000 == 0 { //10000个比赛一个spider,一个赛季大概有30万场比赛,最多30spider
S
1.xxx  
shi.zeyuan 已提交
59 60 61 62
			//先将前面的spider启动
			newSpider.SetDownloader(down.NewMWin007Downloader())
			newSpider = newSpider.AddPipeline(pipeline.NewPipelineConsole())
			newSpider.SetSleepTime("rand", 1, 300)
63
			newSpider.SetThreadnum(10).Run()
S
1.xxx  
shi.zeyuan 已提交
64

M
monomania 已提交
65
			processer = GetEuroTrackProcesser()
M
monomania 已提交
66
			processer.Setup(this)
M
monomania 已提交
67
			newSpider = spider.NewSpider(processer, "EuroTrackProcesser"+strconv.Itoa(i))
M
monomania 已提交
68
		}
M
monomania 已提交
69

M
monomania 已提交
70 71
		temp_flag := v.Ext[win007.MODULE_FLAG]
		bytes, _ := json.Marshal(temp_flag)
72 73 74
		matchExt := new(pojo.MatchExt)
		json.Unmarshal(bytes, matchExt)
		win007_id := matchExt.Sid
M
monomania 已提交
75
		processer.Win007idMatchidMap[win007_id] = v.Id
M
monomania 已提交
76

77
		base_url := strings.Replace(win007.WIN007_EUROODD_BET_URL_PATTERN, "${scheid}", win007_id, 1)
M
monomania 已提交
78
		for _, v := range processer.CompWin007Ids {
79
			url := strings.Replace(base_url, "${cId}", v, 1)
M
monomania 已提交
80 81 82
			newSpider = newSpider.AddUrl(url, "html")
		}
	}
M
monomania 已提交
83

M
monomania 已提交
84 85
	newSpider.SetDownloader(down.NewMWin007Downloader())
	newSpider = newSpider.AddPipeline(pipeline.NewPipelineConsole())
M
monomania 已提交
86
	newSpider.SetSleepTime("rand", 1, 300)
M
monomania 已提交
87 88
	newSpider.SetThreadnum(1).Run()

M
monomania 已提交
89 90
}

91
func (this *EuroTrackProcesser) findParamVal(url string, paramName string) string {
M
monomania 已提交
92 93 94 95 96 97 98 99 100 101
	paramUrl := strings.Split(url, "?")[1]
	paramArr := strings.Split(paramUrl, "&")
	for _, v := range paramArr {
		if strings.Contains(v, paramName) {
			return strings.Split(v, "=")[1]
		}
	}
	return ""
}

102
func (this *EuroTrackProcesser) Process(p *page.Page) {
M
monomania 已提交
103 104
	request := p.GetRequest()
	if !p.IsSucc() {
105
		base.Log.Error("URL:", request.Url, p.Errormsg())
M
monomania 已提交
106 107 108 109 110 111
		return
	}

	current_year := time.Now().Format("2006")

	win007_matchId := this.findParamVal(request.Url, "scheid")
112
	matchId := this.Win007idMatchidMap[win007_matchId]
M
monomania 已提交
113 114 115

	win007_betCompId := this.findParamVal(request.Url, "cId")

116
	var track_list = make([]*entity3.EuroTrack, 0)
M
monomania 已提交
117 118 119 120 121 122 123

	table_node := p.GetHtmlParser().Find(" table.mytable3 tr")
	table_node.Each(func(i int, selection *goquery.Selection) {
		if i < 2 {
			return
		}

124 125 126 127
		track := new(entity3.EuroTrack)
		track_list = append(track_list, track)
		track.MatchId = matchId
		track.CompId = win007_betCompId
M
monomania 已提交
128 129 130

		td_list_node := selection.Find(" td ")
		td_list_node.Each(func(ii int, selection *goquery.Selection) {
S
shi.zeyuan 已提交
131
			val := strings.TrimSpace(selection.Text())
M
monomania 已提交
132 133 134 135 136 137 138
			if "" == val {
				return
			}

			switch ii {
			case 0:
				temp, _ := strconv.ParseFloat(val, 64)
139
				track.Sp3 = temp
M
monomania 已提交
140 141
			case 1:
				temp, _ := strconv.ParseFloat(val, 64)
142
				track.Sp1 = temp
M
monomania 已提交
143 144
			case 2:
				temp, _ := strconv.ParseFloat(val, 64)
145
				track.Sp0 = temp
M
monomania 已提交
146 147
			case 3:
				temp, _ := strconv.ParseFloat(val, 64)
148
				track.Payout = temp
M
monomania 已提交
149
			case 4:
S
shi.zeyuan 已提交
150
				selection.Children().Each(func(iii int, selection *goquery.Selection) {
M
monomania 已提交
151 152 153 154
					val := selection.Text()
					switch iii {
					case 0:
						temp, _ := strconv.ParseFloat(val, 64)
155
						track.Kelly3 = temp
M
monomania 已提交
156 157
					case 1:
						temp, _ := strconv.ParseFloat(val, 64)
158
						track.Kelly1 = temp
M
monomania 已提交
159 160
					case 2:
						temp, _ := strconv.ParseFloat(val, 64)
161
						track.Kelly0 = temp
M
monomania 已提交
162 163 164 165 166
					}
				})
			case 5:
				var month_day string
				var hour_minute string
S
shi.zeyuan 已提交
167
				selection.Children().Each(func(iii int, selection *goquery.Selection) {
M
monomania 已提交
168 169 170 171 172 173 174 175
					val := selection.Text()
					switch iii {
					case 0:
						month_day = val
					case 1:
						hour_minute = val
					}
				})
176
				track.OddDate = current_year + "-" + month_day + " " + hour_minute + ":00"
M
monomania 已提交
177 178 179 180
			}
		})
	})

181
	this.track_process(track_list)
M
monomania 已提交
182 183
}

184 185 186
func (this *EuroTrackProcesser) track_process(track_list []*entity3.EuroTrack) {
	track_lsit_len := len(track_list)
	if track_lsit_len < 1 {
M
monomania 已提交
187 188 189
		return
	}

190
	//将历史欧赔入库前,生成最后欧赔数据
191 192 193 194 195 196
	track_last := track_list[0]
	track_head := track_list[(track_lsit_len - 1)]

	last := new(entity3.EuroLast)
	last.MatchId = track_last.MatchId
	last.CompId = track_last.CompId
197
	last_temp_id, last_exists := this.EuroLastService.Exist(last)
198 199 200 201 202 203 204 205
	last.Sp3 = track_head.Sp3
	last.Sp1 = track_head.Sp1
	last.Sp0 = track_head.Sp0
	last.Ep3 = track_last.Sp3
	last.Ep1 = track_last.Sp1
	last.Ep0 = track_last.Sp0

	if last_exists {
206
		last.Id = last_temp_id
207
		this.EuroLastService.Modify(last)
208
	} else {
209 210 211 212 213
		this.EuroLastService.Save(last)
	}

	his := new(entity3.EuroHis)
	his.EuroLast = *last
214
	his_temp_id, his_exists := this.EuroHisService.Exist(his)
215
	if his_exists {
216
		his.Id = his_temp_id
217 218 219
		this.EuroHisService.Modify(his)
	} else {
		this.EuroHisService.Save(his)
M
monomania 已提交
220 221 222
	}

	//将历史赔率入库
223 224
	track_list_slice := make([]interface{}, 0)
	for _, v := range track_list {
225
		_, exists := this.EuroTrackService.Exist(v)
M
monomania 已提交
226
		if !exists {
227
			track_list_slice = append(track_list_slice, v)
M
monomania 已提交
228 229
		}
	}
230
	this.EuroTrackService.SaveList(track_list_slice)
M
monomania 已提交
231 232
}

233
func (this *EuroTrackProcesser) Finish() {
234
	base.Log.Info("欧赔历史抓取解析完成 \r\n")
M
monomania 已提交
235 236

}