Example #1
0
        public Weather GetWeather(string zipCode, string country, WeatherMeasure measure = WeatherMeasure.Metric)
        {
            string measureQuery = measure == WeatherMeasure.Metric ? "metric" : "imperial";
            //Sign up for a free API key at http://openweathermap.org/appid
            string key         = "42c0e77ad3018dc3c35da3da97274faf";
            string queryString = $"http://api.openweathermap.org/data/2.5/weather?zip={zipCode},{country}&appid={key}&units={measureQuery}";

            var results = GetDataFromService(queryString);

            if (results ["weather"] != null)
            {
                var weather = new Weather {
                    Title       = (string)results ["name"],
                    Temperature = (string)results ["main"] ["temp"] + " F",
                    Wind        = (string)results ["wind"] ["speed"] + " mph",
                    Humidity    = (string)results ["main"] ["humidity"] + " %",
                    Visibility  = (string)results ["weather"] [0] ["main"]
                };

                DateTime time    = new System.DateTime(1970, 1, 1, 0, 0, 0, 0);
                DateTime sunrise = time.AddSeconds((double)results ["sys"] ["sunrise"]);
                DateTime sunset  = time.AddSeconds((double)results ["sys"] ["sunset"]);
                weather.Sunrise = sunrise.ToString() + " UTC";
                weather.Sunset  = sunset.ToString() + " UTC";
                return(weather);
            }
            else
            {
                return(null);
            }
        }
        public Weather GetWeather(string country, string zipCode, WeatherMeasure measure)
        {
            var weather = weatherService.GetWeather(zipCode, country, measure);

            return(weather);
        }