Tibialyzer.House.GatherInformationOnline C# (CSharp) Method

GatherInformationOnline() public static method

public static GatherInformationOnline ( string world, string city, bool guildhall ) : bool
world string
city string
guildhall bool
return bool
        public static bool GatherInformationOnline(string world, string city, bool guildhall)
        {
            bool found = false;
            List<TibiaObject> houses = StorageManager.getHouseByCity(city, guildhall);
            foreach(TibiaObject obj in houses) {
                House h = obj as House;
                if (h.world == null || !h.world.Equals(world, StringComparison.InvariantCultureIgnoreCase)) {
                    found = true;
                    break;
                }
            }
            if (!found) return true;
            try {
                using (WebClient client = new WebClient()) {
                    Dictionary<int, House> idMap = guildhall ? StorageManager.guildHallIdMap : StorageManager.houseIdMap;
                    string html = client.DownloadString(String.Format("https://secure.tibia.com/community/?subtopic=houses&world={0}&town={1}{2}", world.ToTitle(), city.ToTitle().Replace("'", "%27"), guildhall ? "&type=guildhalls" : ""));

                    foreach (TibiaObject obj in houses) {
                        House h = obj as House;
                        h.world = world;
                    }

                    int index = 0;
                    Regex baseRegex = new Regex("<TR BGCOLOR=#[A-Fa-f0-9]+>");
                    Regex auctionedRegex = new Regex("<TD WIDTH=40%><NOBR>(rented|auctioned[^<]*)</NOBR></TD>");
                    Regex houseidRegex = new Regex("<INPUT TYPE=hidden NAME=houseid VALUE=([0-9]+)>");
                    Regex hoursRegex = new Regex("([0-9]+) gold[;] ([0-9]+) hours left");
                    Regex daysRegex = new Regex("([0-9]+) gold[;] ([0-9]+) days left");
                    while (index < html.Length) {
                        Match m = baseRegex.Match(html, index);
                        if (!m.Success)
                            return true;
                        index = m.Index + m.Length;

                        m = auctionedRegex.Match(html, index);
                        if (!m.Success)
                            continue;
                        index = m.Index + m.Length;

                        string status = Player.RemoveJunk(m.Groups[1].Value);

                        m = houseidRegex.Match(html, index);
                        if (!m.Success)
                            continue;
                        index = m.Index + m.Length;

                        int id = int.Parse(m.Groups[1].Value);

                        if (!idMap.ContainsKey(id))
                            continue;
                        House house = idMap[id];

                        house.world = world;

                        if (status.Equals("rented", StringComparison.InvariantCultureIgnoreCase)) {
                            house.occupied = true;
                            house.hoursleft = 0;
                            house.bid = 0;
                        } else {
                            house.occupied = false;
                            house.hoursleft = -1;
                            house.bid = -1;
                            if (!status.Contains("no bids left", StringComparison.InvariantCultureIgnoreCase)) {
                                bool days = status.Contains("days", StringComparison.InvariantCultureIgnoreCase);
                                Regex re = days ? daysRegex : hoursRegex;
                                m = re.Match(status);
                                if (m.Success) {
                                    int bid = int.Parse(m.Groups[1].Value);
                                    int time = int.Parse(m.Groups[2].Value);
                                    house.bid = bid;
                                    house.hoursleft = days ? time * 24 : time;
                                }
                            }
                        }
                    }

                    return true;
                }
            } catch {
                return false;
            }
        }