Skip to content

Commit

Permalink
Merge pull request #11 from ozgurg/datefix
Browse files Browse the repository at this point in the history
release: v3.0.0
  • Loading branch information
ozgurg authored May 19, 2024
2 parents c574a55 + 052b73a commit d9beb94
Show file tree
Hide file tree
Showing 6 changed files with 232 additions and 63 deletions.
6 changes: 3 additions & 3 deletions .github/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

# google-currency-scraper

Scrape extremely up-to-date exchange rates from Google fast and for free, with no external dependencies!
Scrape extremely up-to-date exchange rates from Google fast and for free, with only one external dependency.

## Install

Expand All @@ -23,8 +23,8 @@ const currency = await googleCurrencyScraper({
// {
// from: "USD",
// to: "TRY",
// rate: 17.9539,
// dateUpdated: "2022-08-11T23:24:00.000Z"
// rate: 32.2434,
// dateUpdated: "May 19, 13:59 UTC"
// }
```

Expand Down
202 changes: 200 additions & 2 deletions package-lock.json

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

23 changes: 13 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "google-currency-scraper",
"version": "2.0.0",
"description": "Scrape extremely up-to-date exchange rates from Google fast and for free, with no external dependencies!",
"version": "3.0.0",
"description": "Scrape extremely up-to-date exchange rates from Google fast and for free, with only one external dependency.",
"license": "MIT",
"type": "module",
"exports": "./index.js",
Expand All @@ -27,6 +27,17 @@
"google scraper",
"currency api"
],
"engines": {
"node": ">=18.0.0"
},
"lint-staged": {
"*.js": [
"eslint"
]
},
"dependencies": {
"cheerio": "1.0.0-rc.12"
},
"devDependencies": {
"@commitlint/cli": "19.0.3",
"@commitlint/config-conventional": "19.0.3",
Expand All @@ -35,13 +46,5 @@
"husky": "9.0.11",
"lint-staged": "15.2.2",
"vitest": "1.3.1"
},
"engines": {
"node": ">=18.0.0"
},
"lint-staged": {
"*.js": [
"eslint"
]
}
}
26 changes: 14 additions & 12 deletions src/google-currency-scraper.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// eslint-disable-next-line no-unused-vars
import { CurrencyCode, isValidCurrencyCode } from "./utils/currency-code.js";
import { parseAndNormalizeDateInSearchResult } from "./utils/date.js";
import { cleanDateInSearchResult } from "./utils/date.js";
import { load } from "cheerio";

/**
* @param {object} params
Expand Down Expand Up @@ -32,31 +33,32 @@ const googleCurrencyScraper = async ({ from, to }) => {
const {
exchangeRate,
dateUpdated
} = await parseExchangeRateFromResponseText(responseText);
} = parseExchangeRateFromResponseText(responseText);

return {
from,
to,
rate: exchangeRate,
dateUpdated: parseAndNormalizeDateInSearchResult(dateUpdated).toISOString()
dateUpdated
};
};

/**
* @param {string} responseText
* @return {Promise<{exchangeRate: number, dateUpdated: string}>}
*/
const parseExchangeRateFromResponseText = async responseText => {
const exchangeRatePattern = /data-exchange-rate="([\d.]+)"/;
const exchangeRateMatch = responseText.match(exchangeRatePattern);
const exchangeRateNode = exchangeRateMatch ? exchangeRateMatch[1] : null;
const parseExchangeRateFromResponseText = responseText => {
// I replaced Cheerio with regular expression, but Google uses a different date format depending on the requester's language, region, and similar factors.
// I cannot find a pattern to match every date format that Google uses to parse.
// So I reverted to Cheerio.

const $ = load(responseText);

const dateUpdatedPattern = /<span>(\w{3} \d{1,2}, \d{2}:\d{2} UTC) · <\/span>/;
const dateUpdatedMatch = responseText.match(dateUpdatedPattern);
const dateUpdatedNode = dateUpdatedMatch ? dateUpdatedMatch[1] : null;
const exchangeRateNode = $("[data-exchange-rate]:first-child");
const dateUpdatedNode = exchangeRateNode.next().find("span:not([class]):first-child");

const exchangeRate = parseFloat(exchangeRateNode);
const dateUpdated = dateUpdatedNode;
const exchangeRate = parseFloat(exchangeRateNode.attr("data-exchange-rate"));
const dateUpdated = cleanDateInSearchResult(dateUpdatedNode.text());

return {
exchangeRate,
Expand Down
Loading

0 comments on commit d9beb94

Please sign in to comment.