From c70fde49d81863178c8a567a70ec4c0332683152 Mon Sep 17 00:00:00 2001 From: imsh Date: Thu, 28 Feb 2019 20:28:03 +0100 Subject: [PATCH] Using free.currencyconverterapi.com api instead of www.xe.com/currencyconverter Removed submodule HtmlCrawler Moved to packageReference; --- .gitmodules | 3 - Currency.sln | 6 -- Currency/Currency.csproj | 24 ++++---- Currency/Utils.cs | 123 ++++++++++----------------------------- Currency/packages.config | 6 -- HtmlCrawler | 1 - README.md | 7 +-- plugin.json | 6 +- 8 files changed, 47 insertions(+), 129 deletions(-) delete mode 100644 .gitmodules delete mode 100644 Currency/packages.config delete mode 160000 HtmlCrawler diff --git a/.gitmodules b/.gitmodules deleted file mode 100644 index a919197..0000000 --- a/.gitmodules +++ /dev/null @@ -1,3 +0,0 @@ -[submodule "HtmlCrawler"] - path = HtmlCrawler - url = https://github.com/SakaDream/HtmlCrawler.git diff --git a/Currency.sln b/Currency.sln index 8863fa7..227944c 100644 --- a/Currency.sln +++ b/Currency.sln @@ -5,8 +5,6 @@ VisualStudioVersion = 15.0.27130.2010 MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Currency", "Currency\Currency.csproj", "{3F628A2F-3E21-4221-B8BB-CB3045C22D8D}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HtmlCrawler", "HtmlCrawler\HtmlCrawler\HtmlCrawler.csproj", "{FACF55CA-84B7-4E1E-BB79-53B65EAA4FC0}" -EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -17,10 +15,6 @@ Global {3F628A2F-3E21-4221-B8BB-CB3045C22D8D}.Debug|Any CPU.Build.0 = Debug|Any CPU {3F628A2F-3E21-4221-B8BB-CB3045C22D8D}.Release|Any CPU.ActiveCfg = Release|Any CPU {3F628A2F-3E21-4221-B8BB-CB3045C22D8D}.Release|Any CPU.Build.0 = Release|Any CPU - {FACF55CA-84B7-4E1E-BB79-53B65EAA4FC0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {FACF55CA-84B7-4E1E-BB79-53B65EAA4FC0}.Debug|Any CPU.Build.0 = Debug|Any CPU - {FACF55CA-84B7-4E1E-BB79-53B65EAA4FC0}.Release|Any CPU.ActiveCfg = Release|Any CPU - {FACF55CA-84B7-4E1E-BB79-53B65EAA4FC0}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Currency/Currency.csproj b/Currency/Currency.csproj index 28273ae..ed3f23c 100644 --- a/Currency/Currency.csproj +++ b/Currency/Currency.csproj @@ -35,18 +35,10 @@ - - ..\packages\HtmlAgilityPack.1.6.8\lib\Net45\HtmlAgilityPack.dll - - - ..\packages\Newtonsoft.Json.10.0.3\lib\net45\Newtonsoft.Json.dll - + - - ..\packages\Wox.Plugin.1.3.159\lib\net452\Wox.Plugin.dll - @@ -56,13 +48,17 @@ - - - {facf55ca-84b7-4e1e-bb79-53b65eaa4fc0} - HtmlCrawler - + + 10.0.3 + + + 4.3.0 + + + 1.3.159 + \ No newline at end of file diff --git a/Currency/Utils.cs b/Currency/Utils.cs index 1bc9921..9752f04 100644 --- a/Currency/Utils.cs +++ b/Currency/Utils.cs @@ -1,26 +1,15 @@ using System; using System.Collections.Generic; using System.Globalization; -using System.Linq; -using HtmlCrawler; +using System.Net.Http; +using System.Net.Http.Headers; +using Newtonsoft.Json.Linq; using Wox.Plugin; namespace Currency { class Utils { - private Crawler _crawler; - - public Utils() - { - _crawler = new Crawler(); - } - - private bool CheckToCurrencyCode(string toCurrencyInput, string toCurrencyCrawled) - { - return toCurrencyInput.Equals(toCurrencyCrawled); - } - public decimal ParseAmount(string amount) { decimal result = 0; @@ -33,13 +22,34 @@ public decimal ParseAmount(string amount) private decimal GetExchange(string fromCurrency, string toCurrency, decimal amount) { - var url = $"http://www.xe.com/currencyconverter/convert/?Amount={amount}&From={fromCurrency}&To={toCurrency}"; - _crawler.UpdateDocument(url); - var resultNode = _crawler.GetElementsByClass("uccResultAmount").First(); - var toCurrencyCodeNode = _crawler.GetElementsByClass("uccToCurrencyCode").First(); - return (CheckToCurrencyCode(toCurrency, toCurrencyCodeNode.InnerText)) - ? Decimal.Parse(resultNode.InnerText, new CultureInfo("en-US")) - : -1; + var url = $"https://free.currencyconverterapi.com/api/v6/convert"; + var urlParameters = $"?q={fromCurrency}_{toCurrency}&compact=y"; + + using (var client = new HttpClient()) + { + client.BaseAddress = new Uri(url); + client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); + + // List data response. + HttpResponseMessage response = client.GetAsync(urlParameters).Result; + + if (response.IsSuccessStatusCode) + { + // Parse the response body. + var stringResponse = response.Content.ReadAsStringAsync().Result; + JObject res = JObject.Parse(stringResponse); + var result = res[$"{fromCurrency}_{toCurrency}"]["val"].Value(); + Console.WriteLine("{0} ({1}) = {2}", (int)response.StatusCode, response.ReasonPhrase, result); + + return result * amount; + } + else + { + Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase); + + return -1; + } + } } public List GetResult(SearchParams sp) @@ -54,7 +64,7 @@ public List GetResult(SearchParams sp) { Title = $"{sp.Amount:N} {sp.FromCurrency} = {exchange:N} {sp.ToCurrency}", IcoPath = "Images/icon.png", - SubTitle = $"Source: http://www.xe.com/currencyconverter/" + SubTitle = $"Source: https://free.currencyconverterapi.com" }); } else @@ -96,73 +106,4 @@ public List GetMessage(string message, string desc) return results; } } - #region Debug - //private string APIDebug(string fromCurrency, string toCurrency) - //{ - // string query = fromCurrency + "_" + toCurrency; - // var url = $"http://free.currencyconverterapi.com/api/v3/convert?q={query}&compact=ultra"; - // var request = (HttpWebRequest)WebRequest.Create(url); - // request.Method = "GET"; - // var respone = (HttpWebResponse)request.GetResponse(); - // using (new StreamReader(respone.GetResponseStream())) - // { - // var responeString = new StreamReader(respone.GetResponseStream()).ReadToEnd(); - // var json = JObject.Parse(responeString); - // return url + ", " + json[query]; - // } - //} - - //public List Debug(SearchParams sp) - //{ - // var results = new List(); - - // results.Add(new Result - // { - // Title = $"Debug: {sp.Amount}, {sp.FromCurrency}, {sp.ToCurrency}", - // IcoPath = "Images/icon.png" - // }); - - // return results; - //} - - //public List Debug(string message) - //{ - // var results = new List(); - - // results.Add(new Result - // { - // Title = $"Debug: {message}", - // IcoPath = "Images/icon.png" - // }); - - // return results; - //} - - //public List Debug(string message, string desc) - //{ - // var results = new List(); - - // results.Add(new Result - // { - // Title = $"Debug: {message}", - // SubTitle = $"{desc}", - // IcoPath = "Images/icon.png" - // }); - - // return results; - //} - - //public List DebugJson(string url, string responeString) - //{ - // var results = new List(); - - // results.Add(new Result - // { - // Title = $"Debug: {url}, {responeString}", - // IcoPath = "Images/icon.png" - // }); - - // return results; - //} - #endregion } diff --git a/Currency/packages.config b/Currency/packages.config deleted file mode 100644 index da62910..0000000 --- a/Currency/packages.config +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/HtmlCrawler b/HtmlCrawler deleted file mode 160000 index f000a17..0000000 --- a/HtmlCrawler +++ /dev/null @@ -1 +0,0 @@ -Subproject commit f000a1701724037c8edac6d587f01f9db4e4bd7c diff --git a/README.md b/README.md index 2f87126..b240754 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,5 @@ # Wox Currency Converter -An easy-to-use Currency Converter for Wox. Foreign Exchange Rate Source by [http://www.xe.com/currencyconverter/](http://www.xe.com/currencyconverter/) - -# Install - -`wpm install Currency` +An easy-to-use Currency Converter for Wox. Foreign Exchange Rate Source by [https://free.currencyconverterapi.com](https://free.currencyconverterapi.com) ![](https://puu.sh/yJQsZ.png) @@ -31,3 +27,4 @@ Convert using native speak or Google Search style - v1.0.1 (05/07/2017 07:50 GMT) : Auto convert based on computer's location, instead of Windows Region - v1.1.0 (12/20/2017 18:00 GMT) : Change Foreign Exchange Rate Source to http://www.xe.com/currencyconverter/ - v1.1.1 (01/14/2018 17:00 GMT) : Fix decimal parsing error (Thanks [benyaminl](https://github.com/benyaminl)) +- v1.2.0 (09/02/2018 12:00 GMT) : Switched to https://free.currencyconverterapi.com instead of original one diff --git a/plugin.json b/plugin.json index 8874728..b783095 100644 --- a/plugin.json +++ b/plugin.json @@ -3,10 +3,10 @@ "ActionKeyword":"*", "Name":"Currency", "Description":"An easy-to-use Currency Converter for Wox", - "Author":"SakaDream", - "Version":"1.1.1", + "Author":"SakaDream/imsh", + "Version":"1.2.0", "Language":"csharp", - "Website":"https://github.com/SakaDream/wox-currency-converter", + "Website":"https://github.com/imsh/wox-currency-converter", "IcoPath": "Images\\icon.png", "ExecuteFileName":"Currency.dll" } \ No newline at end of file