egypt-banks-scraper
Version:
Scrape exchange rates from Egypt banks
54 lines (47 loc) • 1.32 kB
JavaScript
/* eslint class-methods-use-this: ["error", { "exceptMethods": ["scraper"] }] */
import rp from 'request-promise';
import Bank from './Bank';
import { banksNames } from './banks_names';
export default class CIB extends Bank {
constructor() {
const url = 'https://www.cibeg.com/_layouts/15/LINKDev.CIB.CurrenciesFunds/FundsCurrencies.aspx/GetCurrencies';
super(banksNames.CIB, url);
}
/**
* Request then pass json to scraper
* @param {function} finish callback to pass rates to when finish scraping
*/
async scrape() {
// try {
const query = {
url: this.url,
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(JSON.parse('{ "lang": "en" }')),
};
const body = await rp(query);
const currencyList = JSON.parse(body).d;
const rates = this.scraper(currencyList);
return (rates);
// } catch (err) {
// finish(err);
// }
}
/**
* Scrape rates from html
* @param {Object} currencyList list from the bank's raw json
*/
scraper(currencyList) {
const rates = [];
currencyList.forEach((currency) => {
rates.push({
code: currency.CurrencyID,
buy: currency.BuyRate,
sell: currency.SellRate,
});
});
return rates;
}
}