Created
February 4, 2019 01:48
-
-
Save arxdsilva/6cc4926bce3fef3f6c60dcba13a48e79 to your computer and use it in GitHub Desktop.
Tibia's house rental prices info
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bufio" | |
"fmt" | |
"net/http" | |
"os" | |
"strconv" | |
"strings" | |
) | |
var cities = []string{ | |
"Ab'Dendriel", "Ankrahmun", "Carlin", | |
"Darashia", "Edron", "Farmine", | |
"Gray+Beach", "Kazordoon", "Liberty+Bay", | |
"Port+Hope", "Rathleton", "Svargrond", | |
"Thais", "Venore", "Yalahar", | |
} | |
type cityInfo struct { | |
City string | |
TotalRent int | |
HousesNum int | |
RentPerHouse int | |
} | |
func main() { | |
wd, _ := os.Getwd() | |
f, _ := os.Create(wd + "/cities.csv") | |
defer f.Close() | |
f.Write([]byte("City,Houses Number,Total Rent,Rent per house")) | |
for _, c := range cities { | |
i, err := getInfos(c) | |
if err != nil { | |
return | |
} | |
infoString := fmt.Sprintf("\n%v,%v,%v,%v", i.City, i.HousesNum, i.TotalRent, i.RentPerHouse) | |
f.Write([]byte(infoString)) | |
} | |
} | |
func getInfos(c string) (info cityInfo, err error) { | |
url := fmt.Sprintf("https://www.tibia.com/community/?subtopic=houses&world=Antica&town=%s&type=houses&order=size", c) | |
resp, err := http.Post(url, "text/html", nil) | |
if err != nil { | |
fmt.Println(err) | |
return | |
} | |
fmt.Println(resp.Status) | |
var sum, totalHouses int | |
scanner := bufio.NewScanner(resp.Body) | |
for scanner.Scan() { | |
text := scanner.Text() | |
if strings.Contains(text, "rented") && strings.Contains(text, " ") { | |
r, _ := sanitize(text) | |
rentSplit := strings.Split(r[3], "k") | |
rentInt, _ := strconv.Atoi(rentSplit[0]) | |
sum += rentInt | |
totalHouses++ | |
} | |
} | |
info = cityInfo{ | |
TotalRent: sum, | |
HousesNum: totalHouses, | |
RentPerHouse: sum / totalHouses, | |
City: c, | |
} | |
return | |
} | |
func sanitize(t string) (aunctioneds []string, err error) { | |
splt := strings.Split(t, "NOBR>") | |
aunctioneds = []string{splt[1], splt[3], splt[5], splt[7], splt[9]} | |
for i := range aunctioneds { | |
aunctioneds[i] = strings.Replace(aunctioneds[i], " ", "", -1) | |
aunctioneds[i] = strings.Replace(aunctioneds[i], "</", "", -1) | |
aunctioneds[i] = strings.Replace(aunctioneds[i], ";", " ", -1) | |
} | |
// fmt.Println(aunctioneds) | |
return | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment