@@ -73,6 +73,14 @@ func (b *Builder) housesWorker(client *resty.Client) error {
7373
7474 })
7575
76+ if len (b .Worlds ) == 0 {
77+ return fmt .Errorf ("no worlds found on tibia.com, possible HTML format change or maintenance" )
78+ }
79+
80+ if len (b .Towns ) == 0 {
81+ return fmt .Errorf ("no towns found on tibia.com, possible HTML format change or maintenance" )
82+ }
83+
7684 // Find the index of Antica in b.Worlds[] or fallback to first index
7785 worldsIndex := func () int {
7886 for i , world := range b .Worlds {
@@ -92,36 +100,35 @@ func (b *Builder) housesWorker(client *resty.Client) error {
92100 return fmt .Errorf ("issue getting %s endpoint. Error: %s" , ApiUrl , err )
93101 }
94102
95- switch res .StatusCode () {
96- case http .StatusOK :
97- // Get byte slice from string.
98- bytes := []byte (res .Body ())
103+ if res .StatusCode () != http .StatusOK {
104+ return fmt .Errorf ("non-200 status retrieving houses for %s. StatusCode: %d" , town , res .StatusCode ())
105+ }
99106
100- var cont SourceHousesOverview
101- err := json .Unmarshal (bytes , & cont )
102- if err != nil {
103- return fmt .Errorf ("issue when unmarshaling data. Town is %s. Err: %s" , town , err )
104- }
107+ // Get byte slice from string.
108+ bytes := []byte (res .Body ())
105109
106- for _ , value := range cont .Houses .HouseList {
107- b .Houses = append (b .Houses , AssetsHouse {
108- Name : value .Name ,
109- HouseID : value .HouseID ,
110- Town : town ,
111- HouseType : "house" ,
112- })
113- }
110+ var cont SourceHousesOverview
111+ err = json .Unmarshal (bytes , & cont )
112+ if err != nil {
113+ return fmt .Errorf ("issue when unmarshaling data. Town is %s. Err: %s" , town , err )
114+ }
114115
115- for _ , value := range cont .Houses .GuildhallList {
116- b .Houses = append (b .Houses , AssetsHouse {
117- Name : value .Name ,
118- HouseID : value .HouseID ,
119- Town : town ,
120- HouseType : "guildhall" ,
121- })
122- }
123- default :
124- log .Printf ("[warn] Issue when retrieving data about houses and guildhalls in %s. StatusCode: %d" , town , res .StatusCode ())
116+ for _ , value := range cont .Houses .HouseList {
117+ b .Houses = append (b .Houses , AssetsHouse {
118+ Name : value .Name ,
119+ HouseID : value .HouseID ,
120+ Town : town ,
121+ HouseType : "house" ,
122+ })
123+ }
124+
125+ for _ , value := range cont .Houses .GuildhallList {
126+ b .Houses = append (b .Houses , AssetsHouse {
127+ Name : value .Name ,
128+ HouseID : value .HouseID ,
129+ Town : town ,
130+ HouseType : "guildhall" ,
131+ })
125132 }
126133
127134 if sleepFlag {
@@ -141,15 +148,24 @@ func (b *Builder) creaturesWorker(client *resty.Client) error {
141148 const raceEndpointIndexer = "&race="
142149
143150 var safe []string
151+ var parseErr error
144152
145153 creatures := doc .Find (".BoxContent .Creatures" ).First ()
146154 creatures .Find ("div" ).Each (func (index int , s * goquery.Selection ) {
155+ if parseErr != nil {
156+ return
157+ }
158+
147159 url , exists := s .Find ("a" ).Attr ("href" )
148160 if ! exists {
149161 return
150162 }
151163
152164 raceIndex := strings .Index (url , raceEndpointIndexer )
165+ if raceIndex == - 1 {
166+ parseErr = fmt .Errorf ("unexpected HTML format from tibia.com: creature URL %q missing %q" , url , raceEndpointIndexer )
167+ return
168+ }
153169 endpoint := strings .TrimPrefix (url [raceIndex :], raceEndpointIndexer )
154170 safe = append (safe , endpoint )
155171 pluralName := s .Find ("div" ).First ().Text ()
@@ -207,6 +223,10 @@ func (b *Builder) creaturesWorker(client *resty.Client) error {
207223 }
208224 })
209225
226+ if parseErr != nil {
227+ return parseErr
228+ }
229+
210230 for i , s := range safe {
211231 str := SpaceMap (b .Creatures [i ].Name )
212232 _ , isSpecial := specialCreaturesCases [s ]
@@ -225,16 +245,30 @@ func (b *Builder) spellsWorker(client *resty.Client) error {
225245 return fmt .Errorf ("%s, func: spellsWorker" , err )
226246 }
227247
248+ var spellParseErr error
249+
228250 doc .Find (".Table3 table.TableContent tr" ).Each (func (index int , s * goquery.Selection ) {
251+ if spellParseErr != nil {
252+ return
253+ }
254+
229255 if index == 0 {
230256 return
231257 }
232258
233259 s .Find ("td" ).EachWithBreak (func (index int , inner * goquery.Selection ) bool {
234260 if index == 0 {
235261 rawText := inner .Text ()
236- spellName := rawText [0 :strings .Index (rawText , " (" )]
237- formula := rawText [strings .Index (rawText , " (" )+ 2 : strings .Index (rawText , ")" )]
262+
263+ parenOpen := strings .Index (rawText , " (" )
264+ parenClose := strings .Index (rawText , ")" )
265+ if parenOpen == - 1 || parenClose == - 1 {
266+ spellParseErr = fmt .Errorf ("unexpected HTML format from tibia.com: spell text %q missing expected parentheses" , rawText )
267+ return false
268+ }
269+
270+ spellName := rawText [0 :parenOpen ]
271+ formula := rawText [parenOpen + 2 : parenClose ]
238272
239273 var endpoint string
240274 if specialCase , isSpecial := specialSpellsCases [spellName ]; isSpecial {
@@ -256,5 +290,9 @@ func (b *Builder) spellsWorker(client *resty.Client) error {
256290 })
257291 })
258292
293+ if spellParseErr != nil {
294+ return spellParseErr
295+ }
296+
259297 return nil
260298}
0 commit comments