LeagueSeasonProcesser.go 6.8 KB
Newer Older
M
monomania 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
package proc

import (
	"github.com/PuerkitoBio/goquery"
	"github.com/hu17889/go_spider/core/common/page"
	"github.com/hu17889/go_spider/core/pipeline"
	"github.com/hu17889/go_spider/core/spider"
	"strconv"
	"strings"
	"tesou.io/platform/foot-parent/foot-api/common/base"
	"tesou.io/platform/foot-parent/foot-api/module/elem/pojo"
	service2 "tesou.io/platform/foot-parent/foot-core/module/elem/service"
	"tesou.io/platform/foot-parent/foot-spider/module/win007"
	"tesou.io/platform/foot-parent/foot-spider/module/win007/down"
)

type LeagueSeasonProcesser struct {
	service2.LeagueService
	service2.LeagueSeasonService
	service2.LeagueSubService
S
1.xx  
shi.zeyuan 已提交
21 22
	//是否是单线程
	SingleThread bool
M
monomania 已提交
23 24
	//联赛次级数据
	leagueSeason_list []*pojo.LeagueSeason
25
	leagueSub_list    []*pojo.LeagueSub
M
monomania 已提交
26 27 28
	sUrl_leagueId     map[string]string
}

29
func GetLeagueSeasonProcesser() *LeagueSeasonProcesser {
M
monomania 已提交
30 31 32
	processer := &LeagueSeasonProcesser{}
	processer.Init()
	return processer
M
monomania 已提交
33 34
}

S
1.xxx  
shi.zeyuan 已提交
35
func (this *LeagueSeasonProcesser) Init() {
M
monomania 已提交
36 37 38 39
	//初始化参数值
	this.leagueSeason_list = make([]*pojo.LeagueSeason, 0)
	this.leagueSub_list = make([]*pojo.LeagueSub, 0)
	this.sUrl_leagueId = make(map[string]string)
S
1.xxx  
shi.zeyuan 已提交
40 41
}

M
monomania 已提交
42 43 44 45 46
func (this *LeagueSeasonProcesser) Setup(temp *LeagueSeasonProcesser) {
	//设置参数值
}


S
1.xxx  
shi.zeyuan 已提交
47
func (this *LeagueSeasonProcesser) Startup() {
M
monomania 已提交
48 49 50 51
	//1.获取所有的联赛
	leaguesList := make([]*pojo.League, 0)
	this.LeagueService.FindAll(&leaguesList)
	//2.配置要抓取的路径
M
monomania 已提交
52
	var newSpider *spider.Spider
S
1.xx  
shi.zeyuan 已提交
53 54
	processer := this
	newSpider = spider.NewSpider(processer, "LeagueSeasonProcesser")
M
monomania 已提交
55
	//index := 0
M
monomania 已提交
56
	for i, v := range leaguesList {
M
monomania 已提交
57
		//先不处理杯赛....
58
		if v.Cup {
M
monomania 已提交
59 60 61 62 63 64
			continue
		}
		//index++
		//if index > 10{
		//	break
		//}
M
monomania 已提交
65
		if i % 10 == 0 {//10个联赛一个spider,总数1000多个联赛,最多100spider
M
monomania 已提交
66
			processer = GetLeagueSeasonProcesser()
M
monomania 已提交
67
			processer.Setup(this)
M
monomania 已提交
68
			newSpider = spider.NewSpider(processer, "LeagueSeasonProcesser"+strconv.Itoa(i))
M
monomania 已提交
69 70
		}

M
monomania 已提交
71 72 73 74 75 76 77 78 79 80
		url := win007.WIN007_MATCH_HIS_PATTERN
		if v.SeasonCross {
			url = strings.Replace(url, "${season}", "2018-2019", 1)
		} else {
			url = strings.Replace(url, "${season}", "2019", 1)
		}
		url = strings.Replace(url, "${leagueId}", v.Id, 1)
		url = strings.Replace(url, "${subId}", "0", 1)
		url = strings.Replace(url, "${round}", "1", 1)

M
monomania 已提交
81
		processer.sUrl_leagueId[url] = v.Id
M
monomania 已提交
82
		newSpider = newSpider.AddUrl(url, "html")
M
monomania 已提交
83 84 85
		if i % 10 == 0 {//10个联赛一个spider,总数1000多个联赛,最多100spider
			newSpider.SetDownloader(down.NewMWin007Downloader())
			newSpider = newSpider.AddPipeline(pipeline.NewPipelineConsole())
M
monomania 已提交
86
			newSpider.SetSleepTime("rand", win007.SLEEP_RAND_S, win007.SLEEP_RAND_E)
87
			newSpider.SetThreadnum(10).Run()
M
monomania 已提交
88
		}
M
monomania 已提交
89 90
	}

M
monomania 已提交
91 92
	newSpider.SetDownloader(down.NewMWin007Downloader())
	newSpider = newSpider.AddPipeline(pipeline.NewPipelineConsole())
M
monomania 已提交
93
	newSpider.SetSleepTime("rand", win007.SLEEP_RAND_S, win007.SLEEP_RAND_E)
M
monomania 已提交
94 95
	newSpider.SetThreadnum(1).Run()

M
monomania 已提交
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
}

func (this *LeagueSeasonProcesser) Process(p *page.Page) {
	request := p.GetRequest()
	if !p.IsSucc() {
		base.Log.Error("URL:,", request.Url, p.Errormsg())
		return
	}

	rawText := p.GetBodyStr()
	if rawText == "" {
		base.Log.Error("rawText:为空.url:", request.Url)
		return
	}
	//1.处理season
S
1.xxx  
shi.zeyuan 已提交
111
	leagueId := this.sUrl_leagueId[request.Url]
M
monomania 已提交
112
	htmlParser := p.GetHtmlParser()
M
monomania 已提交
113
	this.season_process(htmlParser, leagueId, request.Url)
114 115 116

}

M
monomania 已提交
117
func (this *LeagueSeasonProcesser) season_process(htmlParser *goquery.Document, leagueId string, url string) {
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
	//获取赛季开始的月份
	var beginMonth int
	if strings.Contains(url, "-") {
		htmlParser.Find("table[id='mainTable'] tr[onclick]").Each(func(i int, selection *goquery.Selection) {
			temp_id, exist := selection.Attr("onclick")
			if !exist {
				return
			}
			temp_id = strings.Replace(temp_id, "ToAnaly(", "", 1)
			temp_id = strings.Replace(temp_id, ",-1)", "", 1)
			temp_id = strings.TrimSpace(temp_id)

			val_arr := make([]string, 0)
			selection.Find("td").Each(func(i int, selection *goquery.Selection) {
				val := selection.Text()
				val_arr = append(val_arr, strings.TrimSpace(val))
			})

			if len(val_arr) != 5 {
				return
			}

			index := 0
			//比赛时间
			temp_matchDate := val_arr[index]
S
shi.zeyuan 已提交
143
			month, _ := strconv.Atoi(temp_matchDate[:2])
144 145 146 147
			beginMonth = month
		})
	}

M
monomania 已提交
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
	htmlParser.Find("select[id='selSeason'] option").Each(func(i int, selection *goquery.Selection) {
		temp_maxRound := 1
		temp_season := strings.TrimSpace(selection.Text())

		//2.处理sub
		htmlParser.Find("select[id='selSubSclass'] option").Each(func(i int, selection *goquery.Selection) {
			temp_subName := strings.TrimSpace(selection.Text())
			if len(temp_subName) <= 0 {
				return
			}
			val, exist := selection.Attr("value")
			if !exist {
				return
			}
			base.Log.Info("联赛Id:", leagueId, ",赛季:", temp_season, ",次级名称:", temp_subName, ",次级Id:", val)
			leagueSub := new(pojo.LeagueSub)
			leagueSub.LeagueId = leagueId
			leagueSub.Season = temp_season
166
			leagueSub.BeginMonth = beginMonth
M
monomania 已提交
167 168 169 170
			leagueSub.Round = temp_maxRound
			leagueSub.SubId = val
			leagueSub.SubName = temp_subName
			this.leagueSub_list = append(this.leagueSub_list, leagueSub)
171

M
monomania 已提交
172 173 174
		})
		//3.处理round
		htmlParser.Find("select[id='selRound'] option").Each(func(i int, selection *goquery.Selection) {
M
monomania 已提交
175 176
			temp_round_str, exist := selection.Attr("value")
			if !exist {
M
monomania 已提交
177 178
				return;
			}
M
monomania 已提交
179 180 181 182 183 184 185 186 187 188 189 190
			if len(temp_round_str) <= 0 {
				return
			}
			temp_round, _ := strconv.Atoi(temp_round_str)
			if temp_round > temp_maxRound {
				temp_maxRound = temp_round
			}
		})

		leagueSeason := new(pojo.LeagueSeason)
		leagueSeason.LeagueId = leagueId
		leagueSeason.Season = temp_season
191
		leagueSeason.BeginMonth = beginMonth
M
monomania 已提交
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
		leagueSeason.Round = temp_maxRound
		this.leagueSeason_list = append(this.leagueSeason_list, leagueSeason)
	})
}

func (this *LeagueSeasonProcesser) Finish() {
	base.Log.Info("联赛次级抓取解析完成,执行入库 \r\n")

	leagueSeason_list_slice := make([]interface{}, 0)
	leagueSeason_modify_list_slice := make([]interface{}, 0)
	for _, v := range this.leagueSeason_list {
		if nil == v {
			continue
		}
		id, exist := this.LeagueSeasonService.Exist(v)
		if exist {
			v.Id = id
			leagueSeason_modify_list_slice = append(leagueSeason_modify_list_slice, v)
			continue
		}
		leagueSeason_list_slice = append(leagueSeason_list_slice, v)
	}

	this.LeagueSeasonService.SaveList(leagueSeason_list_slice)
	this.LeagueSeasonService.ModifyList(leagueSeason_modify_list_slice)

	leagueSub_list_slice := make([]interface{}, 0)
	leagueSub_modify_list_slice := make([]interface{}, 0)
	for _, v := range this.leagueSub_list {
		if nil == v {
			continue
		}
		id, exist := this.LeagueSubService.Exist(v)
		if exist {
			v.Id = id
			leagueSub_modify_list_slice = append(leagueSub_modify_list_slice, v)
			continue
		}
		leagueSub_list_slice = append(leagueSub_list_slice, v)
	}

	this.LeagueSubService.SaveList(leagueSub_list_slice)
	this.LeagueSubService.ModifyList(leagueSub_modify_list_slice)

}