(squash) init

feat: filters, settings, backups

fix: ts compile errors

feat: new dashboard, webp previews and settings

feat: use webp for pdfs

feat: use webp

fix: analyze resets old data

fix: switch to corsproxy

fix: switch to free cors

fix: max upload limit

fix: currency conversion

feat: transaction export

fix: currency conversion

feat: refactor settings actions

feat: new loader

feat: README + LICENSE

doc: update readme

doc: update readme

doc: update readme

doc: update screenshots

ci: bump prisma
This commit is contained in:
Vasily Zubarev
2025-03-13 00:30:47 +01:00
commit 0b98a2c307
153 changed files with 17271 additions and 0 deletions

57
lib/currency-scraper.ts Normal file
View File

@@ -0,0 +1,57 @@
import { format } from "date-fns"
type HistoricRate = {
currency: string
rate: number
inverse: number
}
export async function getCurrencyRate(currencyCodeFrom: string, currencyCodeTo: string, date: Date): Promise<number> {
const rates = await fetchHistoricalCurrencyRates(currencyCodeFrom, date)
if (!rates || rates.length === 0) {
console.log("Could not fetch currency rates", currencyCodeFrom, currencyCodeTo, date)
return 0
}
const rate = rates.find((rate) => rate.currency === currencyCodeTo)
if (!rate) {
console.log("Could not find currency rate", currencyCodeFrom, currencyCodeTo, date)
return 0
}
return rate.rate
}
export async function fetchHistoricalCurrencyRates(currency: string = "USD", date: Date): Promise<HistoricRate[]> {
const formattedDate = format(date, "yyyy-MM-dd")
const url = `https://corsproxy.io/?${encodeURIComponent(
`https://www.xe.com/currencytables/?from=${currency}&date=${formattedDate}`
)}`
const response = await fetch(url)
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`)
}
const html = await response.text()
// Extract the JSON data from the __NEXT_DATA__ script tag
const scriptTagRegex = /<script id="__NEXT_DATA__" type="application\/json">([\s\S]*?)<\/script>/
const match = html.match(scriptTagRegex)
if (!match || !match[1]) {
throw new Error("Could not find currency data in the page")
}
const jsonData = JSON.parse(match[1])
const historicRates = jsonData.props.pageProps.historicRates as HistoricRate[]
console.log("Historic Rates for this date", historicRates)
return historicRates
}