-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from ozgurg/dev
release: v1.2.0
- Loading branch information
Showing
10 changed files
with
673 additions
and
764 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
// This file should not be used and will be removed in the next major release | ||
import { | ||
closeBrowser, | ||
emulateDevice, | ||
ensurePageLoadOnlyDocument, | ||
launchBrowser, | ||
openNewPage | ||
} from "./utils/browser.js"; | ||
import { CurrencyCode, isValidCurrencyCode } from "./utils/currency-code.js"; | ||
import { objectToQueryString } from "./utils/object-to-query-string.js"; | ||
import { getDate, parseAndNormalizeDateInSearchResult } from "./utils/date.js"; | ||
|
||
/** | ||
* @param {object} params | ||
* @param {CurrencyCode | string} params.from | ||
* @param {CurrencyCode | string} params.to | ||
* @returns {Promise<{from: CurrencyCode | string, to: CurrencyCode | string, rate: number, dateUpdated: string}>} | ||
* @deprecated | ||
*/ | ||
const googleCurrencyScraper = async ({ from, to }) => { | ||
if (!isValidCurrencyCode(from)) { | ||
throw new Error(`Invalid 'from' currency code: ${from}`); | ||
} | ||
|
||
if (!isValidCurrencyCode(to)) { | ||
throw new Error(`Invalid 'to' currency code: ${to}`); | ||
} | ||
|
||
if (from === to) { | ||
return { | ||
from, | ||
to, | ||
rate: 1, | ||
dateUpdated: getDate() | ||
}; | ||
} | ||
|
||
const browser = await launchBrowser(); | ||
|
||
const page = await openNewPage(browser); | ||
|
||
// To lighter page and faster load times, I emulate a mobile device. | ||
// So why iPhone 7? | ||
// It doesn't matter which one as long as it's a mobile device. | ||
// That's why I chose iPhone 7 as I use it in real life :) | ||
await emulateDevice(page, "iPhone 7"); | ||
|
||
// To lighter page and faster load times, make sure load only document. | ||
await ensurePageLoadOnlyDocument(page); | ||
|
||
await goToGoogleCurrencySearchResult(page, { from, to }); | ||
|
||
const result = await parseResult(page); | ||
|
||
await closeBrowser(browser); | ||
|
||
return { | ||
from, | ||
to, | ||
rate: result.rate, | ||
dateUpdated: parseAndNormalizeDateInSearchResult(result.dateUpdated) | ||
}; | ||
}; | ||
|
||
/** | ||
* @param {*} page | ||
* @param {object} params | ||
* @param {CurrencyCode | string} params.from | ||
* @param {CurrencyCode | string} params.to | ||
* @returns {Promise<* | null>} | ||
* @deprecated | ||
*/ | ||
async function goToGoogleCurrencySearchResult(page, { from, to }) { | ||
const qs = objectToQueryString({ | ||
q: `1+${from}+to+${to}`, | ||
hl: "en" // Make sure to use the English language to avoid any weirdness | ||
}); | ||
return await page.goto(`https://www.google.com/search?${qs}`, { | ||
waitUntil: "networkidle2" | ||
}); | ||
} | ||
|
||
/** | ||
* @param {*} page | ||
* @returns {Promise<{rate: number, dateUpdated: string}>} | ||
* @deprecated | ||
*/ | ||
async function parseResult(page) { | ||
return page.$eval( | ||
"[data-exchange-rate]", | ||
element => ({ | ||
rate: parseFloat(element.getAttribute("data-exchange-rate")), | ||
dateUpdated: element.nextSibling.querySelector("span").textContent | ||
}) | ||
); | ||
} | ||
|
||
export default googleCurrencyScraper; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// TODO: Replace "node-fetch" with "fetch" when its commonly available | ||
// https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API | ||
import fetch from "node-fetch"; | ||
|
||
/** | ||
* @param {string} url | ||
* @param {object} config | ||
* @returns {Promise<string>} | ||
*/ | ||
const makeGetRequest = async (url, config) => { | ||
const response = await fetch(url, { | ||
method: "GET", | ||
...config | ||
}); | ||
return response.text(); | ||
}; | ||
|
||
export { | ||
makeGetRequest | ||
}; |
This file was deleted.
Oops, something went wrong.