EuroTrackProcesser.go 5.7 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
	//博彩公司对应的win007id
27 28
	CompWin007Ids []string
	MatchLastList []*pojo.MatchLast
M
monomania 已提交
29

30
	Win007idMatchidMap map[string]string
M
monomania 已提交
31 32
}

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

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

M
monomania 已提交
44 45 46 47 48 49 50 51
func (this *EuroTrackProcesser) Startup() {
	for i, v := range this.MatchLastList {

		var processer *EuroTrackProcesser
		if i%10000 == 0 { //10000个比赛一个spider,一个赛季大概有30万场比赛,最多30条线程
			processer = GetEuroTrackProcesser()
		}
		newSpider := spider.NewSpider(processer, "EuroTrackProcesser"+strconv.Itoa(i))
M
monomania 已提交
52

M
monomania 已提交
53 54
		temp_flag := v.Ext[win007.MODULE_FLAG]
		bytes, _ := json.Marshal(temp_flag)
55 56 57
		matchExt := new(pojo.MatchExt)
		json.Unmarshal(bytes, matchExt)
		win007_id := matchExt.Sid
M
monomania 已提交
58
		processer.Win007idMatchidMap[win007_id] = v.Id
M
monomania 已提交
59

60 61
		base_url := strings.Replace(win007.WIN007_EUROODD_BET_URL_PATTERN, "${scheid}", win007_id, 1)
		for _, v := range this.CompWin007Ids {
62
			url := strings.Replace(base_url, "${cId}", v, 1)
M
monomania 已提交
63 64
			newSpider = newSpider.AddUrl(url, "html")
		}
M
monomania 已提交
65 66 67 68 69

		newSpider.SetDownloader(down.NewMWin007Downloader())
		newSpider = newSpider.AddPipeline(pipeline.NewPipelineConsole())
		newSpider.SetSleepTime("rand", 1000, 20000)
		newSpider.SetThreadnum(1).Run()
M
monomania 已提交
70
	}
M
monomania 已提交
71

M
monomania 已提交
72 73
}

74
func (this *EuroTrackProcesser) findParamVal(url string, paramName string) string {
M
monomania 已提交
75 76 77 78 79 80 81 82 83 84
	paramUrl := strings.Split(url, "?")[1]
	paramArr := strings.Split(paramUrl, "&")
	for _, v := range paramArr {
		if strings.Contains(v, paramName) {
			return strings.Split(v, "=")[1]
		}
	}
	return ""
}

85
func (this *EuroTrackProcesser) Process(p *page.Page) {
M
monomania 已提交
86 87
	request := p.GetRequest()
	if !p.IsSucc() {
88
		base.Log.Error("URL:", request.Url, p.Errormsg())
M
monomania 已提交
89 90 91 92 93 94
		return
	}

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

	win007_matchId := this.findParamVal(request.Url, "scheid")
95
	matchId := this.Win007idMatchidMap[win007_matchId]
M
monomania 已提交
96 97 98

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

99
	var track_list = make([]*entity3.EuroTrack, 0)
M
monomania 已提交
100 101 102 103 104 105 106

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

107 108 109 110
		track := new(entity3.EuroTrack)
		track_list = append(track_list, track)
		track.MatchId = matchId
		track.CompId = win007_betCompId
M
monomania 已提交
111 112 113

		td_list_node := selection.Find(" td ")
		td_list_node.Each(func(ii int, selection *goquery.Selection) {
S
shi.zeyuan 已提交
114
			val := strings.TrimSpace(selection.Text())
M
monomania 已提交
115 116 117 118 119 120 121
			if "" == val {
				return
			}

			switch ii {
			case 0:
				temp, _ := strconv.ParseFloat(val, 64)
122
				track.Sp3 = temp
M
monomania 已提交
123 124
			case 1:
				temp, _ := strconv.ParseFloat(val, 64)
125
				track.Sp1 = temp
M
monomania 已提交
126 127
			case 2:
				temp, _ := strconv.ParseFloat(val, 64)
128
				track.Sp0 = temp
M
monomania 已提交
129 130
			case 3:
				temp, _ := strconv.ParseFloat(val, 64)
131
				track.Payout = temp
M
monomania 已提交
132
			case 4:
S
shi.zeyuan 已提交
133
				selection.Children().Each(func(iii int, selection *goquery.Selection) {
M
monomania 已提交
134 135 136 137
					val := selection.Text()
					switch iii {
					case 0:
						temp, _ := strconv.ParseFloat(val, 64)
138
						track.Kelly3 = temp
M
monomania 已提交
139 140
					case 1:
						temp, _ := strconv.ParseFloat(val, 64)
141
						track.Kelly1 = temp
M
monomania 已提交
142 143
					case 2:
						temp, _ := strconv.ParseFloat(val, 64)
144
						track.Kelly0 = temp
M
monomania 已提交
145 146 147 148 149
					}
				})
			case 5:
				var month_day string
				var hour_minute string
S
shi.zeyuan 已提交
150
				selection.Children().Each(func(iii int, selection *goquery.Selection) {
M
monomania 已提交
151 152 153 154 155 156 157 158
					val := selection.Text()
					switch iii {
					case 0:
						month_day = val
					case 1:
						hour_minute = val
					}
				})
159
				track.OddDate = current_year + "-" + month_day + " " + hour_minute + ":00"
M
monomania 已提交
160 161 162 163
			}
		})
	})

164
	this.track_process(track_list)
M
monomania 已提交
165 166
}

167 168 169
func (this *EuroTrackProcesser) track_process(track_list []*entity3.EuroTrack) {
	track_lsit_len := len(track_list)
	if track_lsit_len < 1 {
M
monomania 已提交
170 171 172
		return
	}

173
	//将历史欧赔入库前,生成最后欧赔数据
174 175 176 177 178 179
	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
180
	last_temp_id, last_exists := this.EuroLastService.Exist(last)
181 182 183 184 185 186 187 188
	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 {
189
		last.Id = last_temp_id
190
		this.EuroLastService.Modify(last)
191
	} else {
192 193 194 195 196
		this.EuroLastService.Save(last)
	}

	his := new(entity3.EuroHis)
	his.EuroLast = *last
197
	his_temp_id, his_exists := this.EuroHisService.Exist(his)
198
	if his_exists {
199
		his.Id = his_temp_id
200 201 202
		this.EuroHisService.Modify(his)
	} else {
		this.EuroHisService.Save(his)
M
monomania 已提交
203 204 205
	}

	//将历史赔率入库
206 207
	track_list_slice := make([]interface{}, 0)
	for _, v := range track_list {
208
		_, exists := this.EuroTrackService.Exist(v)
M
monomania 已提交
209
		if !exists {
210
			track_list_slice = append(track_list_slice, v)
M
monomania 已提交
211 212
		}
	}
213
	this.EuroTrackService.SaveList(track_list_slice)
M
monomania 已提交
214 215
}

216
func (this *EuroTrackProcesser) Finish() {
217
	base.Log.Info("欧赔历史抓取解析完成 \r\n")
M
monomania 已提交
218 219

}