From 58f25400835f707f4f79f7d3657e7808a05a44ab Mon Sep 17 00:00:00 2001 From: Kwoth Date: Thu, 22 Dec 2016 07:29:24 +0100 Subject: [PATCH] Weather fixed --- .../Searches/Commands/Models/WeatherModels.cs | 67 +++++++++++++++++++ src/NadekoBot/Modules/Searches/Searches.cs | 33 ++++----- .../Resources/CommandStrings.Designer.cs | 8 +-- src/NadekoBot/Resources/CommandStrings.resx | 6 +- src/NadekoBot/_Extensions/Extensions.cs | 2 + 5 files changed, 94 insertions(+), 22 deletions(-) create mode 100644 src/NadekoBot/Modules/Searches/Commands/Models/WeatherModels.cs diff --git a/src/NadekoBot/Modules/Searches/Commands/Models/WeatherModels.cs b/src/NadekoBot/Modules/Searches/Commands/Models/WeatherModels.cs new file mode 100644 index 00000000..417b4d46 --- /dev/null +++ b/src/NadekoBot/Modules/Searches/Commands/Models/WeatherModels.cs @@ -0,0 +1,67 @@ +ο»Ώusing System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace NadekoBot.Modules.Searches.Commands.Models +{ + public class Coord + { + public double lon { get; set; } + public double lat { get; set; } + } + + public class Weather + { + public int id { get; set; } + public string main { get; set; } + public string description { get; set; } + public string icon { get; set; } + } + + public class Main + { + public double temp { get; set; } + public int pressure { get; set; } + public int humidity { get; set; } + public double temp_min { get; set; } + public double temp_max { get; set; } + } + + public class Wind + { + public double speed { get; set; } + public int deg { get; set; } + } + + public class Clouds + { + public int all { get; set; } + } + + public class Sys + { + public int type { get; set; } + public int id { get; set; } + public double message { get; set; } + public string country { get; set; } + public double sunrise { get; set; } + public double sunset { get; set; } + } + + public class WeatherData + { + public Coord coord { get; set; } + public List weather { get; set; } + public Main main { get; set; } + public int visibility { get; set; } + public Wind wind { get; set; } + public Clouds clouds { get; set; } + public int dt { get; set; } + public Sys sys { get; set; } + public int id { get; set; } + public string name { get; set; } + public int cod { get; set; } + } +} diff --git a/src/NadekoBot/Modules/Searches/Searches.cs b/src/NadekoBot/Modules/Searches/Searches.cs index b51eaa03..6c05e4c5 100644 --- a/src/NadekoBot/Modules/Searches/Searches.cs +++ b/src/NadekoBot/Modules/Searches/Searches.cs @@ -17,6 +17,7 @@ using ImageSharp; using NadekoBot.Extensions; using System.IO; using NadekoBot.Modules.Searches.Commands.OMDB; +using NadekoBot.Modules.Searches.Commands.Models; namespace NadekoBot.Modules.Searches { @@ -25,28 +26,30 @@ namespace NadekoBot.Modules.Searches { [NadekoCommand, Usage, Description, Aliases] [RequireContext(ContextType.Guild)] - public async Task Weather(IUserMessage umsg, string city, string country) + public async Task Weather(IUserMessage umsg, [Remainder] string query) { var channel = (ITextChannel)umsg.Channel; - city = city.Replace(" ", ""); - country = city.Replace(" ", ""); + if (string.IsNullOrWhiteSpace(query)) + return; + string response; using (var http = new HttpClient()) - response = await http.GetStringAsync($"http://api.ninetales.us/nadekobot/weather/?city={city}&country={country}").ConfigureAwait(false); + response = await http.GetStringAsync($"http://api.openweathermap.org/data/2.5/weather?q={query}&appid=42cd627dd60debf25a5739e50a217d74&units=metric").ConfigureAwait(false); - var obj = JObject.Parse(response)["weather"]; + var data = JsonConvert.DeserializeObject(response); var embed = new EmbedBuilder() - .AddField(fb => fb.WithName("🌍 **Location**").WithValue($"{obj["target"]}").WithIsInline(true)) - .AddField(fb => fb.WithName("πŸ“ **Lat,Long**").WithValue($"{obj["latitude"]}, {obj["longitude"]}").WithIsInline(true)) - .AddField(fb => fb.WithName("☁ **Condition**").WithValue($"{obj["condition"]}").WithIsInline(true)) - .AddField(fb => fb.WithName("πŸ˜“ **Humidity**").WithValue($"{obj["humidity"]}%").WithIsInline(true)) - .AddField(fb => fb.WithName("πŸ’¨ **Wind Speed**").WithValue($"{obj["windspeedk"]}km/h ({obj["windspeedm"]}mph)").WithIsInline(true)) - .AddField(fb => fb.WithName("🌑 **Temperature**").WithValue($"{obj["centigrade"]}Β°C ({obj["fahrenheit"]}Β°F)").WithIsInline(true)) - .AddField(fb => fb.WithName("πŸ”† **Feels like**").WithValue($"{obj["feelscentigrade"]}Β°C ({obj["feelsfahrenheit"]}Β°F)").WithIsInline(true)) - .AddField(fb => fb.WithName("πŸŒ„ **Sunrise**").WithValue($"{obj["sunrise"]}").WithIsInline(true)) - .AddField(fb => fb.WithName("πŸŒ‡ **Sunset**").WithValue($"{obj["sunset"]}").WithIsInline(true)) - .WithOkColor(); + .AddField(fb => fb.WithName("🌍 **Location**").WithValue(data.name + ", " + data.sys.country).WithIsInline(true)) + .AddField(fb => fb.WithName("πŸ“ **Lat,Long**").WithValue($"{data.coord.lat}, {data.coord.lon}").WithIsInline(true)) + .AddField(fb => fb.WithName("☁ **Condition**").WithValue(String.Join(", ", data.weather.Select(w=>w.main))).WithIsInline(true)) + .AddField(fb => fb.WithName("πŸ˜“ **Humidity**").WithValue($"{data.main.humidity}%").WithIsInline(true)) + .AddField(fb => fb.WithName("πŸ’¨ **Wind Speed**").WithValue(data.wind.speed + " km/h").WithIsInline(true)) + .AddField(fb => fb.WithName("🌑 **Temperature**").WithValue(data.main.temp + "Β°C").WithIsInline(true)) + .AddField(fb => fb.WithName("πŸ”† **Min - Max**").WithValue($"{data.main.temp_min}Β°C - {data.main.temp_max}Β°C").WithIsInline(true)) + .AddField(fb => fb.WithName("πŸŒ„ **Sunrise (utc)**").WithValue($"{data.sys.sunrise.ToUnixTimestamp():HH:mm}").WithIsInline(true)) + .AddField(fb => fb.WithName("πŸŒ‡ **Sunset (utc)**").WithValue($"{data.sys.sunset.ToUnixTimestamp():HH:mm}").WithIsInline(true)) + .WithOkColor() + .WithFooter(efb => efb.WithText("Powered by http://openweathermap.org")); await channel.EmbedAsync(embed.Build()).ConfigureAwait(false); } diff --git a/src/NadekoBot/Resources/CommandStrings.Designer.cs b/src/NadekoBot/Resources/CommandStrings.Designer.cs index 2728e701..6ad23be8 100644 --- a/src/NadekoBot/Resources/CommandStrings.Designer.cs +++ b/src/NadekoBot/Resources/CommandStrings.Designer.cs @@ -4648,8 +4648,8 @@ namespace NadekoBot.Resources { return ResourceManager.GetString("osub_usage", resourceCulture); } } - - /// + + /// /// Looks up a localized string similar to overwatch ow. /// public static string overwatch_cmd { @@ -7818,7 +7818,7 @@ namespace NadekoBot.Resources { } /// - /// Looks up a localized string similar to Shows weather data for a specified city and a country. BOTH ARE REQUIRED. Use country abbrevations.. + /// Looks up a localized string similar to Shows weather data for a specified city. You can also specify a country after a comma.. /// public static string weather_desc { get { @@ -7827,7 +7827,7 @@ namespace NadekoBot.Resources { } /// - /// Looks up a localized string similar to `{0}we Moscow RF`. + /// Looks up a localized string similar to `{0}we Moscow, RU`. /// public static string weather_usage { get { diff --git a/src/NadekoBot/Resources/CommandStrings.resx b/src/NadekoBot/Resources/CommandStrings.resx index 3e6df293..51bf4cf4 100644 --- a/src/NadekoBot/Resources/CommandStrings.resx +++ b/src/NadekoBot/Resources/CommandStrings.resx @@ -1876,10 +1876,10 @@ weather we - Shows weather data for a specified city and a country. BOTH ARE REQUIRED. Use country abbrevations. + Shows weather data for a specified city. You can also specify a country after a comma. - `{0}we Moscow RF` + `{0}we Moscow, RU` youtube yt @@ -2781,7 +2781,7 @@ `{0}crstats` or `{0}crstats 3` - + overwatch ow diff --git a/src/NadekoBot/_Extensions/Extensions.cs b/src/NadekoBot/_Extensions/Extensions.cs index 2e9c4b68..4c98b5dd 100644 --- a/src/NadekoBot/_Extensions/Extensions.cs +++ b/src/NadekoBot/_Extensions/Extensions.cs @@ -82,6 +82,8 @@ namespace NadekoBot.Extensions public static double UnixTimestamp(this DateTime dt) => dt.ToUniversalTime().Subtract(new DateTime(1970, 1, 1)).TotalSeconds; + public static DateTime ToUnixTimestamp(this double number) => new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddSeconds(number); + public static async Task SendMessageAsync(this IGuildUser user, string message, bool isTTS = false) => await (await user.CreateDMChannelAsync().ConfigureAwait(false)).SendMessageAsync(message, isTTS).ConfigureAwait(false);