Skip to content

Commit

Permalink
API update
Browse files Browse the repository at this point in the history
  • Loading branch information
Advaith3600 committed Mar 1, 2024
1 parent ce81a35 commit b0d420b
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 28 deletions.
79 changes: 53 additions & 26 deletions Community.PowerToys.Run.Plugin.CurrencyConverter/Main.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using System.Globalization;
using System.Net;
using System.Net.Http;
using System.Text.Json;
using ManagedCommon;
using System.Globalization;
using System.Text.RegularExpressions;

using Wox.Plugin;
using ManagedCommon;
using Microsoft.PowerToys.Settings.UI.Library;
using System.Text.RegularExpressions;

using Clipboard = System.Windows.Clipboard;

Expand All @@ -20,7 +22,7 @@ public class Main : IPlugin, ISettingProvider

public string Description => "Currency Converter Plugin";

private Dictionary<string, (double, DateTime)> ConversionCache = new Dictionary<string, (double, DateTime)>();
private Dictionary<string, (JsonElement, DateTime)> ConversionCache = new Dictionary<string, (JsonElement, DateTime)>();
private readonly HttpClient Client = new HttpClient();
private readonly RegionInfo regionInfo = new RegionInfo(CultureInfo.CurrentCulture.Name);

Expand Down Expand Up @@ -73,55 +75,80 @@ public void UpdateSettings(PowerLauncherPluginSettings settings)
}
}

private double? GetConversionRate(string fromCurrency, string toCurrency)
private double GetConversionRate(string fromCurrency, string toCurrency)
{
string key = $"{fromCurrency}-{toCurrency}";
if (ConversionCache.ContainsKey(key) && ConversionCache[key].Item2 > DateTime.Now.AddHours(-1)) // cache for 1 hour
if (ConversionCache.ContainsKey(fromCurrency) && ConversionCache[fromCurrency].Item2 > DateTime.Now.AddHours(-1)) // cache for 1 hour
{
return ConversionCache[key].Item1;
try
{
return ConversionCache[fromCurrency].Item1.GetProperty(toCurrency).GetDouble();
}
catch (KeyNotFoundException)
{
throw new Exception($"{toCurrency.ToUpper()} is not a valid currency");
}
}
else
{
string url = $"https://cdn.jsdelivr.net/gh/fawazahmed0/currency-api@1/latest/currencies/{fromCurrency}/{toCurrency}.json";
string url = $"https://cdn.jsdelivr.net/npm/@fawazahmed0/currency-api@latest/v1/currencies/{fromCurrency}.min.json";
try
{
var response = Client.GetStringAsync(url).Result;
JsonDocument document = JsonDocument.Parse(response);
JsonElement root = document.RootElement;
double conversionRate = root.GetProperty(toCurrency).GetDouble();
ConversionCache[key] = (conversionRate, DateTime.Now);
var response = Client.GetAsync(url).Result;
if (!response.IsSuccessStatusCode)
{
if (response.StatusCode == HttpStatusCode.NotFound)
{
throw new Exception($"{fromCurrency.ToUpper()} is not a valid currency");
}
else
{
throw new Exception("Something went wrong while fetching the conversion rate");
}
}

var content = response.Content.ReadAsStringAsync().Result;
JsonElement element = JsonDocument.Parse(content).RootElement.GetProperty(fromCurrency);
double conversionRate = element.GetProperty(toCurrency).GetDouble();
ConversionCache[fromCurrency] = (element, DateTime.Now);
return conversionRate;
}
catch (Exception ex)
catch (KeyNotFoundException)
{
return null;
throw new Exception($"{toCurrency.ToUpper()} is not a valid currency");
}
}
}

private Result GetConversion(double amountToConvert, string fromCurrency, string toCurrency)
{
double? conversionRate = GetConversionRate(fromCurrency.ToLower(), toCurrency.ToLower());
fromCurrency = fromCurrency.ToUpper();
toCurrency = toCurrency.ToUpper();

if (conversionRate == null)
double conversionRate = 0;
try
{
conversionRate = GetConversionRate(fromCurrency.ToLower(), toCurrency.ToLower());
}
catch (Exception e)
{
return new Result
{
Title = $"Something went wrong while converting from {fromCurrency} to {toCurrency}",
SubTitle = "Please try again. Check your internet and the plugin settings if this persists.",
Title = e.Message,
SubTitle = "Press enter to open the currencies list",
IcoPath = IconPath,
Action = e =>
{
string url = "https://cdn.jsdelivr.net/npm/@fawazahmed0/currency-api@latest/v1/currencies.json";
System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(url) { UseShellExecute = true });
return true;
}
};
}

double convertedAmount = Math.Round(amountToConvert * (double) conversionRate, 2);
double convertedAmount = Math.Round(amountToConvert * conversionRate, 2);
string formatted = convertedAmount.ToString("N", CultureInfo.CurrentCulture);

return new Result
{
Title = $"{formatted} {toCurrency}",
SubTitle = $"Currency conversion from {fromCurrency} to {toCurrency}",
Title = $"{formatted} {toCurrency.ToUpper()}",
SubTitle = $"Currency conversion from {fromCurrency.ToUpper()} to {toCurrency.ToUpper()}",
IcoPath = IconPath,
Action = e =>
{
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Currency Converter

This is a plugin developed for PowerToys Run to convert currencies.
PowerToys Run plugin which will convert real and crypto currencies.

![Screenshot](screenshots/screenshot1.png)

Expand Down Expand Up @@ -60,4 +60,4 @@ Your local currency, global currency and the quick conversion direction can also

## Conversion API

This plugin internally uses [Currency API](https://github.com/fawazahmed0/currency-api) for the latest conversion rates.
This plugin internally uses [Currency API](https://github.com/fawazahmed0/exchange-api) for the latest conversion rates.

0 comments on commit b0d420b

Please sign in to comment.