Skip to content

Commit

Permalink
Using free.currencyconverterapi.com api instead of www.xe.com/currenc…
Browse files Browse the repository at this point in the history
…yconverter

Removed submodule HtmlCrawler
Moved to packageReference;
  • Loading branch information
imsh committed Feb 28, 2019
1 parent 5d0fdd5 commit c70fde4
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 129 deletions.
3 changes: 0 additions & 3 deletions .gitmodules

This file was deleted.

6 changes: 0 additions & 6 deletions Currency.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
24 changes: 10 additions & 14 deletions Currency/Currency.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,10 @@
<StartupObject />
</PropertyGroup>
<ItemGroup>
<Reference Include="HtmlAgilityPack, Version=1.6.8.0, Culture=neutral, PublicKeyToken=bd319b19eaf3b43a, processorArchitecture=MSIL">
<HintPath>..\packages\HtmlAgilityPack.1.6.8\lib\Net45\HtmlAgilityPack.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.10.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
<Reference Include="Wox.Plugin, Version=1.3.159.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Wox.Plugin.1.3.159\lib\net452\Wox.Plugin.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="CurrencyPlugin.cs" />
Expand All @@ -56,13 +48,17 @@
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\HtmlCrawler\HtmlCrawler\HtmlCrawler.csproj">
<Project>{facf55ca-84b7-4e1e-bb79-53b65eaa4fc0}</Project>
<Name>HtmlCrawler</Name>
</ProjectReference>
<PackageReference Include="Newtonsoft.Json">
<Version>10.0.3</Version>
</PackageReference>
<PackageReference Include="System.Net.Http">
<Version>4.3.0</Version>
</PackageReference>
<PackageReference Include="Wox.Plugin">
<Version>1.3.159</Version>
</PackageReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
123 changes: 32 additions & 91 deletions Currency/Utils.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<decimal>();
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<Result> GetResult(SearchParams sp)
Expand All @@ -54,7 +64,7 @@ public List<Result> 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
Expand Down Expand Up @@ -96,73 +106,4 @@ public List<Result> 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<Result> Debug(SearchParams sp)
//{
// var results = new List<Result>();

// results.Add(new Result
// {
// Title = $"Debug: {sp.Amount}, {sp.FromCurrency}, {sp.ToCurrency}",
// IcoPath = "Images/icon.png"
// });

// return results;
//}

//public List<Result> Debug(string message)
//{
// var results = new List<Result>();

// results.Add(new Result
// {
// Title = $"Debug: {message}",
// IcoPath = "Images/icon.png"
// });

// return results;
//}

//public List<Result> Debug(string message, string desc)
//{
// var results = new List<Result>();

// results.Add(new Result
// {
// Title = $"Debug: {message}",
// SubTitle = $"{desc}",
// IcoPath = "Images/icon.png"
// });

// return results;
//}

//public List<Result> DebugJson(string url, string responeString)
//{
// var results = new List<Result>();

// results.Add(new Result
// {
// Title = $"Debug: {url}, {responeString}",
// IcoPath = "Images/icon.png"
// });

// return results;
//}
#endregion
}
6 changes: 0 additions & 6 deletions Currency/packages.config

This file was deleted.

1 change: 0 additions & 1 deletion HtmlCrawler
Submodule HtmlCrawler deleted from f000a1
7 changes: 2 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -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)

Expand Down Expand Up @@ -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
6 changes: 3 additions & 3 deletions plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

0 comments on commit c70fde4

Please sign in to comment.