Exemplo n.º 1
0
        // Get the distance between two airports
        public int GetDistance(string from, string to, DbFace dbface)
        {
            lock (dbface)
            {
                // Try to get the distance from the database
                int? distance = dbface.GetValue<int>(string.Format("select km from distances where origin = '{0}' and destination = '{1}'", from, to));
                if (distance.HasValue)
                    return distance.Value;

                if (from == "NYC")
                    from = "JFK";
                if (from == "LON")
                    from = "LHR";

                // Otherwise, query www.world-airport.codes.com
                CookieContainer cookies = new CookieContainer();
                string output = WebUtilities.GetPage("http://www.world-airport-codes.com/dist/?a1=" + from + "&a2=" + to, ref cookies);

                int kiloi = output.IndexOf("kilometres");
                if (kiloi < 0)
                    return 0;   // error!

                string kilostr = output.Substring(kiloi - 13, 13);
                int? value = GetNum(kilostr, true);

                if (!value.HasValue)
                {
                    status.Add(string.Format("Could not find distance from {0} to {1}", from, to));
                    return 0;
                }

                dbface.Execute(string.Format("insert into distances (origin, destination, km) values ('{0}', '{1}', {2})", from, to, value.Value), false);
                return value.Value;
            }
        }