all currencies converted correctly

This commit is contained in:
Gregory Tertyshny 2024-07-15 14:32:15 +03:00
parent 64c26ab368
commit f1947c0e00
6 changed files with 125 additions and 83 deletions

View file

@ -1,4 +1,3 @@
import currency from "currency.js";
import { bookUrl } from "./bookUrl"; import { bookUrl } from "./bookUrl";
import { extractPriceFrom } from "./extractPriceFrom"; import { extractPriceFrom } from "./extractPriceFrom";
import { getRate } from "./rates"; import { getRate } from "./rates";
@ -22,18 +21,13 @@ export const bookPriceFor = async (country) => {
let convertedPrice; let convertedPrice;
if (countryPrice) { if (countryPrice) {
l("found price", countryPrice, url);
const rate = await getRate(country.currencyCode); const rate = await getRate(country.currencyCode);
convertedPrice = await convertedPrice(
l("rate for", country.currencyCode, "is", rate); countryPrice,
country.currencyCode,
convertedPrice = currency(countryPrice).divide(rate); rate,
);
convertedPrice = {
intValue: convertedPrice.intValue,
format: convertedPrice.format(),
};
l("converted price for", country.currencyCode, convertedPrice);
} }
const newPrice = { const newPrice = {

46
lib/convertPrice.js Normal file
View file

@ -0,0 +1,46 @@
import currency from "currency.js";
export const convertPrice = async (price, curr, rate) => {
l("rate for", curr, "is", rate);
let useVedic = false;
switch (curr) {
case "inr":
case "php": {
useVedic = true;
break;
}
case "jpy": {
break;
}
case "zar": {
price = price.replace(",", ".");
break;
}
case "dkk": {
price = price.replace("kr.", "").replace(",", ".");
break;
}
case "pen": {
price = price.replace("S/.", "");
break;
}
case "clp":
case "cop":
case "twd": {
break;
}
default:
price = price.replace(/[^\d,.-]/, "").replace(",", ".");
}
const convertedPrice = currency(price, { useVedic }).divide(rate);
l("converted price for", curr, convertedPrice);
return {
intValue: convertedPrice.intValue,
formatted: convertedPrice.format(),
};
};

View file

@ -6,44 +6,44 @@ export const COUNTRIES = [
{ countryCode: "za", currencyCode: "zar" }, { countryCode: "za", currencyCode: "zar" },
{ countryCode: "au", currencyCode: "aud" }, { countryCode: "au", currencyCode: "aud" },
{ countryCode: "hk", currencyCode: "hkd" }, { countryCode: "hk", currencyCode: "hkd" },
{ countryCode: "jp", currencyCode: "JPY" }, { countryCode: "jp", currencyCode: "jpy" },
// { countryCode: "my", currencyCode: "EUR" }, { countryCode: "my", currencyCode: "myr" },
// { countryCode: "nz", currencyCode: "EUR" }, { countryCode: "nz", currencyCode: "nzd" },
// { countryCode: "ph", currencyCode: "EUR" }, { countryCode: "ph", currencyCode: "php" },
// { countryCode: "sg", currencyCode: "EUR" }, { countryCode: "sg", currencyCode: "sgd" },
// { countryCode: "tw", currencyCode: "EUR" }, { countryCode: "tw", currencyCode: "twd" },
// { countryCode: "th", currencyCode: "EUR" }, { countryCode: "th", currencyCode: "usd" },
// { countryCode: "at", currencyCode: "EUR" }, { countryCode: "at", currencyCode: "eur" },
// { countryCode: "be", currencyCode: "EUR" }, { countryCode: "be", currencyCode: "eur" },
// { countryCode: "cy", currencyCode: "EUR" }, { countryCode: "cy", currencyCode: "eur" },
// { countryCode: "cz", currencyCode: "EUR" }, { countryCode: "cz", currencyCode: "czk" },
// { countryCode: "dk", currencyCode: "EUR" }, { countryCode: "dk", currencyCode: "dkk" },
// { countryCode: "ee", currencyCode: "EUR" }, { countryCode: "ee", currencyCode: "eur" },
// { countryCode: "fi", currencyCode: "EUR" }, { countryCode: "fi", currencyCode: "eur" },
// { countryCode: "fr", currencyCode: "EUR" }, { countryCode: "fr", currencyCode: "eur" },
// { countryCode: "de", currencyCode: "EUR" }, { countryCode: "de", currencyCode: "eur" },
// { countryCode: "gr", currencyCode: "EUR" }, { countryCode: "gr", currencyCode: "eur" },
// { countryCode: "ie", currencyCode: "EUR" }, { countryCode: "ie", currencyCode: "eur" },
// { countryCode: "it", currencyCode: "EUR" }, { countryCode: "it", currencyCode: "eur" },
// { countryCode: "lt", currencyCode: "EUR" }, { countryCode: "lt", currencyCode: "eur" },
// { countryCode: "lu", currencyCode: "EUR" }, { countryCode: "lu", currencyCode: "eur" },
// { countryCode: "mt", currencyCode: "EUR" }, { countryCode: "mt", currencyCode: "eur" },
// { countryCode: "nl", currencyCode: "EUR" }, { countryCode: "nl", currencyCode: "eur" },
// { countryCode: "no", currencyCode: "EUR" }, { countryCode: "no", currencyCode: "nok" },
// { countryCode: "pl", currencyCode: "EUR" }, { countryCode: "pl", currencyCode: "pln" },
// { countryCode: "pt", currencyCode: "EUR" }, { countryCode: "pt", currencyCode: "eur" },
// { countryCode: "ro", currencyCode: "EUR" }, { countryCode: "ro", currencyCode: "ron" },
// { countryCode: "sk", currencyCode: "EUR" }, { countryCode: "sk", currencyCode: "eur" },
// { countryCode: "si", currencyCode: "EUR" }, { countryCode: "si", currencyCode: "eur" },
// { countryCode: "es", currencyCode: "EUR" }, { countryCode: "es", currencyCode: "eur" },
// { countryCode: "se", currencyCode: "EUR" }, { countryCode: "se", currencyCode: "sek" },
// { countryCode: "ch", currencyCode: "EUR" }, { countryCode: "ch", currencyCode: "chf" },
// { countryCode: "tr", currencyCode: "EUR" }, { countryCode: "tr", currencyCode: "try" },
// { countryCode: "gb", currencyCode: "EUR" }, { countryCode: "gb", currencyCode: "gbp" },
// { countryCode: "ar", currencyCode: "EUR" }, { countryCode: "ar", currencyCode: "usd" },
// { countryCode: "br", currencyCode: "EUR" }, { countryCode: "br", currencyCode: "brl" },
// { countryCode: "cl", currencyCode: "EUR" }, { countryCode: "cl", currencyCode: "clp" },
// { countryCode: "co", currencyCode: "EUR" }, { countryCode: "co", currencyCode: "cop" },
// { countryCode: "mx", currencyCode: "EUR" }, { countryCode: "mx", currencyCode: "mxn" },
// { countryCode: "pe", currencyCode: "EUR" }, { countryCode: "pe", currencyCode: "pen" },
]; ];

View file

@ -40,7 +40,11 @@ export const extractPriceFrom = async (url) => {
l("starting observing price on", url); l("starting observing price on", url);
return await observePriceOnPage(iframe.contentDocument.body, url); const price = await observePriceOnPage(iframe.contentDocument.body, url);
document.body.removeChild(iframe);
return price;
} catch (e) { } catch (e) {
l("getPriceForCountry", e, url); l("getPriceForCountry", e, url);
return ""; return "";

View file

@ -5,28 +5,26 @@ import { bookPriceFor } from "./bookPriceFor";
/* /*
TODO: TODO:
- not all currencies parsed as expected:
https://www.kobo.com/za/en/ebook/nine-minds
ZA: R300,60 -> $1,674.58
JP: 2,246 -> $Infinity
- publish to stores - publish to stores
- check in chrome
- more durable source for rates - more durable source for rates
- React for UI - React for UI
- More informative, show loading progress - More informative UI, show loading progress
- clear stale cache - clear stale cache
- readme how to use and debug
- configuration (purge cache, change base currency)
*/ */
const createPricesContainer = () => { const createPricesContainer = () => {
const pricingActionContainer = document const pricingActionContainer = document.querySelector(
.querySelector(".primary-right-container") ".primary-right-container",
.querySelector(".pricing-details") );
.querySelector(".action-container");
const container = document.createElement("div"); const container = document.createElement("div");
container.style.display = "flex"; container.style.display = "flex";
container.style.flexDirection = "column"; container.style.flexDirection = "column";
container.style.border = "1px solid black";
container.style.padding = "10px";
return pricingActionContainer.parentNode.insertBefore( return pricingActionContainer.parentNode.insertBefore(
container, container,
@ -34,7 +32,7 @@ const createPricesContainer = () => {
); );
}; };
const getCountriesPrice = async () => { const getPrices = async () => {
const prices = []; const prices = [];
for (const country of COUNTRIES) { for (const country of COUNTRIES) {
// intentionally blocking execution // intentionally blocking execution
@ -46,14 +44,6 @@ const getCountriesPrice = async () => {
return prices; return prices;
}; };
const formatPrice = (price) => {
const convertedPrice = price?.convertedPrice?.format ?? "NO PRICE";
const countryPrice = price?.countryPrice ?? "NO PRICE";
return `${price.countryCode.toUpperCase()}: ${countryPrice} -> ${convertedPrice}`;
};
const sortPrices = (prices) => const sortPrices = (prices) =>
prices.sort( prices.sort(
(a, b) => (a, b) =>
@ -68,19 +58,28 @@ const showPrices = (container, prices) => {
link.href = price.url; link.href = price.url;
link.target = "_blank"; link.target = "_blank";
link.style.marginBottom = "5px"; link.style.marginBottom = "5px";
link.innerText = formatPrice(price); link.style.display = "flex";
link.style.justifyContent = "space-between";
const oldPrice = document.createElement("p");
oldPrice.innerText = `${price.countryCode.toUpperCase()}: ${price?.countryPrice || "NO PRICE"}`;
const newPrice = document.createElement("p");
newPrice.innerText = price?.convertedPrice?.formatted ?? "NO PRICE";
newPrice.style.fontWeight = "bold";
link.appendChild(oldPrice);
link.appendChild(newPrice);
container.appendChild(link); container.appendChild(link);
}); });
}; };
async function main() { async function main() {
const container = createPricesContainer();
container.innerText = "LOADING PRICES...";
try { try {
initCache(); initCache();
const container = createPricesContainer(); const countriesPrice = await getPrices();
container.innerText = "LOADING PRICES...";
const countriesPrice = await getCountriesPrice();
l("country prices", countriesPrice); l("country prices", countriesPrice);
@ -91,6 +90,7 @@ async function main() {
showPrices(container, countriesPrice); showPrices(container, countriesPrice);
} catch (e) { } catch (e) {
l("error", e); l("error", e);
container.innerText = "FAILED TO LOAD PRICES. CHECK CONSOLE FOR MORE INFO";
} finally { } finally {
l("done"); l("done");
} }

View file

@ -4,12 +4,10 @@ import { getRates, cacheRates } from "./cache";
export const baseCurrency = "usd"; export const baseCurrency = "usd";
export const loadCurrencyRates = async () => { export const loadCurrencyRates = async () => {
l("loading currency rates");
try { try {
const res = await fetch( const url = `https://cdn.jsdelivr.net/npm/@fawazahmed0/currency-api@latest/v1/currencies/${baseCurrency}.json`;
`https://cdn.jsdelivr.net/npm/@fawazahmed0/currency-api@latest/v1/currencies/${baseCurrency}.json`, l("loading currency rates", url);
); const res = await fetch(url);
return await res.json(); return await res.json();
} catch (e) { } catch (e) {
l("loadCurrencyRates", e); l("loadCurrencyRates", e);