11package main
22
33import (
4- "fmt"
54 "net/http"
6- "regexp"
75 "strings"
86
9- "github.com/PuerkitoBio/goquery "
7+ "github.com/TibiaData/tibiadata-api-go/src/validation "
108)
119
1210// Child of BoostableBoss (used for list of boostable bosses and boosted boss section)
@@ -28,95 +26,119 @@ type BoostableBossesOverviewResponse struct {
2826 Information Information `json:"information"`
2927}
3028
31- var (
32- BoostedBossNameRegex = regexp .MustCompile (`<b>(.*)</b>` )
33- BoostedBossImageRegex = regexp .MustCompile (`<img[^>]+\bsrc=["']([^"']+)["']` )
34- BoostableBossInformationRegex = regexp .MustCompile (`<img src="(.*)" border.*div>(.*)<\/div>` )
35- )
29+ func TibiaBoostableBossesOverviewImpl (BoxContentHTML string ) (BoostableBossesOverviewResponse , error ) {
30+ const (
31+ bodyIndexer = `<body`
32+ endBodyIndexer = `</body>`
3633
37- func TibiaBoostableBossesOverviewImpl (BoxContentHTML string ) (* BoostableBossesOverviewResponse , error ) {
38- // Creating empty vars
39- var (
40- BoostedBossName , BoostedBossImage string
41- )
42- // Loading HTML data into ReaderHTML for goquery with NewReader
43- ReaderHTML , err := goquery .NewDocumentFromReader (strings .NewReader (BoxContentHTML ))
44- if err != nil {
45- return nil , fmt .Errorf ("[error] TibiaBoostableBossesOverviewImpl failed at goquery.NewDocumentFromReader, err: %s" , err )
46- }
34+ todayChecker = `Today's boosted boss: `
35+ bossesChecker = `<div class="CaptionContainer">`
4736
48- // Getting data from div.InnerTableContainer and then first p
49- InnerTableContainerTMPB , err := ReaderHTML .Find (".InnerTableContainer p" ).First ().Html ()
50- if err != nil {
51- return nil , fmt .Errorf ("[error] TibiaBoostableBossesOverviewImpl failed at ReaderHTML.Find, error: %s" , err )
52- }
37+ todayBossIndexer = `title="` + todayChecker
38+ endTodayBossIndexer = `" src="`
5339
54- // Regex to get data for name for boosted boss
55- subma1b := BoostedBossNameRegex . FindAllStringSubmatch ( InnerTableContainerTMPB , - 1 )
40+ todayBossImgIndexer = `https://static.tibia.com/images/global/header/monsters/`
41+ endTodayBossImgIndexer = `" onClick="`
5642
57- if len (subma1b ) > 0 {
58- // Settings vars for usage in JSONData
59- BoostedBossName = subma1b [0 ][1 ]
60- }
43+ bossesImgIndexer = `https://static.tibia.com/images/library/`
44+ endBossesImgIndexer = `"`
6145
62- // Regex to get image of boosted boss
63- subma2b := BoostedBossImageRegex .FindAllStringSubmatch (InnerTableContainerTMPB , - 1 )
46+ bossesNameIndexer = `border=0 /> <div>`
47+ endBossesNameIndexer = `</div>`
48+ )
6449
65- if len (subma2b ) > 0 {
66- // Settings vars for usage in JSONData
67- BoostedBossImage = subma2b [0 ][1 ]
68- }
50+ bodyIdx := strings .Index (
51+ BoxContentHTML , bodyIndexer ,
52+ )
53+ endBodyIdx := strings .Index (
54+ BoxContentHTML [bodyIdx :], endBodyIndexer ,
55+ ) + bodyIdx + len (endBodyIndexer )
6956
70- // Creating empty BoostableBossesData var
71- var BoostableBossesData []OverviewBoostableBoss
57+ data := BoxContentHTML [bodyIdx :endBodyIdx ]
7258
73- var insideError error
59+ var (
60+ started bool
61+ //scanner = bufio.NewScanner(strings.NewReader(data))
7462
75- // Running query over each div
76- ReaderHTML . Find ( ".BoxContent div div" ). EachWithBreak ( func ( index int , s * goquery. Selection ) bool {
63+ boostedBossName string
64+ boostedBossImg string
7765
78- // Storing HTML into BoostableBossDivHTML
79- BoostableBossDivHTML , err := s .Html ()
80- if err != nil {
81- insideError = fmt .Errorf ("[error] TibiaBoostableBossesOverviewImpl failed at BoostableBossDivHTML, err := s.Html(), err: %s" , err )
82- return false
66+ bosses = make ([]OverviewBoostableBoss , 0 , validation .AmountOfBoostableBosses )
67+ )
68+ //for scanner.Scan() {
69+ // cur := scanner.Text()
70+ split := strings .Split (data , "\n " )
71+ for _ , cur := range split {
72+ isTodaysLine := strings .Contains (cur , todayChecker ) && ! started
73+ isBossesLine := strings .Contains (cur , bossesChecker )
74+
75+ if ! isTodaysLine && ! isBossesLine {
76+ continue
8377 }
8478
85- // Regex to get data for name, race and img src param for creature
86- subma1 := BoostableBossInformationRegex . FindAllStringSubmatch ( BoostableBossDivHTML , - 1 )
79+ if isTodaysLine {
80+ started = true
8781
88- // check if regex return length is over 0 and the match of name is over 1
89- if len (subma1 ) > 0 && len (subma1 [0 ][2 ]) > 1 {
90- // Adding bool to indicate features in boostable_boss_list
91- FeaturedRace := false
92- if subma1 [0 ][2 ] == BoostedBossName {
93- FeaturedRace = true
94- }
82+ todayBossIdx := strings .Index (
83+ cur , todayBossIndexer ,
84+ ) + len (todayBossIndexer )
85+ endTodayBossIdx := strings .Index (
86+ cur [todayBossIdx :], endTodayBossIndexer ,
87+ ) + todayBossIdx
9588
96- // Creating data block to return
97- BoostableBossesData = append (BoostableBossesData , OverviewBoostableBoss {
98- Name : TibiaDataSanitizeEscapedString (subma1 [0 ][2 ]),
99- ImageURL : subma1 [0 ][1 ],
100- Featured : FeaturedRace ,
101- })
89+ boostedBossName = TibiaDataSanitizeEscapedString (
90+ cur [todayBossIdx :endTodayBossIdx ],
91+ )
92+
93+ todayBossImgIdx := strings .Index (
94+ cur [todayBossIdx :], todayBossImgIndexer ,
95+ ) + todayBossIdx
96+ endTodayBossImgIdx := strings .Index (
97+ cur [todayBossImgIdx :], endTodayBossImgIndexer ,
98+ ) + todayBossImgIdx
99+
100+ boostedBossImg = cur [todayBossImgIdx :endTodayBossImgIdx ]
102101 }
103102
104- return true
105- })
103+ if isBossesLine {
104+ for idx := strings .Index (cur , bossesImgIndexer ); idx != - 1 ; idx = strings .Index (cur , bossesImgIndexer ) {
105+ imgIdx := strings .Index (
106+ cur , bossesImgIndexer ,
107+ )
108+ endImgIdx := strings .Index (
109+ cur [imgIdx :], endBossesImgIndexer ,
110+ ) + imgIdx
111+ img := cur [imgIdx :endImgIdx ]
112+
113+ nameIdx := strings .Index (
114+ cur , bossesNameIndexer ,
115+ ) + len (bossesNameIndexer )
116+ endNameIdx := strings .Index (
117+ cur [nameIdx :], endBossesNameIndexer ,
118+ ) + nameIdx
119+ name := TibiaDataSanitizeEscapedString (cur [nameIdx :endNameIdx ])
120+
121+ bosses = append (bosses , OverviewBoostableBoss {
122+ Name : name ,
123+ ImageURL : img ,
124+ Featured : name == boostedBossName ,
125+ })
126+
127+ cur = cur [endNameIdx - 1 :]
128+ }
106129
107- if insideError != nil {
108- return nil , insideError
130+ break
131+ }
109132 }
110133
111- // Build the data-blob
112- return & BoostableBossesOverviewResponse {
134+ return BoostableBossesOverviewResponse {
113135 BoostableBossesContainer {
114136 Boosted : OverviewBoostableBoss {
115- Name : TibiaDataSanitizeEscapedString ( BoostedBossName ) ,
116- ImageURL : BoostedBossImage ,
137+ Name : boostedBossName ,
138+ ImageURL : boostedBossImg ,
117139 Featured : true ,
118140 },
119- BoostableBosses : BoostableBossesData ,
141+ BoostableBosses : bosses ,
120142 },
121143 Information {
122144 APIDetails : TibiaDataAPIDetails ,
0 commit comments