Exemplo n.º 1
0
        protected virtual void HandleTimeCommand(CommandMatch cmd, IChannelMessageEventArgs msg)
        {
            string location = ((string)cmd.Arguments[0]).Trim();

            if (location.Length == 0)
            {
                location = Config.DefaultLocation;
            }

            string aliasedLocation;

            if (Config.LocationAliases.TryGetValue(location, out aliasedLocation))
            {
                location = aliasedLocation;
            }

            // obtain location
            var     client = new GeoNamesClient(Config.GeoNames);
            GeoName loc    = client.GetFirstGeoName(location).SyncWait();

            if (loc == null)
            {
                ConnectionManager.SendChannelMessage(msg.Channel, $"{msg.SenderNickname}: GeoNames cannot find that location!");
                return;
            }

            // obtain timezone
            GeoTimeZoneResult geoTimeZoneResult = client.GetTimezone(loc.Latitude, loc.Longitude).SyncWait();

            // find actual date/time using our zone data
            DateTimeZone zone = TimeZoneProvider.GetZoneOrNull(geoTimeZoneResult.TimezoneID);

            if (zone == null)
            {
                ConnectionManager.SendChannelMessage(msg.Channel, $"{msg.SenderNickname}: I don't know the timezone {geoTimeZoneResult.TimezoneID}.");
                return;
            }

            ZonedDateTime time = SystemClock.Instance.GetCurrentInstant().InZone(zone);

            bool lucky = cmd.CommandName.StartsWith("l");

            ConnectionManager.SendChannelMessage(
                msg.Channel,
                lucky
                    ? $"{msg.SenderNickname}: The time there is {time:yyyy-MM-dd HH:mm:ss}."
                    : $"{msg.SenderNickname}: The time in {loc.Name} is {time:yyyy-MM-dd HH:mm:ss}."
                );
        }
Exemplo n.º 2
0
        protected virtual void GetWeatherForLocation(string location, string channel, string nick, bool lookupAlias = true, bool showLocName = true)
        {
            if (lookupAlias)
            {
                string aliasedLocation;
                if (Config.LocationAliases.TryGetValue(location, out aliasedLocation))
                {
                    location = aliasedLocation;
                }
            }

            // try specials first
            var providersWeathers = new List <(IWeatherProvider Provider, string WeatherString)>();

            foreach (IWeatherProvider provider in WeatherProviders)
            {
                // returns null if unsuccessful
                string weatherDescr = provider.GetWeatherDescriptionForSpecial(location);
                providersWeathers.Add((provider, weatherDescr));
            }

            if (providersWeathers.Any(pw => pw.WeatherString != null))
            {
                // some special; skip the geocoding
                foreach (var(provider, weather) in providersWeathers)
                {
                    if (weather == null)
                    {
                        continue;
                    }

                    OutputWeather(null, channel, nick, weather, showLocName);
                }
                return;
            }

            // geocode
            var geoClient = new GeoNamesClient(Config.GeoNames);

            Match   latLonMatch = LatLonRegex.Match(location);
            decimal latitude, longitude;
            string  locName = null;

            if (latLonMatch.Success)
            {
                latitude  = ParseDecimalInv(latLonMatch.Groups["Latitude"].Value);
                longitude = ParseDecimalInv(latLonMatch.Groups["Longitude"].Value);
                if (showLocName)
                {
                    locName = geoClient.GetFirstReverseGeo(latitude, longitude).Result;
                }
            }
            else
            {
                // find the location using GeoNames (Wunderground's geocoding is really bad)
                GeoName loc = geoClient.GetFirstGeoName(location).Result;
                if (loc == null)
                {
                    ConnectionManager.SendChannelMessage(channel, $"{nick}: GeoNames cannot find that location!");
                    return;
                }
                latitude  = loc.Latitude;
                longitude = loc.Longitude;
                locName   = loc.NameAndCountryName;
            }

            foreach (var(provider, weather) in providersWeathers)
            {
                string finalWeather = weather;
                if (finalWeather == null)
                {
                    finalWeather = provider.GetWeatherDescriptionForCoordinates(latitude, longitude);
                }
                OutputWeather(locName, channel, nick, finalWeather, showLocName);
            }
        }