Fix !corona by changing data source to covid19-api.org, added a country code parameter as well

This commit is contained in:
Daan Boerlage 2021-01-24 22:15:15 +01:00
parent 21303bfca8
commit 0434335239
Signed by: daan
GPG key ID: FCE070E1E4956606
4 changed files with 87 additions and 22 deletions

View 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; }
}
}

View file

@ -1,4 +1,6 @@
using System; using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Discord; using Discord;
using Discord.Commands; using Discord.Commands;
@ -19,23 +21,29 @@ namespace Geekbot.Bot.Commands.Utils.Corona
[Command("corona", RunMode = RunMode.Async)] [Command("corona", RunMode = RunMode.Async)]
[Summary("Get the latest worldwide corona statistics")] [Summary("Get the latest worldwide corona statistics")]
public async Task Summary() public async Task Summary([Summary("CountryCode")] string countryCode = null)
{ {
try try
{ {
var summary = await HttpAbstractions.Get<CoronaSummaryDto>(new Uri("https://api.covid19api.com/world/total")); var summary = await GetCoronaInfo(countryCode);
var activeCases = summary.TotalConfirmed - (summary.TotalRecovered + summary.TotalDeaths); if (summary == null)
{
await Context.Channel.SendMessageAsync($"`{countryCode}` is not a valid country code");
return;
}
string CalculatePercentage(decimal i) => (i / summary.TotalConfirmed).ToString("#0.##%"); var activeCases = summary.Cases - (summary.Recovered + summary.Deaths);
string CalculatePercentage(decimal i) => (i / summary.Cases).ToString("#0.##%");
var activePercent = CalculatePercentage(activeCases); var activePercent = CalculatePercentage(activeCases);
var recoveredPercentage = CalculatePercentage(summary.TotalRecovered); var recoveredPercentage = CalculatePercentage(summary.Recovered);
var deathsPercentage = CalculatePercentage(summary.TotalDeaths); var deathsPercentage = CalculatePercentage(summary.Deaths);
var numberFormat = "#,#"; var numberFormat = "#,#";
var totalFormatted = summary.TotalConfirmed.ToString(numberFormat); var totalFormatted = summary.Cases.ToString(numberFormat);
var activeFormatted = activeCases.ToString(numberFormat); var activeFormatted = activeCases.ToString(numberFormat);
var recoveredFormatted = summary.TotalRecovered.ToString(numberFormat); var recoveredFormatted = summary.Recovered.ToString(numberFormat);
var deathsFormatted = summary.TotalDeaths.ToString(numberFormat); var deathsFormatted = summary.Deaths.ToString(numberFormat);
var eb = new EmbedBuilder var eb = new EmbedBuilder
{ {
@ -46,7 +54,7 @@ namespace Geekbot.Bot.Commands.Utils.Corona
}, },
Footer = new EmbedFooterBuilder Footer = new EmbedFooterBuilder
{ {
Text = "Source: covid19api.com", Text = "Source: covid19-api.org",
}, },
Color = Color.Red Color = Color.Red
}; };
@ -62,5 +70,43 @@ namespace Geekbot.Bot.Commands.Utils.Corona
await _errorHandler.HandleCommandException(e, Context); 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,
};
}
} }
} }

View file

@ -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; }
}
}

View 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; }
}
}