|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "net/http" |
| 6 | + "regexp" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/PuerkitoBio/goquery" |
| 10 | +) |
| 11 | + |
| 12 | +type SectionBoardLastPost struct { |
| 13 | + ID int `json:"id"` |
| 14 | + PostedAt string `json:"posted_at"` |
| 15 | + CharacterName string `json:"character_name"` |
| 16 | +} |
| 17 | + |
| 18 | +type SectionBoard struct { |
| 19 | + ID int `json:"id"` |
| 20 | + Name string `json:"name"` |
| 21 | + Description string `json:"description"` |
| 22 | + Posts int `json:"posts"` |
| 23 | + Threads int `json:"threads"` |
| 24 | + LastPost SectionBoardLastPost `json:"last_post"` |
| 25 | +} |
| 26 | + |
| 27 | +type ForumSectionResponse struct { |
| 28 | + Boards []SectionBoard `json:"boards"` |
| 29 | + Information Information `json:"information"` |
| 30 | +} |
| 31 | + |
| 32 | +var ( |
| 33 | + boardInformationRegex = regexp.MustCompile(`.*boardid=(.*)">(.*)<\/a><br\/><font class="ff_info">(.*)<\/font><\/td><td class="TextRight">(.*)<\/td><td class="TextRight">(.*)<\/td><td><span class="LastPostInfo">`) |
| 34 | + lastPostIdRegex = regexp.MustCompile(`.*postid=(.*)#post`) |
| 35 | + lastPostPostedAtRegex = regexp.MustCompile(`.*height="9"\/><\/a>(.*)<\/span><span><font class="ff_info">`) |
| 36 | + lastPostCharacterNameRegex = regexp.MustCompile(`.*subtopic=characters&name=.*\">(.*)<\/a><\/span>`) |
| 37 | +) |
| 38 | + |
| 39 | +// TibiaForumSectionImpl func |
| 40 | +func TibiaForumSectionImpl(BoxContentHTML string) (*ForumSectionResponse, error) { |
| 41 | + // Loading HTML data into ReaderHTML for goquery with NewReader |
| 42 | + ReaderHTML, err := goquery.NewDocumentFromReader(strings.NewReader(BoxContentHTML)) |
| 43 | + if err != nil { |
| 44 | + return nil, fmt.Errorf("[error] TibiaForumSectionImpl failed at goquery.NewDocumentFromReader, err: %s", err) |
| 45 | + } |
| 46 | + |
| 47 | + var ( |
| 48 | + BoardsData []SectionBoard |
| 49 | + LastPostId int |
| 50 | + LastPostPostedAt, LastPostCharacterName string |
| 51 | + |
| 52 | + insideError error |
| 53 | + ) |
| 54 | + |
| 55 | + // Running query over each div |
| 56 | + ReaderHTML.Find(".TableContentContainer .TableContent tbody tr:not(.LabelH)").EachWithBreak(func(index int, s *goquery.Selection) bool { |
| 57 | + // Storing HTML into CreatureDivHTML |
| 58 | + BoardsDivHTML, err := s.Html() |
| 59 | + if err != nil { |
| 60 | + insideError = fmt.Errorf("[error] TibiaForumSectionImpl failed at BoardsDivHTML, err := s.Html(), err: %s", err) |
| 61 | + return false |
| 62 | + } |
| 63 | + |
| 64 | + subma1 := boardInformationRegex.FindAllStringSubmatch(BoardsDivHTML, -1) |
| 65 | + if len(subma1) == 0 { |
| 66 | + return false |
| 67 | + } |
| 68 | + |
| 69 | + subma2 := lastPostIdRegex.FindAllStringSubmatch(BoardsDivHTML, -1) |
| 70 | + if len(subma2) > 0 { |
| 71 | + LastPostId = TibiaDataStringToInteger(subma2[0][1]) |
| 72 | + } |
| 73 | + |
| 74 | + subma3 := lastPostPostedAtRegex.FindAllStringSubmatch(BoardsDivHTML, -1) |
| 75 | + if len(subma3) > 0 { |
| 76 | + LastPostPostedAt = TibiaDataDatetime(strings.Trim(TibiaDataSanitizeStrings(subma3[0][1]), " ")) |
| 77 | + } |
| 78 | + |
| 79 | + subma4 := lastPostCharacterNameRegex.FindAllStringSubmatch(BoardsDivHTML, -1) |
| 80 | + if len(subma4) > 0 { |
| 81 | + LastPostCharacterName = TibiaDataSanitizeStrings(subma4[0][1]) |
| 82 | + } |
| 83 | + |
| 84 | + BoardsData = append(BoardsData, SectionBoard{ |
| 85 | + ID: TibiaDataStringToInteger(subma1[0][1]), |
| 86 | + Name: subma1[0][2], |
| 87 | + Description: subma1[0][3], |
| 88 | + Posts: TibiaDataStringToInteger(subma1[0][4]), |
| 89 | + Threads: TibiaDataStringToInteger(subma1[0][5]), |
| 90 | + LastPost: SectionBoardLastPost{ |
| 91 | + ID: LastPostId, |
| 92 | + PostedAt: LastPostPostedAt, |
| 93 | + CharacterName: LastPostCharacterName, |
| 94 | + }, |
| 95 | + }) |
| 96 | + |
| 97 | + return true |
| 98 | + }) |
| 99 | + |
| 100 | + if insideError != nil { |
| 101 | + return nil, insideError |
| 102 | + } |
| 103 | + |
| 104 | + // |
| 105 | + // Build the data-blob |
| 106 | + return &ForumSectionResponse{ |
| 107 | + Boards: BoardsData, |
| 108 | + Information: Information{ |
| 109 | + APIDetails: TibiaDataAPIDetails, |
| 110 | + Timestamp: TibiaDataDatetime(""), |
| 111 | + Status: Status{ |
| 112 | + HTTPCode: http.StatusOK, |
| 113 | + }, |
| 114 | + }, |
| 115 | + }, nil |
| 116 | +} |
0 commit comments