conversion

This commit is contained in:
appelemac 2016-08-30 18:51:27 +02:00
parent 9057221390
commit 88a2bb83f8
2 changed files with 1155 additions and 0 deletions

View File

@ -0,0 +1,206 @@
using Discord;
using Discord.Commands;
using NadekoBot.Attributes;
using NadekoBot.Extensions;
using Newtonsoft.Json;
using NLog;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
namespace NadekoBot.Modules.Searches
{
public partial class Searches
{
[Group]
public class UnitConverterCommands
{
private Logger _log;
public UnitConverterCommands()
{
_log = LogManager.GetCurrentClassLogger();
try
{
Units = JsonConvert.DeserializeObject<List<JsonUnit>>(File.ReadAllText("units.json"));
}
catch (Exception e)
{
_log.Warn("Could not load units: " + e.Message);
}
}
public List<JsonUnit> Units { get; set; }
[Command("updatecur")]
[RequireContext(ContextType.Guild)]
public async Task UpdateCurrency(IUserMessage msg)
{
var channel = msg.Channel as IGuildChannel;
var currencyRates = await UpdateCurrencyRates();
var unitTypeString = "currency";
var baseType = new JsonUnit()
{
Triggers = new List<string>() { currencyRates.Base },
Modifier = decimal.One,
UnitType = unitTypeString
};
foreach (var rate in currencyRates.ConversionRates)
{
Units.Add(new JsonUnit() {
Triggers = new List<string>() { rate.Key},
UnitType = unitTypeString,
Modifier = rate.Value
});
}
File.WriteAllText("units.json", JsonConvert.SerializeObject(Units, Formatting.Indented));
await msg.Reply("done");
}
[LocalizedCommand, LocalizedDescription, LocalizedSummary]
[RequireContext(ContextType.Guild)]
public async Task ConvertList(IUserMessage msg)
{
var sb = new StringBuilder("Units that can be used by the converter: ");
var res = Units.GroupBy(x => x.UnitType);
foreach (var group in res)
{
sb.AppendLine($"{group.Key}: ```xl");
foreach (var el in group)
{
sb.AppendLine(string.Join(",", el.Triggers));
}
sb.AppendLine("```");
}
await msg.ReplyLong(sb.ToString());
}
[LocalizedCommand, LocalizedDescription, LocalizedSummary]
public async Task Convert(IUserMessage msg, string origin, string target, decimal value)
{
var originUnit = Units.Find(x => x.Triggers.Contains(origin));
var targetUnit = Units.Find(x => x.Triggers.Contains(target));
if (originUnit.UnitType != targetUnit.UnitType)
{
await msg.Reply(string.Format("Cannot convert {0} to {1}: types of unit are not equal", originUnit.Triggers.First(), targetUnit.Triggers.First()));
return;
}
decimal res = (value * originUnit.Modifier) / targetUnit.Modifier;
await msg.Reply(string.Format("{0} {1} is equal to {2} {3}", value, originUnit.Triggers.First(), res, targetUnit.Triggers.First()));
}
}
public static async Task<Rates> UpdateCurrencyRates()
{
using (var http = new HttpClient())
{
var res = await http.GetStringAsync("http://api.fixer.io/latest").ConfigureAwait(false);
return JsonConvert.DeserializeObject<Rates>(res);
}
}
public class Rates
{
[JsonProperty("base")]
public string Base { get; set; }
[JsonProperty("date")]
public DateTime Date { get; set; }
[JsonProperty("rates")]
public Dictionary<string, decimal> ConversionRates { get; set; }
}
public class JsonUnit
{
public List<string> Triggers { get; set; }
public string UnitType { get; set; }
public decimal Modifier { get; set; }
}
#region GetXML
/*
public class UnitCollection
{
public List<UnitType> UnitTypes;
public UnitCollection(string content)
{
using (var xmlReader = XmlReader.Create(File.OpenRead("units.xml"), new XmlReaderSettings() { IgnoreComments = true, IgnoreWhitespace = true }))
{
XmlDocument doc = new XmlDocument();
doc.Load(xmlReader);
UnitTypes = new List<UnitType>();
foreach (XmlNode node in doc.LastChild.ChildNodes)
{ //units/
UnitType type = new UnitType()
{
Name = node.Name
};
var units = new List<Unit>();
foreach (XmlNode unitNode in node.ChildNodes)
{
var curNode = unitNode.FirstChild;
Unit u = new Unit()
{
Key = curNode.InnerText,
Singular = (curNode = curNode.NextSibling).InnerText,
Plural = (curNode = curNode.NextSibling).InnerText,
Symbol = (curNode = curNode.NextSibling).InnerText,
Source = curNode.NextSibling.NextSibling.InnerText
};
List<Factor> factors = new List<Factor>();
foreach (XmlNode factorNode in curNode.NextSibling.ChildNodes)
{
Factor f = new Factor()
{
Modifier = factorNode.FirstChild.InnerText.Replace(" ", "")
};
f.From = factorNode.Attributes.GetNamedItem("from").InnerText;
factors.Add(f);
}
u.Factors = factors;
units.Add(u);
}
type.Units = units;
UnitTypes.Add(type);
}
}
}
public class UnitType
{
public string Name { get; set; }
public List<Unit> Units { get; set; }
}
public class Unit
{
public string Key { get; set; }
public string Plural { get; set; }
public string Singular { get; set; }
public string Symbol { get; set; }
public List<Factor> Factors { get; set; }
public string Source { get; set; }
}
public class Factor
{
public string From { get; set; }
public string Modifier { get; set; }
}
}
*/
#endregion
}
}

949
src/NadekoBot/units.json Normal file
View File

@ -0,0 +1,949 @@
[
{
"Triggers": [
"millimeter",
"millimeters",
"millimeter",
"mm"
],
"UnitType": "length",
"Modifier": 0.001
},
{
"Triggers": [
"centimeter",
"centimeters",
"centimeter",
"cm"
],
"UnitType": "length",
"Modifier": 0.01
},
{
"Triggers": [
"decimeter",
"decimeters",
"decimeter",
"dm"
],
"UnitType": "length",
"Modifier": 0.1
},
{
"Triggers": [
"meter",
"meters",
"meter",
"m"
],
"UnitType": "length",
"Modifier": 1.0
},
{
"Triggers": [
"kilometer",
"kilometers",
"kilometer",
"km"
],
"UnitType": "length",
"Modifier": 1000.0
},
{
"Triggers": [
"foot",
"feet",
"foot",
"ft"
],
"UnitType": "length",
"Modifier": 0.3048
},
{
"Triggers": [
"inch",
"inches",
"inch",
"in"
],
"UnitType": "length",
"Modifier": 0.0254
},
{
"Triggers": [
"mile",
"miles",
"mile",
"mi"
],
"UnitType": "length",
"Modifier": 1609.344
},
{
"Triggers": [
"yard",
"yards",
"yard",
"yd"
],
"UnitType": "length",
"Modifier": 0.9144
},
{
"Triggers": [
"cubic foot",
"cubic feet",
"cubic foot",
"ft3"
],
"UnitType": "volume",
"Modifier": 0.02831685
},
{
"Triggers": [
"cubic inch",
"cubic inches",
"cubic inch",
"in3"
],
"UnitType": "volume",
"Modifier": 0.00001638706
},
{
"Triggers": [
"cubic mile",
"cubic miles",
"cubic mile",
"mi3"
],
"UnitType": "volume",
"Modifier": 4168182000.0
},
{
"Triggers": [
"cubic yard",
"cubic yards",
"cubic yard",
"yd3"
],
"UnitType": "volume",
"Modifier": 0.7645549
},
{
"Triggers": [
"cup",
"cups",
"cup",
"cup"
],
"UnitType": "volume",
"Modifier": 0.0002365882
},
{
"Triggers": [
"imperial gallon",
"Imperial gallons",
"Imperial gallon",
"gal"
],
"UnitType": "volume",
"Modifier": 0.00454609
},
{
"Triggers": [
"us gallon",
"US gallons",
"US gallon",
"gal"
],
"UnitType": "volume",
"Modifier": 0.003785412
},
{
"Triggers": [
"milliliter",
"milliliters",
"milliliter",
"mL"
],
"UnitType": "volume",
"Modifier": 0.000001
},
{
"Triggers": [
"liter",
"liters",
"liter",
"L"
],
"UnitType": "volume",
"Modifier": 0.001
},
{
"Triggers": [
"imperial fluid ounce",
"Imperial fluid ounces",
"Imperial fluid ounce",
"fl oz"
],
"UnitType": "volume",
"Modifier": 0.00002841306
},
{
"Triggers": [
"us fluid ounce",
"US fluid ounces",
"US fluid ounce",
"fl oz"
],
"UnitType": "volume",
"Modifier": 0.00002957353
},
{
"Triggers": [
"imperial pint",
"Imperial pints",
"Imperial pint",
"pt"
],
"UnitType": "volume",
"Modifier": 0.00056826125
},
{
"Triggers": [
"us liquid pint",
"US pints (liquid)",
"US pint (liquid)",
"pt"
],
"UnitType": "volume",
"Modifier": 0.0004731765
},
{
"Triggers": [
"us dry pint",
"US pints (dry)",
"US pint (dry)",
"pt"
],
"UnitType": "volume",
"Modifier": 0.0005506105
},
{
"Triggers": [
"imperial quart",
"Imperial quarts",
"Imperial quart",
"qt"
],
"UnitType": "volume",
"Modifier": 0.00113652297
},
{
"Triggers": [
"us liquid quart",
"US quarts (liquid)",
"US quart (liquid)",
"qt"
],
"UnitType": "volume",
"Modifier": 0.0009463529
},
{
"Triggers": [
"us dry quart",
"US quarts (dry)",
"US quart (dry)",
"qt"
],
"UnitType": "volume",
"Modifier": 0.001101221
},
{
"Triggers": [
"tablespoon",
"tablespoons",
"tablespoon",
"tbsp"
],
"UnitType": "volume",
"Modifier": 0.00001478676
},
{
"Triggers": [
"teaspoon",
"teaspoons",
"teaspoon",
"tspn"
],
"UnitType": "volume",
"Modifier": 0.000004928922
},
{
"Triggers": [
"milligram",
"milligrams",
"milligram",
"mg"
],
"UnitType": "weight",
"Modifier": 0.000001
},
{
"Triggers": [
"gram",
"grams",
"gram",
"g"
],
"UnitType": "weight",
"Modifier": 0.001
},
{
"Triggers": [
"kilogram",
"kilograms",
"kilogram",
"kg"
],
"UnitType": "weight",
"Modifier": 1.0
},
{
"Triggers": [
"carat",
"carats",
"carat",
"CD"
],
"UnitType": "weight",
"Modifier": 0.00020
},
{
"Triggers": [
"grain",
"grains",
"grain",
"gr"
],
"UnitType": "weight",
"Modifier": 0.00006479891
},
{
"Triggers": [
"ounce",
"ounces",
"ounce",
"oz"
],
"UnitType": "weight",
"Modifier": 0.02834952
},
{
"Triggers": [
"pennyweight",
"pennyweights",
"pennyweight",
"dwt"
],
"UnitType": "weight",
"Modifier": 0.001555174
},
{
"Triggers": [
"pound",
"pounds",
"pound",
"lb"
],
"UnitType": "weight",
"Modifier": 0.4535924
},
{
"Triggers": [
"stone",
"stones",
"stone",
"st"
],
"UnitType": "weight",
"Modifier": 6.35029318
},
{
"Triggers": [
"slug",
"slugs",
"slug",
"slug"
],
"UnitType": "weight",
"Modifier": 14.59390
},
{
"Triggers": [
"metric ton",
"metric tons",
"metric ton",
"t"
],
"UnitType": "weight",
"Modifier": 1000.0
},
{
"Triggers": [
"long ton",
"long tons",
"long ton",
"t"
],
"UnitType": "weight",
"Modifier": 1016.047
},
{
"Triggers": [
"short ton",
"short tons",
"short ton",
"t"
],
"UnitType": "weight",
"Modifier": 907.1847
},
{
"Triggers": [
"acre",
"acres",
"acre",
"acre"
],
"UnitType": "area",
"Modifier": 4046.873
},
{
"Triggers": [
"are",
"ares",
"are",
"a"
],
"UnitType": "area",
"Modifier": 100.0
},
{
"Triggers": [
"hectare",
"hectares",
"hectare",
"ha"
],
"UnitType": "area",
"Modifier": 10000.0
},
{
"Triggers": [
"square foot",
"square feet",
"square foot",
"ft2"
],
"UnitType": "area",
"Modifier": 0.09290304
},
{
"Triggers": [
"square meter",
"square meters",
"square meter",
"m2"
],
"UnitType": "area",
"Modifier": 1.0
},
{
"Triggers": [
"square kilometer",
"square kilometers",
"square kilometer",
"km2"
],
"UnitType": "area",
"Modifier": 1000000.0
},
{
"Triggers": [
"square inch",
"square inches",
"square inch",
"in2"
],
"UnitType": "area",
"Modifier": 0.00064516
},
{
"Triggers": [
"square yard",
"square yards",
"square yard",
"yd2"
],
"UnitType": "area",
"Modifier": 0.8361274
},
{
"Triggers": [
"square mile",
"square miles",
"square mile",
"mi2"
],
"UnitType": "area",
"Modifier": 2589988.0
},
{
"Triggers": [
"aankadam",
"aankadam",
"aankadam",
"aankadam"
],
"UnitType": "area",
"Modifier": 6.69
},
{
"Triggers": [
"perch",
"perches",
"perch",
"perch"
],
"UnitType": "area",
"Modifier": 25.29
},
{
"Triggers": [
"cent",
"cents",
"cent",
"cent"
],
"UnitType": "area",
"Modifier": 40.47
},
{
"Triggers": [
"chatak",
"chataks",
"chatak",
"chatak"
],
"UnitType": "area",
"Modifier": 41.81
},
{
"Triggers": [
"kottah",
"kottah (B)",
"kottah (B)",
"kottah (B)"
],
"UnitType": "area",
"Modifier": 66.89
},
{
"Triggers": [
"guntha",
"guntha",
"guntha",
"guntha"
],
"UnitType": "area",
"Modifier": 101.17
},
{
"Triggers": [
"ground",
"grounds",
"ground",
"ground"
],
"UnitType": "area",
"Modifier": 222.97
},
{
"Triggers": [
"marla",
"marla",
"marla",
"marla"
],
"UnitType": "area",
"Modifier": 501.68
},
{
"Triggers": [
"rood",
"roods",
"rood",
"rood"
],
"UnitType": "area",
"Modifier": 1011.71
},
{
"Triggers": [
"bigha I",
"bigha I",
"bigha I",
"bigha I"
],
"UnitType": "area",
"Modifier": 1618.74
},
{
"Triggers": [
"bigha II",
"bigha II",
"bigha II",
"bigha II"
],
"UnitType": "area",
"Modifier": 2529.29
},
{
"Triggers": [
"kanal",
"kanal",
"kanal",
"kanal"
],
"UnitType": "area",
"Modifier": 10033.53
},
{
"Triggers": [
"biswa I",
"biswa I",
"biswa I",
"biswa I"
],
"UnitType": "area",
"Modifier": 32374.85
},
{
"Triggers": [
"biswa II",
"biswa II",
"biswa II",
"biswa II"
],
"UnitType": "area",
"Modifier": 50585.71
},
{
"Triggers": [
"pascal",
"pascal",
"pascal",
"Pa"
],
"UnitType": "pressure",
"Modifier": 1.0
},
{
"Triggers": [
"torr",
"torr",
"torr",
"Torr"
],
"UnitType": "pressure",
"Modifier": 133.3224
},
{
"Triggers": [
"bar",
"bars",
"bar",
"bar"
],
"UnitType": "pressure",
"Modifier": 100000.0
},
{
"Triggers": [
"millibar",
"millibars",
"millibar",
"mb"
],
"UnitType": "pressure",
"Modifier": 100.0
},
{
"Triggers": [
"psi",
"psi",
"psi",
"lbf/in2"
],
"UnitType": "pressure",
"Modifier": 6894.757
},
{
"Triggers": [
"day",
"days",
"day",
"d"
],
"UnitType": "time",
"Modifier": 86400.0
},
{
"Triggers": [
"hour",
"hours",
"hour",
"h"
],
"UnitType": "time",
"Modifier": 3600.0
},
{
"Triggers": [
"minute",
"minutes",
"minute",
"min"
],
"UnitType": "time",
"Modifier": 60.0
},
{
"Triggers": [
"year",
"years",
"year",
"yr"
],
"UnitType": "time",
"Modifier": 31536000.0
},
{
"Triggers": [
"AUD"
],
"UnitType": "currency",
"Modifier": 1.4787
},
{
"Triggers": [
"BGN"
],
"UnitType": "currency",
"Modifier": 1.9558
},
{
"Triggers": [
"BRL"
],
"UnitType": "currency",
"Modifier": 3.5991
},
{
"Triggers": [
"CAD"
],
"UnitType": "currency",
"Modifier": 1.4562
},
{
"Triggers": [
"CHF"
],
"UnitType": "currency",
"Modifier": 1.0951
},
{
"Triggers": [
"CNY"
],
"UnitType": "currency",
"Modifier": 7.4565
},
{
"Triggers": [
"CZK"
],
"UnitType": "currency",
"Modifier": 27.025
},
{
"Triggers": [
"DKK"
],
"UnitType": "currency",
"Modifier": 7.4448
},
{
"Triggers": [
"GBP"
],
"UnitType": "currency",
"Modifier": 0.8517
},
{
"Triggers": [
"HKD"
],
"UnitType": "currency",
"Modifier": 8.6631
},
{
"Triggers": [
"HRK"
],
"UnitType": "currency",
"Modifier": 7.4846
},
{
"Triggers": [
"HUF"
],
"UnitType": "currency",
"Modifier": 308.97
},
{
"Triggers": [
"IDR"
],
"UnitType": "currency",
"Modifier": 14814.35
},
{
"Triggers": [
"ILS"
],
"UnitType": "currency",
"Modifier": 4.2241
},
{
"Triggers": [
"INR"
],
"UnitType": "currency",
"Modifier": 74.8703
},
{
"Triggers": [
"JPY"
],
"UnitType": "currency",
"Modifier": 114.27
},
{
"Triggers": [
"KRW"
],
"UnitType": "currency",
"Modifier": 1244.47
},
{
"Triggers": [
"MXN"
],
"UnitType": "currency",
"Modifier": 20.7542
},
{
"Triggers": [
"MYR"
],
"UnitType": "currency",
"Modifier": 4.5205
},
{
"Triggers": [
"NOK"
],
"UnitType": "currency",
"Modifier": 9.2873
},
{
"Triggers": [
"NZD"
],
"UnitType": "currency",
"Modifier": 1.5427
},
{
"Triggers": [
"PHP"
],
"UnitType": "currency",
"Modifier": 51.797
},
{
"Triggers": [
"PLN"
],
"UnitType": "currency",
"Modifier": 4.3436
},
{
"Triggers": [
"RON"
],
"UnitType": "currency",
"Modifier": 4.4505
},
{
"Triggers": [
"RUB"
],
"UnitType": "currency",
"Modifier": 72.4564
},
{
"Triggers": [
"SEK"
],
"UnitType": "currency",
"Modifier": 9.5008
},
{
"Triggers": [
"SGD"
],
"UnitType": "currency",
"Modifier": 1.5196
},
{
"Triggers": [
"THB"
],
"UnitType": "currency",
"Modifier": 38.608
},
{
"Triggers": [
"TRY"
],
"UnitType": "currency",
"Modifier": 3.2977
},
{
"Triggers": [
"USD"
],
"UnitType": "currency",
"Modifier": 1.1168
},
{
"Triggers": [
"ZAR"
],
"UnitType": "currency",
"Modifier": 16.0537
}
]