From 66230ccc7b471208c36c928fd49a4c34265ffe14 Mon Sep 17 00:00:00 2001 From: SakaDream Date: Sun, 7 May 2017 15:04:09 +0700 Subject: [PATCH] Auto convert based on computer's location, instead of Windows Region --- Currency/Currency.csproj | 4 --- Currency/CurrencyPlugin.cs | 36 ++++++++++++++------------ Currency/Models/SearchParams.cs | 37 ++++++++++++++++++++++----- {Currency/Images => Images}/icon.png | Bin README.md | 11 ++++---- Currency/plugin.json => plugin.json | 2 +- 6 files changed, 56 insertions(+), 34 deletions(-) rename {Currency/Images => Images}/icon.png (100%) rename Currency/plugin.json => plugin.json (94%) diff --git a/Currency/Currency.csproj b/Currency/Currency.csproj index faca133..b2d7f67 100644 --- a/Currency/Currency.csproj +++ b/Currency/Currency.csproj @@ -59,10 +59,6 @@ - - - - diff --git a/Currency/CurrencyPlugin.cs b/Currency/CurrencyPlugin.cs index 2cd20f4..5705c59 100644 --- a/Currency/CurrencyPlugin.cs +++ b/Currency/CurrencyPlugin.cs @@ -89,7 +89,7 @@ public List Query(Query query) return new List(); } } - catch(Exception) + catch(Exception e) { //return Debug(query.RawQuery + ". Inside catch: " + e.Message); return new List(); @@ -111,21 +111,6 @@ private decimal GetRate(string fromCurrency, string toCurrency) } } - //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 GetResult(SearchParams sp) { var results = new List(); @@ -147,10 +132,26 @@ public List GetResult(SearchParams sp) IcoPath = "Images/icon.png" }); } - // Debug: Add {APIDebug(sp.FromCurrency, sp.ToCurrency)} to Result.Title + //Debug: Add {APIDebug(sp.FromCurrency, sp.ToCurrency)} to Result.Title 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(); @@ -203,5 +204,6 @@ public List GetResult(SearchParams sp) // return results; //} + #endregion } } diff --git a/Currency/Models/SearchParams.cs b/Currency/Models/SearchParams.cs index 9e3e419..672e7f3 100644 --- a/Currency/Models/SearchParams.cs +++ b/Currency/Models/SearchParams.cs @@ -1,9 +1,7 @@ -using System; -using System.Collections.Generic; +using Newtonsoft.Json.Linq; using System.Globalization; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using System.IO; +using System.Net; namespace Currency.Models { @@ -18,7 +16,7 @@ public SearchParams(decimal Amount, string FromCurrency) { this.Amount = Amount; this.FromCurrency = FromCurrency; - this.ToCurrency = RegionInfo.CurrentRegion.ISOCurrencySymbol; + this.ToCurrency = GetCurrencyCode(); } public SearchParams(decimal Amount, string FromCurrency, string ToCurrency) { @@ -36,7 +34,32 @@ public SearchParams(string FromCurrency) { this.Amount = 1; this.FromCurrency = FromCurrency; - this.ToCurrency = RegionInfo.CurrentRegion.ISOCurrencySymbol; + this.ToCurrency = GetCurrencyCode(); + } + + private string GetCurrencyCode() + { + var CountryCode = ""; + + var url = $"http://ip-api.com/json"; + 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); + CountryCode = json["countryCode"].ToString(); + } + try + { + RegionInfo ri = new RegionInfo(CountryCode); + return ri.ISOCurrencySymbol; + } + catch + { + return RegionInfo.CurrentRegion.ISOCurrencySymbol; + } } } } diff --git a/Currency/Images/icon.png b/Images/icon.png similarity index 100% rename from Currency/Images/icon.png rename to Images/icon.png diff --git a/README.md b/README.md index e028a98..4861b8e 100644 --- a/README.md +++ b/README.md @@ -2,23 +2,24 @@ An easy-to-use Currency Converter for Wox. Currency API by [http://www.currencyconverterapi.com/](http://www.currencyconverterapi.com/) # Features -Auto convert based on Windows localization +**New in v1.0.1!** Auto convert based on computer's location (Public IP) -![](https://puu.sh/vHeML.png) +![](https://puu.sh/vIhpB.png) Get Foreign exchange rate -![](https://puu.sh/vHeLZ.png) +![](https://puu.sh/vIhqe.png) Convert using native speak or Google Search style ![](https://puu.sh/vHeMt.png) -![Convert using Google Search style](https://puu.sh/vHeME.png) +![](https://puu.sh/vHeME.png) # About Wox - Homepage: [http://www.getwox.com/](http://www.getwox.com/) - Doc: [http://doc.getwox.com/en/](http://doc.getwox.com/en/) # Changelog -v1.0.0 (05/06/2017 07:19 GMT) : First Release +- v1.0.0 (05/06/2017 07:19 GMT) : First Release +- v1.0.1 (06/06/2017 07:50 GMT) : Auto convert based on computer's location, instead of Windows Region \ No newline at end of file diff --git a/Currency/plugin.json b/plugin.json similarity index 94% rename from Currency/plugin.json rename to plugin.json index da34d4f..face7bc 100644 --- a/Currency/plugin.json +++ b/plugin.json @@ -4,7 +4,7 @@ "Name":"Currency", "Description":"An easy-to-use Currency Converter for Wox", "Author":"SakaDream", - "Version":"1.0.0", + "Version":"1.0.1", "Language":"csharp", "Website":"https://github.com/SakaDream/wox-currency-converter", "IcoPath": "Images\\icon.png",