Skip to content

Commit

Permalink
feat(scrapeData): use remote source
Browse files Browse the repository at this point in the history
  • Loading branch information
kripod committed Apr 14, 2024
1 parent ce4aff2 commit 331d5ab
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"prettier-plugin-astro": "0.13.0",
"tsx": "4.7.2",
"typescript": "5.4.5",
"undici": "6.13.0",
"xlsx": "https://cdn.sheetjs.com/xlsx-0.20.2/xlsx-0.20.2.tgz"
},
"packageManager": "[email protected]"
Expand Down
8 changes: 8 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 14 additions & 7 deletions src/pages/api/_scrapeData.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as fs from "node:fs/promises";

import { Agent, fetch } from "undici";
import * as XLSX from "xlsx";

import { currencyPair, isCurrency } from "../../utils/currency";
Expand All @@ -9,14 +10,20 @@ import { roundTo, safeParseFloat } from "../../utils/number";
const QUOTE_CURRENCY = "HUF";
const DEFAULT_FRACTION_DIGITS = 2;

const workbook = XLSX.read(
await fs.readFile(new URL("./_data.xlsx", import.meta.url)),
{
cellDates: true,
cellText: false,
cellHTML: false,
},
const response = await fetch(
"https://www.mnb.hu/Root/ExchangeRate/arfolyam.xlsx",
{ dispatcher: new Agent({ connectTimeout: 300_000 }) },
);
if (!response.ok) {
throw new Error("Cannot fetch sheet");
}

const data = await response.arrayBuffer();
const workbook = XLSX.read(data, {
cellDates: true,
cellText: false,
cellHTML: false,
});
const sheetName = workbook.SheetNames[0];
const sheet = sheetName != null ? workbook.Sheets[sheetName] : undefined;

Expand Down

0 comments on commit 331d5ab

Please sign in to comment.