public async Task <WeatherInfo> GetCurrentWeatherAsync(string city)
        {
            string      url    = $"http://api.openweathermap.org/data/2.5/weather?q={city}&units=metric&appid={AppID}";
            WeatherInfo result = null;

            result = await Task.Run(() => DownloadCurrentInfo(url));

            return(result);
        }
        public async Task <WeatherInfo> GetCurrentWeatherAsync(string city, string countryCode)
        {
            string      url    = $"http://api.openweathermap.org/data/2.5/weather?q={city},{countryCode}&units=metric&appid={AppID}";
            WeatherInfo result = null;

            try
            {
                result = await Task.Run(() => DownloadCurrentInfo(url));
            }
            catch (Exception e)
            {
                Debug.WriteLine($"{e} ERROR {DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}");
            }

            return(result);
        }
Esempio n. 3
0
        public async Task CurrentWeather(params string[] inputArray)
        {// first string in the inputArray will be name of City and Second string is countryCode
            Weather     weather     = new Weather();
            WeatherInfo weatherInfo = null;

            if (inputArray.Length == 1)
            {
                string city = inputArray[0];
                weatherInfo = await weather.GetCurrentWeatherAsync(city);
            }
            if (inputArray.Length == 2)
            {
                string city        = inputArray[0];
                string countryCode = inputArray[1];
                weatherInfo = await weather.GetCurrentWeatherAsync(city, countryCode);
            }

            if (weatherInfo == null) // if getting weatherInfo method getting exception than weatherinfo would be null
            {
                await ReplyAsync("Error Occur Try again ^오^");
            }
            else
            {
                string filepath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"Assets\");
                var    embed    = new EmbedBuilder();

                embed.ThumbnailUrl = $"attachment://{weatherInfo.weather[0].icon}.png";
                embed.Title        = $"{ weatherInfo.weather[0].main }";
                embed.Description  = $"On {weatherInfo.name}\n" +
                                     $"Current temperature:{weatherInfo.main.temp} \nMin: {weatherInfo.main.temp_min} Max: {weatherInfo.main.temp_max}\n" +
                                     $"Pressure: {weatherInfo.main.pressure}\n" +
                                     $"Wind {weatherInfo.wind.speed}\n" +
                                     $"Now it is {weatherInfo.weather[0].description}";


                await Context.Channel.SendFileAsync($"{filepath}{weatherInfo.weather[0].icon}.png", "", false, embed.Build());

                //await ReplyAsync($"Current Weather: {weatherInfo.weather[0].main}\n" +
                //    $"On {weatherInfo.name}\n" +
                //    $"Current temperature:{weatherInfo.main.temp} Min: {weatherInfo.main.temp_min} Max: {weatherInfo.main.temp_max}\n" +
                //    $"Pressure: {weatherInfo.main.pressure}\n" +
                //    $"Wind {weatherInfo.wind.speed}\n" +
                //    $"Now it is {weatherInfo.weather[0].description}");
            }
        }
        public string GetCurrentWeather(string city)
        {
            string currentWeatherInfo = "";

            using (WebClient web = new WebClient())
            {
                string url  = $"http://api.openweathermap.org/data/2.5/weather?q={city}&units=metric&appid={AppID}";
                var    json = web.DownloadString(url);

                WeatherInfo output = JsonConvert.DeserializeObject <WeatherInfo>(json);

                currentWeatherInfo = $"Current Weather: {output.weather[0].main}\n" +
                                     $"On {output.name}\n" +
                                     $"Current temperature:{output.main.temp} Min: {output.main.temp_min} Max: {output.main.temp_max}\n" +
                                     $"Pressure: {output.main.pressure}\n" +
                                     $"Wind {output.wind.speed}\n" +
                                     $"Now it is {output.weather[0].description}\n";
            }

            return(currentWeatherInfo);
        }