Пример #1
0
        /// <summary>
        /// Actual command logic
        /// </summary>
        /// <param name="source">The user who triggered the command.</param>
        /// <param name="channel">The channel the command was triggered in.</param>
        /// <param name="args">The arguments to the command.</param>
        /// <returns></returns>
        protected override CommandResponseHandler execute(User source, string channel, string[] args)
        {
            if (args.Length == 0)
            {
                string[] messageParameters = { "geolocate", "1", args.Length.ToString() };
                return(new CommandResponseHandler(new Message().get("notEnoughParameters", messageParameters)));
            }

            GeolocateResult location = getLocation(IPAddress.Parse(args[0]));

            string[] messageArgs = { location.ToString() };
            return(new CommandResponseHandler(new Message().get("locationMessage", messageArgs)));
        }
Пример #2
0
        /// <summary>
        /// Gets the location of the IP address.
        /// </summary>
        /// <param name="ip">The ip.</param>
        /// <returns></returns>
        public static GeolocateResult getLocation(IPAddress ip)
        {
            Stream          s      = HttpRequest.get("http://api.ipinfodb.com/v2/ip_query.php?key=" + Configuration.singleton()["ipinfodbApiKey"] + "&ip=" + ip + "&timezone=false");
            XmlTextReader   xtr    = new XmlTextReader(s);
            GeolocateResult result = new GeolocateResult();

            while (!xtr.EOF)
            {
                xtr.Read();
                switch (xtr.Name)
                {
                case "Status":
                    result.status = xtr.ReadElementContentAsString();
                    break;

                case "CountryCode":
                    result.countryCode = xtr.ReadElementContentAsString();
                    break;

                case "CountryName":
                    result.country = xtr.ReadElementContentAsString();
                    break;

                case "RegionCode":
                    result.regionCode = xtr.ReadElementContentAsString();
                    break;

                case "RegionName":
                    result.region = xtr.ReadElementContentAsString();
                    break;

                case "City":
                    result.city = xtr.ReadElementContentAsString();
                    break;

                case "ZipPostalCode":
                    result.zipPostalCode = xtr.ReadElementContentAsString();
                    break;

                case "Latitude":
                    result.latitude = xtr.ReadElementContentAsFloat();
                    break;

                case "Longitude":
                    result.longitude = xtr.ReadElementContentAsFloat();
                    break;
                }
            }
            return(result);
        }
Пример #3
0
        /// <summary>
        /// Gets the location of the IP address.
        /// </summary>
        /// <param name="ip">The ip.</param>
        /// <returns></returns>
        public static GeolocateResult getLocation(IPAddress ip)
        {
            Stream s = HttpRequest.get("http://api.ipinfodb.com/v2/ip_query.php?key=" + Configuration.singleton()["ipinfodbApiKey"] + "&ip=" + ip + "&timezone=false");
            XmlTextReader xtr = new XmlTextReader(s);
            GeolocateResult result = new GeolocateResult();

            while (!xtr.EOF)
            {
                xtr.Read();
                switch (xtr.Name)
                {
                    case "Status":
                        result.status = xtr.ReadElementContentAsString();
                        break;
                    case "CountryCode":
                        result.countryCode = xtr.ReadElementContentAsString();
                        break;
                    case "CountryName":
                        result.country = xtr.ReadElementContentAsString();
                        break;
                    case "RegionCode":
                        result.regionCode = xtr.ReadElementContentAsString();
                        break;
                    case "RegionName":
                        result.region = xtr.ReadElementContentAsString();
                        break;
                    case "City":
                        result.city = xtr.ReadElementContentAsString();
                        break;
                    case "ZipPostalCode":
                        result.zipPostalCode = xtr.ReadElementContentAsString();
                        break;
                    case "Latitude":
                        result.latitude = xtr.ReadElementContentAsFloat();
                        break;
                    case "Longitude":
                        result.longitude = xtr.ReadElementContentAsFloat();
                        break;
                }
            }
            return result;
        }
Пример #4
0
        public GeolocateResult GetLocation(IPAddress address)
        {
            if (this.configuration.IpInfoDbApiKey == string.Empty)
            {
                this.logger.Error("API key is empty, please fix this in configuration.");
                return(new GeolocateResult());
            }

            if (address == null)
            {
                throw new ArgumentNullException("address");
            }

            var queryParameters = new NameValueCollection
            {
                { "key", this.configuration.IpInfoDbApiKey },
                { "ip", address.ToString() }
            };

            using (var s = this.webServiceClient.DoApiCall(
                       queryParameters,
                       "https://accounts.wmflabs.org/api.php",
                       this.configuration.UserAgent))
            {
                var xtr = new XmlTextReader(s);

                var result = new GeolocateResult();

                while (!xtr.EOF)
                {
                    xtr.Read();
                    switch (xtr.Name)
                    {
                    case "statusCode":
                        result.Status = xtr.ReadElementContentAsString();
                        break;

                    case "countryCode":
                        result.CountryCode = xtr.ReadElementContentAsString();
                        break;

                    case "countryName":
                        result.Country = xtr.ReadElementContentAsString();
                        break;

                    case "regionName":
                        result.Region = xtr.ReadElementContentAsString();
                        break;

                    case "cityName":
                        result.City = xtr.ReadElementContentAsString();
                        break;

                    case "zipCode":
                        result.ZipPostalCode = xtr.ReadElementContentAsString();
                        break;

                    case "latitude":
                        result.Latitude = xtr.ReadElementContentAsFloat();
                        break;

                    case "longitude":
                        result.Longitude = xtr.ReadElementContentAsFloat();
                        break;
                    }
                }

                return(result);
            }
        }