2017-10-15 07:39:46 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Net.Http;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Discord.WebSocket;
|
|
|
|
|
using NadekoBot.Core.Services;
|
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
using NLog;
|
2017-10-16 10:34:16 +00:00
|
|
|
|
using NadekoBot.Modules.Utility.Common;
|
|
|
|
|
using NadekoBot.Extensions;
|
2017-10-15 07:39:46 +00:00
|
|
|
|
|
|
|
|
|
namespace NadekoBot.Modules.Utility.Services
|
|
|
|
|
{
|
|
|
|
|
public class ConverterService : INService, IUnloadableService
|
|
|
|
|
{
|
2017-10-16 10:34:16 +00:00
|
|
|
|
public ConvertUnit[] Units =>
|
|
|
|
|
_cache.Redis.GetDatabase()
|
|
|
|
|
.StringGet("converter_units")
|
|
|
|
|
.ToString()
|
|
|
|
|
.MapJson<ConvertUnit[]>();
|
|
|
|
|
|
2017-10-15 07:39:46 +00:00
|
|
|
|
private readonly Logger _log;
|
|
|
|
|
private readonly Timer _currencyUpdater;
|
|
|
|
|
private readonly TimeSpan _updateInterval = new TimeSpan(12, 0, 0);
|
|
|
|
|
private readonly DbService _db;
|
2017-10-16 10:34:16 +00:00
|
|
|
|
private readonly IDataCache _cache;
|
|
|
|
|
private readonly HttpClient _http;
|
2017-10-15 07:39:46 +00:00
|
|
|
|
|
2017-10-16 10:34:16 +00:00
|
|
|
|
public ConverterService(DiscordSocketClient client, DbService db,
|
|
|
|
|
IDataCache cache)
|
2017-10-15 07:39:46 +00:00
|
|
|
|
{
|
|
|
|
|
_log = LogManager.GetCurrentClassLogger();
|
|
|
|
|
_db = db;
|
2017-10-16 10:34:16 +00:00
|
|
|
|
_cache = cache;
|
|
|
|
|
_http = new HttpClient();
|
2017-10-15 07:39:46 +00:00
|
|
|
|
|
|
|
|
|
if (client.ShardId == 0)
|
|
|
|
|
{
|
2017-10-16 10:34:16 +00:00
|
|
|
|
_currencyUpdater = new Timer(async (shouldLoad) => await UpdateCurrency((bool)shouldLoad),
|
|
|
|
|
client.ShardId == 0,
|
|
|
|
|
TimeSpan.Zero,
|
|
|
|
|
_updateInterval);
|
2017-10-15 07:39:46 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task<Rates> GetCurrencyRates()
|
|
|
|
|
{
|
2017-10-16 10:34:16 +00:00
|
|
|
|
var res = await _http.GetStringAsync("http://api.fixer.io/latest").ConfigureAwait(false);
|
|
|
|
|
return JsonConvert.DeserializeObject<Rates>(res);
|
2017-10-15 07:39:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task UpdateCurrency(bool shouldLoad)
|
|
|
|
|
{
|
2017-10-21 10:21:03 +00:00
|
|
|
|
try
|
2017-10-15 07:39:46 +00:00
|
|
|
|
{
|
2017-10-21 10:21:03 +00:00
|
|
|
|
var unitTypeString = "currency";
|
|
|
|
|
if (shouldLoad)
|
2017-10-15 07:39:46 +00:00
|
|
|
|
{
|
2017-10-21 10:21:03 +00:00
|
|
|
|
var currencyRates = await GetCurrencyRates();
|
|
|
|
|
var baseType = new ConvertUnit()
|
|
|
|
|
{
|
|
|
|
|
Triggers = new[] { currencyRates.Base },
|
|
|
|
|
Modifier = decimal.One,
|
|
|
|
|
UnitType = unitTypeString
|
|
|
|
|
};
|
|
|
|
|
var range = currencyRates.ConversionRates.Select(u => new ConvertUnit()
|
|
|
|
|
{
|
|
|
|
|
Triggers = new[] { u.Key },
|
|
|
|
|
Modifier = u.Value,
|
|
|
|
|
UnitType = unitTypeString
|
|
|
|
|
}).ToArray();
|
2017-10-15 07:39:46 +00:00
|
|
|
|
|
2017-10-21 10:21:03 +00:00
|
|
|
|
var fileData = JsonConvert.DeserializeObject<ConvertUnit[]>(
|
|
|
|
|
File.ReadAllText("data/units.json"));
|
2017-10-15 07:39:46 +00:00
|
|
|
|
|
2017-10-21 10:21:03 +00:00
|
|
|
|
var data = JsonConvert.SerializeObject(range.Append(baseType).Concat(fileData).ToList());
|
|
|
|
|
_cache.Redis.GetDatabase()
|
|
|
|
|
.StringSet("converter_units", data);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_log.Warn("Ignore the message below");
|
|
|
|
|
_log.Warn(ex);
|
2017-10-15 07:39:46 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Task Unload()
|
|
|
|
|
{
|
2017-10-16 10:34:16 +00:00
|
|
|
|
_currencyUpdater?.Change(Timeout.Infinite, Timeout.Infinite);
|
2017-10-15 07:39:46 +00:00
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class Rates
|
|
|
|
|
{
|
|
|
|
|
public string Base { get; set; }
|
|
|
|
|
public DateTime Date { get; set; }
|
|
|
|
|
[JsonProperty("rates")]
|
|
|
|
|
public Dictionary<string, decimal> ConversionRates { get; set; }
|
|
|
|
|
}
|
|
|
|
|
}
|