WELib/OpenWeatherMap.cs
/// <summary>
/// Weather data grab from openweathermap.org!
/// </summary>
/// <param name="apiKey">Required API key from openweathermap.org!</param>
/// <param name="CityName">Provide a city name!</param>
/// <param name="units">Add metric for Celsius/Add imperial for Fahrenheit</param>
/// <returns></returns>
Weather data grab from openweathermap.org!
Weather data grab from openweathermap . org!
public static string WeatherForecast(string apiKey, string CityName, string units)
        {
            string outs = string.Empty;
            try
            {
                if (apiKey.Length > 0) 
                {
                    if (units.Length > 0)
                    {
                        string html = @"https://api.openweathermap.org/data/2.5/weather?q={0}&appid={1}&units=" + units;
                        HttpResponseMessage response = clientH.GetAsync(string.Format(html, CityName, apiKey)).GetAwaiter().GetResult();
                        response.EnsureSuccessStatusCode();
                        string responseBody = response.Content.ReadAsStringAsync().Result;
                        string l = "";
                        string line = "";
                        responseBody = responseBody.Replace(",", Environment.NewLine);
                        responseBody = responseBody.Replace("\"", "");
                        responseBody = responseBody.Replace("}", "");
                        responseBody = responseBody.Replace("{", "");
                        responseBody = responseBody.Replace("wind:", "");
                        responseBody = responseBody.Replace("main:", "");
                        using (var sr = new StringReader(responseBody))
                        {
                            while ((line = sr.ReadLine()) != null)
                            {
                                if (line.Contains("temp") || line.Contains("feel") || line.Contains("humidity") || line.Contains("speed") || line.Contains("pressure"))
                                {
                                    l += line + Environment.NewLine;
                                }
                            }
                        }
                        outs = l;
                        outs = outs.Replace("temp:", "");
                        outs = outs.Replace("feels_like:", "");
                        outs = outs.Replace("temp_min:", "");
                        outs = outs.Replace("temp_max:", "");
                        outs = outs.Replace("humidity:", "");
                        outs = outs.Replace("speed:", "");
                        outs = outs.Replace("pressure:", "");
                    }
                    else
                    {
                        outs = "No openweathermap.org API Key saved or unit input inccorect! Please check";
                    }
                }
            }
            catch
            {
                outs = "Error: Please check city name!";
            }
            return outs;
        }
public static string WeatherForecast ( string apiKey , string CityName , string units ) { string outs = string . Empty ; try { if ( apiKey . Length > 0 ) { if ( units . Length > 0 ) { string html = @"https://api.openweathermap.org/data/2.5/weather?q={0}&appid={1}&units=" + units ; HttpResponseMessage response = clientH . GetAsync ( string . Format ( html , CityName , apiKey ) ) . GetAwaiter ( ) . GetResult ( ) ; response . EnsureSuccessStatusCode ( ) ; string responseBody = response . Content . ReadAsStringAsync ( ) . Result ; string l = " " ; string line = " " ; responseBody = responseBody . Replace ( " " , Environment . NewLine ) ; responseBody = responseBody . Replace ( " \" " , " " ) ; responseBody = responseBody . Replace ( " " , " " ) ; responseBody = responseBody . Replace ( " " , " " ) ; responseBody = responseBody . Replace ( " " , " " ) ; responseBody = responseBody . Replace ( " " , " " ) ; using ( var sr = new StringReader ( responseBody ) ) { while ( ( line = sr . ReadLine ( ) ) != null ) { if ( line . Contains ( " " ) || line . Contains ( " " ) || line . Contains ( " " ) || line . Contains ( " " ) || line . Contains ( " " ) ) { l += line + Environment . NewLine ; } } } outs = l ; outs = outs . Replace ( " " , " " ) ; outs = outs . Replace ( " " , " " ) ; outs = outs . Replace ( " " , " " ) ; outs = outs . Replace ( " " , " " ) ; outs = outs . Replace ( " " , " " ) ; outs = outs . Replace ( " " , " " ) ; outs = outs . Replace ( " " , " " ) ; } else { outs = " " ; } } } catch { outs = " " ; } return outs ; }
Weather data grab from openweathermap.org!
Weather data grab from openweathermap . org!
// we check the lenght of api key and units //Open weather map API link with celsius  // TODO: will decide if I put switch for ferenhait //parssing the oudtput //--------------------------------- //we check only for what we need, like: temp, feel, humidity, wind speed //renaming output parts //--------------------------------- //we print the issue on the log viewer console //In case of error we output this in console. //print the final weather forecast
None
apiKey
string
Required API key from openweathermap.org!
Required API key from openweathermap . org!
CityName
string
Provide a city name!
Provide a city name!
units
string
Add metric for Celsius/Add imperial for Fahrenheit
Add metric for Celsius / Add imperial for Fahrenheit