Fix !corona by changing data source to covid19-api.org, added a country code parameter as well
This commit is contained in:
parent
21303bfca8
commit
0434335239
4 changed files with 87 additions and 22 deletions
19
src/Bot/Commands/Utils/Corona/CoronaApiCountryResponseDto.cs
Normal file
19
src/Bot/Commands/Utils/Corona/CoronaApiCountryResponseDto.cs
Normal file
|
@ -0,0 +1,19 @@
|
|||
using Newtonsoft.Json;
|
||||
|
||||
namespace Geekbot.Bot.Commands.Utils.Corona
|
||||
{
|
||||
public record CoronaApiCountryResponseDto
|
||||
{
|
||||
[JsonProperty("country")]
|
||||
public string Country { get; init; }
|
||||
|
||||
[JsonProperty("cases")]
|
||||
public decimal Cases { get; init; }
|
||||
|
||||
[JsonProperty("deaths")]
|
||||
public decimal Deaths { get; init; }
|
||||
|
||||
[JsonProperty("recovered")]
|
||||
public decimal Recovered { get; init; }
|
||||
}
|
||||
}
|
|
@ -1,4 +1,6 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Discord;
|
||||
using Discord.Commands;
|
||||
|
@ -11,7 +13,7 @@ namespace Geekbot.Bot.Commands.Utils.Corona
|
|||
public class CoronaStats : ModuleBase
|
||||
{
|
||||
private readonly IErrorHandler _errorHandler;
|
||||
|
||||
|
||||
public CoronaStats(IErrorHandler errorHandler)
|
||||
{
|
||||
_errorHandler = errorHandler;
|
||||
|
@ -19,24 +21,30 @@ namespace Geekbot.Bot.Commands.Utils.Corona
|
|||
|
||||
[Command("corona", RunMode = RunMode.Async)]
|
||||
[Summary("Get the latest worldwide corona statistics")]
|
||||
public async Task Summary()
|
||||
public async Task Summary([Summary("CountryCode")] string countryCode = null)
|
||||
{
|
||||
try
|
||||
{
|
||||
var summary = await HttpAbstractions.Get<CoronaSummaryDto>(new Uri("https://api.covid19api.com/world/total"));
|
||||
var activeCases = summary.TotalConfirmed - (summary.TotalRecovered + summary.TotalDeaths);
|
||||
var summary = await GetCoronaInfo(countryCode);
|
||||
if (summary == null)
|
||||
{
|
||||
await Context.Channel.SendMessageAsync($"`{countryCode}` is not a valid country code");
|
||||
return;
|
||||
}
|
||||
|
||||
var activeCases = summary.Cases - (summary.Recovered + summary.Deaths);
|
||||
|
||||
string CalculatePercentage(decimal i) => (i / summary.TotalConfirmed).ToString("#0.##%");
|
||||
string CalculatePercentage(decimal i) => (i / summary.Cases).ToString("#0.##%");
|
||||
var activePercent = CalculatePercentage(activeCases);
|
||||
var recoveredPercentage = CalculatePercentage(summary.TotalRecovered);
|
||||
var deathsPercentage = CalculatePercentage(summary.TotalDeaths);
|
||||
var recoveredPercentage = CalculatePercentage(summary.Recovered);
|
||||
var deathsPercentage = CalculatePercentage(summary.Deaths);
|
||||
|
||||
var numberFormat = "#,#";
|
||||
var totalFormatted = summary.TotalConfirmed.ToString(numberFormat);
|
||||
var totalFormatted = summary.Cases.ToString(numberFormat);
|
||||
var activeFormatted = activeCases.ToString(numberFormat);
|
||||
var recoveredFormatted = summary.TotalRecovered.ToString(numberFormat);
|
||||
var deathsFormatted = summary.TotalDeaths.ToString(numberFormat);
|
||||
|
||||
var recoveredFormatted = summary.Recovered.ToString(numberFormat);
|
||||
var deathsFormatted = summary.Deaths.ToString(numberFormat);
|
||||
|
||||
var eb = new EmbedBuilder
|
||||
{
|
||||
Author = new EmbedAuthorBuilder
|
||||
|
@ -46,7 +54,7 @@ namespace Geekbot.Bot.Commands.Utils.Corona
|
|||
},
|
||||
Footer = new EmbedFooterBuilder
|
||||
{
|
||||
Text = "Source: covid19api.com",
|
||||
Text = "Source: covid19-api.org",
|
||||
},
|
||||
Color = Color.Red
|
||||
};
|
||||
|
@ -54,7 +62,7 @@ namespace Geekbot.Bot.Commands.Utils.Corona
|
|||
eb.AddInlineField("Active", $"{activeFormatted} ({activePercent})");
|
||||
eb.AddInlineField("Recovered", $"{recoveredFormatted} ({recoveredPercentage})");
|
||||
eb.AddInlineField("Deaths", $"{deathsFormatted} ({deathsPercentage})");
|
||||
|
||||
|
||||
await Context.Channel.SendMessageAsync(String.Empty, false, eb.Build());
|
||||
}
|
||||
catch (Exception e)
|
||||
|
@ -62,5 +70,43 @@ namespace Geekbot.Bot.Commands.Utils.Corona
|
|||
await _errorHandler.HandleCommandException(e, Context);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task<CoronaTotalDto> GetCoronaInfo(string countryCode = null)
|
||||
{
|
||||
var allCountries = await HttpAbstractions.Get<List<CoronaApiCountryResponseDto>>(new Uri("https://covid19-api.org/api/status"));
|
||||
|
||||
if (string.IsNullOrEmpty(countryCode))
|
||||
{
|
||||
return allCountries.Aggregate(
|
||||
new CoronaTotalDto(),
|
||||
(accumulate, source) =>
|
||||
{
|
||||
accumulate.Cases += source.Cases;
|
||||
accumulate.Deaths += source.Deaths;
|
||||
accumulate.Recovered += source.Recovered;
|
||||
return accumulate;
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
if (countryCode.Length != 2)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var upcasedCountryCode = countryCode.ToUpper();
|
||||
var countryStats = allCountries.Find(x => x.Country == upcasedCountryCode);
|
||||
if (countryStats == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return new CoronaTotalDto()
|
||||
{
|
||||
Cases = countryStats.Cases,
|
||||
Deaths = countryStats.Deaths,
|
||||
Recovered = countryStats.Recovered,
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,9 +0,0 @@
|
|||
namespace Geekbot.Bot.Commands.Utils.Corona
|
||||
{
|
||||
public class CoronaSummaryDto
|
||||
{
|
||||
public decimal TotalConfirmed { get; set; }
|
||||
public decimal TotalDeaths { get; set; }
|
||||
public decimal TotalRecovered { get; set; }
|
||||
}
|
||||
}
|
9
src/Bot/Commands/Utils/Corona/CoronaTotalDto.cs
Normal file
9
src/Bot/Commands/Utils/Corona/CoronaTotalDto.cs
Normal file
|
@ -0,0 +1,9 @@
|
|||
namespace Geekbot.Bot.Commands.Utils.Corona
|
||||
{
|
||||
public record CoronaTotalDto
|
||||
{
|
||||
public decimal Cases { get; set; }
|
||||
public decimal Deaths { get; set; }
|
||||
public decimal Recovered { get; set; }
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue