|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "log" |
| 5 | + "regexp" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "github.com/PuerkitoBio/goquery" |
| 9 | +) |
| 10 | + |
| 11 | +// Child of BoostableBoss (used for list of boostable bosses and boosted boss section) |
| 12 | +type OverviewBoostableBoss struct { |
| 13 | + Name string `json:"name"` |
| 14 | + ImageURL string `json:"image_url"` |
| 15 | + Featured bool `json:"featured"` |
| 16 | +} |
| 17 | + |
| 18 | +// Child of JSONData |
| 19 | +type BoostableBossesContainer struct { |
| 20 | + Boosted OverviewBoostableBoss `json:"boosted"` |
| 21 | + BoostableBosses []OverviewBoostableBoss `json:"boostable_boss_list"` |
| 22 | +} |
| 23 | + |
| 24 | +// |
| 25 | +// The base includes two levels: BoostableBosses and Information |
| 26 | +type BoostableBossesOverviewResponse struct { |
| 27 | + BoostableBosses BoostableBossesContainer `json:"boostable_bosses"` |
| 28 | + Information Information `json:"information"` |
| 29 | +} |
| 30 | + |
| 31 | +var ( |
| 32 | + BoostedBossNameRegex = regexp.MustCompile(`<b>(.*)</b>`) |
| 33 | + BoostedBossImageRegex = regexp.MustCompile(`<img[^>]+\bsrc=["']([^"']+)["']`) |
| 34 | + BoostableBossInformationRegex = regexp.MustCompile(`<img src="(.*)" border.*div>(.*)<\/div>`) |
| 35 | +) |
| 36 | + |
| 37 | +func TibiaBoostableBossesOverviewV3Impl(BoxContentHTML string) BoostableBossesOverviewResponse { |
| 38 | + var ( |
| 39 | + BoostedBossName, BoostedBossImage string |
| 40 | + ) |
| 41 | + |
| 42 | + // Loading HTML data into ReaderHTML for goquery with NewReader |
| 43 | + ReaderHTML, err := goquery.NewDocumentFromReader(strings.NewReader(BoxContentHTML)) |
| 44 | + if err != nil { |
| 45 | + log.Fatal(err) |
| 46 | + } |
| 47 | + |
| 48 | + // Getting data from div.InnerTableContainer and then first p |
| 49 | + InnerTableContainerTMPB, err := ReaderHTML.Find(".InnerTableContainer p").First().Html() |
| 50 | + if err != nil { |
| 51 | + log.Fatal(err) |
| 52 | + } |
| 53 | + |
| 54 | + // Regex to get data for name for boosted boss |
| 55 | + subma1b := BoostedBossNameRegex.FindAllStringSubmatch(InnerTableContainerTMPB, -1) |
| 56 | + |
| 57 | + if len(subma1b) > 0 { |
| 58 | + // Settings vars for usage in JSONData |
| 59 | + BoostedBossName = subma1b[0][1] |
| 60 | + } |
| 61 | + |
| 62 | + // Regex to get image of boosted boss |
| 63 | + subma2b := BoostedBossImageRegex.FindAllStringSubmatch(InnerTableContainerTMPB, -1) |
| 64 | + |
| 65 | + if len(subma2b) > 0 { |
| 66 | + // Settings vars for usage in JSONData |
| 67 | + BoostedBossImage = subma2b[0][1] |
| 68 | + } |
| 69 | + |
| 70 | + // Creating empty BoostableBossesData var |
| 71 | + var BoostableBossesData []OverviewBoostableBoss |
| 72 | + |
| 73 | + // Running query over each div |
| 74 | + ReaderHTML.Find(".BoxContent div div").Each(func(index int, s *goquery.Selection) { |
| 75 | + |
| 76 | + // Storing HTML into BoostableBossDivHTML |
| 77 | + BoostableBossDivHTML, err := s.Html() |
| 78 | + if err != nil { |
| 79 | + log.Fatal(err) |
| 80 | + } |
| 81 | + |
| 82 | + // Regex to get data for name, race and img src param for creature |
| 83 | + subma1 := BoostableBossInformationRegex.FindAllStringSubmatch(BoostableBossDivHTML, -1) |
| 84 | + |
| 85 | + // check if regex return length is over 0 and the match of name is over 1 |
| 86 | + if len(subma1) > 0 && len(subma1[0][2]) > 1 { |
| 87 | + // Adding bool to indicate features in boostable_boss_list |
| 88 | + FeaturedRace := false |
| 89 | + if subma1[0][2] == BoostedBossName { |
| 90 | + FeaturedRace = true |
| 91 | + } |
| 92 | + |
| 93 | + // Creating data block to return |
| 94 | + BoostableBossesData = append(BoostableBossesData, OverviewBoostableBoss{ |
| 95 | + Name: TibiaDataSanitizeEscapedString(subma1[0][2]), |
| 96 | + ImageURL: subma1[0][1], |
| 97 | + Featured: FeaturedRace, |
| 98 | + }) |
| 99 | + } |
| 100 | + }) |
| 101 | + |
| 102 | + // Build the data-blob |
| 103 | + return BoostableBossesOverviewResponse{ |
| 104 | + BoostableBossesContainer{ |
| 105 | + Boosted: OverviewBoostableBoss{ |
| 106 | + Name: BoostedBossName, |
| 107 | + ImageURL: BoostedBossImage, |
| 108 | + Featured: true, |
| 109 | + }, |
| 110 | + BoostableBosses: BoostableBossesData, |
| 111 | + }, |
| 112 | + Information{ |
| 113 | + APIVersion: TibiaDataAPIversion, |
| 114 | + Timestamp: TibiaDataDatetimeV3(""), |
| 115 | + }, |
| 116 | + } |
| 117 | +} |
0 commit comments