-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathTibiaForumSection.go
More file actions
116 lines (98 loc) · 3.57 KB
/
Copy pathTibiaForumSection.go
File metadata and controls
116 lines (98 loc) · 3.57 KB
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
package main
import (
"fmt"
"net/http"
"regexp"
"strings"
"github.com/PuerkitoBio/goquery"
)
type SectionBoardLastPost struct {
ID int `json:"id"`
PostedAt string `json:"posted_at"`
CharacterName string `json:"character_name"`
}
type SectionBoard struct {
ID int `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
Posts int `json:"posts"`
Threads int `json:"threads"`
LastPost SectionBoardLastPost `json:"last_post"`
}
type ForumSectionResponse struct {
Boards []SectionBoard `json:"boards"`
Information Information `json:"information"`
}
var (
boardInformationRegex = regexp.MustCompile(`.*boardid=(.*)">(.*)<\/a><br\/><font class="ff_info">(.*)<\/font><\/td><td class="TextRight">(.*)<\/td><td class="TextRight">(.*)<\/td><td><span class="LastPostInfo">`)
lastPostIdRegex = regexp.MustCompile(`.*postid=(.*)#post`)
lastPostPostedAtRegex = regexp.MustCompile(`.*height="9"\/><\/a>(.*)<\/span><span><font class="ff_info">`)
lastPostCharacterNameRegex = regexp.MustCompile(`.*subtopic=characters&name=.*\">(.*)<\/a><\/span>`)
)
// TibiaForumSectionImpl func
func TibiaForumSectionImpl(BoxContentHTML string) (*ForumSectionResponse, error) {
// Loading HTML data into ReaderHTML for goquery with NewReader
ReaderHTML, err := goquery.NewDocumentFromReader(strings.NewReader(BoxContentHTML))
if err != nil {
return nil, fmt.Errorf("[error] TibiaForumSectionImpl failed at goquery.NewDocumentFromReader, err: %s", err)
}
var (
BoardsData []SectionBoard
LastPostId int
LastPostPostedAt, LastPostCharacterName string
insideError error
)
// Running query over each div
ReaderHTML.Find(".TableContentContainer .TableContent tbody tr:not(.LabelH)").EachWithBreak(func(index int, s *goquery.Selection) bool {
// Storing HTML into CreatureDivHTML
BoardsDivHTML, err := s.Html()
if err != nil {
insideError = fmt.Errorf("[error] TibiaForumSectionImpl failed at BoardsDivHTML, err := s.Html(), err: %s", err)
return false
}
subma1 := boardInformationRegex.FindAllStringSubmatch(BoardsDivHTML, -1)
if len(subma1) == 0 {
return false
}
subma2 := lastPostIdRegex.FindAllStringSubmatch(BoardsDivHTML, -1)
if len(subma2) > 0 {
LastPostId = TibiaDataStringToInteger(subma2[0][1])
}
subma3 := lastPostPostedAtRegex.FindAllStringSubmatch(BoardsDivHTML, -1)
if len(subma3) > 0 {
LastPostPostedAt = TibiaDataDatetime(strings.Trim(TibiaDataSanitizeStrings(subma3[0][1]), " "))
}
subma4 := lastPostCharacterNameRegex.FindAllStringSubmatch(BoardsDivHTML, -1)
if len(subma4) > 0 {
LastPostCharacterName = TibiaDataSanitizeStrings(subma4[0][1])
}
BoardsData = append(BoardsData, SectionBoard{
ID: TibiaDataStringToInteger(subma1[0][1]),
Name: subma1[0][2],
Description: TibiaDataSanitizeEscapedString(subma1[0][3]),
Posts: TibiaDataStringToInteger(subma1[0][4]),
Threads: TibiaDataStringToInteger(subma1[0][5]),
LastPost: SectionBoardLastPost{
ID: LastPostId,
PostedAt: LastPostPostedAt,
CharacterName: LastPostCharacterName,
},
})
return true
})
if insideError != nil {
return nil, insideError
}
//
// Build the data-blob
return &ForumSectionResponse{
Boards: BoardsData,
Information: Information{
APIDetails: TibiaDataAPIDetails,
Timestamp: TibiaDataDatetime(""),
Status: Status{
HTTPCode: http.StatusOK,
},
},
}, nil
}