LeagueSeasonProcesser.go 6.0 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 21 22 23
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

	//联赛次级数据
	leagueSeason_list []*pojo.LeagueSeason
24
	leagueSub_list    []*pojo.LeagueSub
M
monomania 已提交
25 26 27
	sUrl_leagueId     map[string]string
}

28
func GetLeagueSeasonProcesser() *LeagueSeasonProcesser {
M
monomania 已提交
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
	return &LeagueSeasonProcesser{}
}

func (this *LeagueSeasonProcesser) Startup() {
	//初始化参数值
	this.leagueSeason_list = make([]*pojo.LeagueSeason, 0)
	this.leagueSub_list = make([]*pojo.LeagueSub, 0)
	this.sUrl_leagueId = make(map[string]string)

	//1.获取所有的联赛
	leaguesList := make([]*pojo.League, 0)
	this.LeagueService.FindAll(&leaguesList)

	//2.配置要抓取的路径
	newSpider := spider.NewSpider(this, "LeagueSeasonProcesser")
	//index := 0
	for _, v := range leaguesList {
		//先不处理杯赛....
47
		if v.Cup {
M
monomania 已提交
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
			continue
		}
		//index++
		//if index > 10{
		//	break
		//}
		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)

		this.sUrl_leagueId[url] = v.Id
		newSpider = newSpider.AddUrl(url, "html")
	}

	newSpider.SetDownloader(down.NewMWin007Downloader())
	newSpider = newSpider.AddPipeline(pipeline.NewPipelineConsole())
	newSpider.SetSleepTime("rand", 100, 2000)
	newSpider.SetThreadnum(1).Run()
}

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
	htmlParser := p.GetHtmlParser()
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
	this.season_process(htmlParser, request.Url)

}

func (this *LeagueSeasonProcesser) season_process(htmlParser *goquery.Document, url string) {
	leagueId := this.sUrl_leagueId[url]

	//获取赛季开始的月份
	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)
			base.Log.Info("比赛ID为:", 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
			//比赛时间
			index++
			temp_matchDate := val_arr[index]
			month, _ := strconv.Atoi(temp_matchDate[:1])
			beginMonth = month
		})
	}

M
monomania 已提交
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
	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
145
			leagueSub.BeginMonth = beginMonth
M
monomania 已提交
146 147 148 149
			leagueSub.Round = temp_maxRound
			leagueSub.SubId = val
			leagueSub.SubName = temp_subName
			this.leagueSub_list = append(this.leagueSub_list, leagueSub)
150

M
monomania 已提交
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
		})
		//3.处理round
		htmlParser.Find("select[id='selRound'] option").Each(func(i int, selection *goquery.Selection) {
			temp_round_str := strings.TrimSpace(selection.Text())
			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
167
		leagueSeason.BeginMonth = beginMonth
M
monomania 已提交
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
		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)

}