-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathTibiaBoostableBossesOverview.go
160 lines (128 loc) · 4.22 KB
/
TibiaBoostableBossesOverview.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package main
import (
"errors"
"net/http"
"strings"
"github.com/TibiaData/tibiadata-api-go/src/validation"
)
// Child of BoostableBoss (used for list of boostable bosses and boosted boss section)
type OverviewBoostableBoss struct {
Name string `json:"name"` // The name of the boss.
ImageURL string `json:"image_url"` // The URL to this boss's image.
Featured bool `json:"featured"` // Whether it is featured of not.
}
// Child of JSONData
type BoostableBossesContainer struct {
Boosted OverviewBoostableBoss `json:"boosted"` // The current boosted boss.
BoostableBosses []OverviewBoostableBoss `json:"boostable_boss_list"` // The list of boostable bosses.
}
// The base includes two levels: BoostableBosses and Information
type BoostableBossesOverviewResponse struct {
BoostableBosses BoostableBossesContainer `json:"boostable_bosses"`
Information Information `json:"information"`
}
func TibiaBoostableBossesOverviewImpl(BoxContentHTML string, url string) (BoostableBossesOverviewResponse, error) {
const (
bodyIndexer = `<body`
endBodyIndexer = `</body>`
todayChecker = `Today's boosted boss: `
bossesChecker = `<div class="CaptionContainer" >`
todayBossIndexer = `title="` + todayChecker
endTodayBossIndexer = `" src="`
todayBossImgIndexer = `https://static.tibia.com/images/global/header/monsters/`
endTodayBossImgIndexer = `" onClick="`
bossesImgIndexer = `https://static.tibia.com/images/library/`
endBossesImgIndexer = `"`
bossesNameIndexer = `border=0 /> <div>`
endBossesNameIndexer = `</div>`
)
bodyIdx := strings.Index(
BoxContentHTML, bodyIndexer,
)
if bodyIdx == -1 {
return BoostableBossesOverviewResponse{}, errors.New("[error] body passd to TibiaBoostableBossesOverviewImpl is not valid")
}
endBodyIdx := strings.Index(
BoxContentHTML[bodyIdx:], endBodyIndexer,
) + bodyIdx + len(endBodyIndexer)
if endBodyIdx == -1 {
return BoostableBossesOverviewResponse{}, errors.New("[error] body passd to TibiaBoostableBossesOverviewImpl is not valid")
}
data := BoxContentHTML[bodyIdx:endBodyIdx]
var (
started bool
boostedBossName string
boostedBossImg string
bosses = make([]OverviewBoostableBoss, 0, validation.AmountOfBoostableBosses)
)
split := strings.Split(data, "\n")
for _, cur := range split {
isTodaysLine := strings.Contains(cur, todayChecker) && !started
isBossesLine := strings.Contains(cur, bossesChecker)
if !isTodaysLine && !isBossesLine {
continue
}
if isTodaysLine {
started = true
todayBossIdx := strings.Index(
cur, todayBossIndexer,
) + len(todayBossIndexer)
endTodayBossIdx := strings.Index(
cur[todayBossIdx:], endTodayBossIndexer,
) + todayBossIdx
boostedBossName = TibiaDataSanitizeEscapedString(
cur[todayBossIdx:endTodayBossIdx],
)
todayBossImgIdx := strings.Index(
cur[todayBossIdx:], todayBossImgIndexer,
) + todayBossIdx
endTodayBossImgIdx := strings.Index(
cur[todayBossImgIdx:], endTodayBossImgIndexer,
) + todayBossImgIdx
boostedBossImg = cur[todayBossImgIdx:endTodayBossImgIdx]
}
if isBossesLine {
for idx := strings.Index(cur, bossesImgIndexer); idx != -1; idx = strings.Index(cur, bossesImgIndexer) {
imgIdx := strings.Index(
cur, bossesImgIndexer,
)
endImgIdx := strings.Index(
cur[imgIdx:], endBossesImgIndexer,
) + imgIdx
img := cur[imgIdx:endImgIdx]
nameIdx := strings.Index(
cur, bossesNameIndexer,
) + len(bossesNameIndexer)
endNameIdx := strings.Index(
cur[nameIdx:], endBossesNameIndexer,
) + nameIdx
name := TibiaDataSanitizeEscapedString(cur[nameIdx:endNameIdx])
bosses = append(bosses, OverviewBoostableBoss{
Name: name,
ImageURL: img,
Featured: name == boostedBossName,
})
cur = cur[endNameIdx-1:]
}
break
}
}
return BoostableBossesOverviewResponse{
BoostableBossesContainer{
Boosted: OverviewBoostableBoss{
Name: boostedBossName,
ImageURL: boostedBossImg,
Featured: true,
},
BoostableBosses: bosses,
},
Information{
APIDetails: TibiaDataAPIDetails,
Timestamp: TibiaDataDatetime(""),
TibiaURLs: []string{url},
Status: Status{
HTTPCode: http.StatusOK,
},
},
}, nil
}