egypt-banks-scraper
Version:
Scrape exchange rates from Egypt banks
64 lines (55 loc) • 1.48 kB
JavaScript
/* eslint class-methods-use-this: ["error", { "exceptMethods": ["scraper"] }] */
import cheerio from 'cheerio';
import Bank from './Bank';
import { banksNames } from './banks_names';
export default class ABE extends Bank {
constructor() {
const url = 'https://www.abe.com.eg/index.php/en/2017-03-02-08-57-42';
const selector = 'table tbody tr';
super(banksNames.ABE, url, selector);
}
static getCurrencyCode(name) {
const dict = {
'US DOLLAR': 'USD',
EURO: 'EUR',
'POUND STERLING': 'GBP',
'SAUDI RIALS': 'SAR',
};
return (dict[name]);
}
/**
* Scrape rates from html
* @param {Object} html html of bank web page to scrape
*/
scraper(html) {
const $ = cheerio.load(html);
// const tableRows = $('table tbody tr');
const tableRows = $('table tbody').eq(0).children();
const rates = [];
tableRows.each((index, row) => {
if (index === 0) return;
const currencyName = $(row)
.children()
.eq(2)
.text()
.trim();
const buyRate = $(row)
.children()
.eq(1)
.text()
.trim();
const sellRate = $(row)
.children()
.eq(0)
.text()
.trim();
if (Number(buyRate) === 0 || Number(sellRate) === 0) return;
rates.push({
code: ABE.getCurrencyCode(currencyName),
buy: Number(buyRate),
sell: Number(sellRate),
});
});
return rates;
}
}