Beispiel #1
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 OWM
        private WeatherResponceOWM GetWeatherOWM(string city)
        {
            var url            = WebConfigurationManager.AppSettings["OWMConnectingString"] + city + "&units=metric&appid=1ae53a9fe9ca665c0eec2d741c52bf83";
            var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);

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

                WR = JsonConvert.DeserializeObject <WeatherResponceOWM>(response);
                return(WR);
            }
            catch (WebException ex)
            {
                if (ex.Status == WebExceptionStatus.ProtocolError &&
                    ex.Response != null)
                {
                    var resp = (HttpWebResponse)ex.Response;
                    if (resp.StatusCode == HttpStatusCode.NotFound)
                    {
                        WeatherResponceOWM WR = new WeatherResponceOWM();
                        WR.name = "Not found";
                        return(WR);
                    }
                    else
                    {
                        return(null);
                    }
                }
                else
                {
                    return(null);
                }
            }
        }