/// <summary>
        /// Get forecast from wunderground.com and openweathermap.org
        /// </summary>
        /// <param name="model">index model</param>
        /// <returns></returns>
        public (WeatherResponceOWM OWMForecast, WeatherResponceWU WUForecast) GetWeather(Index model)
        {
            var forecastOWM = GetWeatherOWM(model.ResCityName);
            var forecastWU  = new WeatherResponceWU();

            if (forecastOWM.sys != null)
            {
                forecastWU = GetWeatherWU(model.ResCityName.Replace(" ", "_"), forecastOWM.sys.country, WebConfigurationManager.AppSettings["WUConnectingString"]);
            }
            return(forecastOWM, forecastWU);
        }
예제 #2
0
 public void ViewGen(WeatherResponceOWM OWM, WeatherResponceWU WU)
 {
     if (OWM != null)
     {
         if (OWM.name != "Not found")
         {
             ViewBag.Portal1 = "OpenWeatherMap";
             ViewBag.Name1   = OWM.name + ", " + OWM.sys.country;
             ViewBag.temp1   = OWM.Main.Temp + "°C";
             ViewBag.cloud1  = "Weather: " + OWM.weather[0].main;
             ViewBag.wind1   = "Wind: " + OWM.Wind.speed + "m/sec, " + OWM.Wind.deg + " deg";
             ViewBag.humb1   = "Humibity: " + OWM.Main.humbity + "%";
             ViewBag.pres1   = "Pressure: " + OWM.Main.pressure + "hpa";
         }
         else
         {
             ViewBag.Portal1 = "OpenWeatherMap";
             ViewBag.Name1   = "City is not found";
         }
     }
     else
     {
         ViewBag.Portal1 = "OpenWeatherMap";
         ViewBag.Name1   = "City is not found";
     }
     if (WU?.current_observation != null)
     {
         if (WU.current_observation.display_location.city != "Not found")
         {
             ViewBag.Portal2 = "Weather Underground";
             ViewBag.Name2   = WU.current_observation.display_location.full.Replace("_", " ");
             ViewBag.temp2   = WU.current_observation.temp_c + "°C";
             ViewBag.cloud2  = "Weather: " + WU.current_observation.weather;
             ViewBag.wind2   = "Wind: " + WU.current_observation.wind_kph + "m/sec, " + WU.current_observation.wind_degrees + " deg";
             ViewBag.humb2   = "Humibity: " + WU.current_observation.relative_humidity;
             ViewBag.pres2   = "Pressure: " + WU.current_observation.pressure_mb + "hpa";
         }
         else
         {
             ViewBag.Portal2 = "Weather Underground";
             ViewBag.Name2   = "City is not found";
         }
     }
     else
     {
         ViewBag.Portal2 = "Weather Underground";
         ViewBag.Name2   = "City is not found";
     }
 }
        //Get weather from WU
        private WeatherResponceWU GetWeatherWU(string city, string testcountry, string url)
        {
            HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url + city + ".json");

            try
            {
                var    httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                string response;
                using (StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream()))
                {
                    response = streamReader.ReadToEnd();
                }

                var WRCC = new WeatherResponceWUCheckCIty();
                WRCC = JsonConvert.DeserializeObject <WeatherResponceWUCheckCIty>(response);

                if (WRCC.response.results != null)
                {
                    string strcode = WRCC.response.results[0].zmw;
                    if (testcountry != null)
                    {
                        for (int i = 0; i < WRCC.response.results.Length; i++)
                        {
                            if (WRCC.response.results[i].country_iso3166 == testcountry)
                            {
                                strcode = WRCC.response.results[i].zmw;
                                break;
                            }
                        }
                    }
                    return(GetWeatherWU(city, testcountry, WebConfigurationManager.AppSettings["WUConnectingString"] + "/zmw:"));
                }
                else
                {
                    var WRGF = new WeatherResponceWU();
                    WRGF = JsonConvert.DeserializeObject <WeatherResponceWU>(response);
                    if (WRGF.current_observation != null)
                    {
                        if (WRGF.current_observation.display_location.city.ToLower() != city.ToLower())
                        {
                            WRGF.current_observation.display_location.city = "Not found";
                        }
                    }
                    return(WRGF);
                }
            }
            catch (WebException ex)
            {
                if (ex.Status == WebExceptionStatus.ProtocolError &&
                    ex.Response != null)
                {
                    var resp = (HttpWebResponse)ex.Response;
                    if (resp.StatusCode == HttpStatusCode.NotFound)
                    {
                        WeatherResponceWU WRWU = new WeatherResponceWU();
                        WRWU.current_observation.display_location.city = "Not found";
                        return(null);
                    }
                    else
                    {
                        return(null);
                    }
                }
                else
                {
                    return(null);
                }
            }
        }