Skip to content

Commit

Permalink
handle password errrors in discount scraper
Browse files Browse the repository at this point in the history
  • Loading branch information
eshaham committed Jul 30, 2017
1 parent c856fde commit 77c9797
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 32 deletions.
36 changes: 32 additions & 4 deletions lib/helpers/navigation.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,39 @@
import waitUntil from './waiting';

async function waitForUrl(page, url) {
function getKeyByValue(object, value) {
return Object.keys(object).find(key => object[key] === value);
}

async function waitForUrls(page, urls, timeout = 20000) {
try {
await waitUntil(async () => {
const current = await page.property('url');
return getKeyByValue(urls, current) != null;
}, timeout, 1000);
} catch (e) {
if (e && e.timeout) {
const current = await page.property('url');
console.log(`timeout reached this page: ${current}`);
e.lastUrl = current;
}
throw e;
}

const current = await page.property('url');
return getKeyByValue(urls, current);
}

function waitForUrl(page, url, timeout) {
return waitForUrls(page, { default: url }, timeout);
}

async function waitForRedirect(page, timeout = 20000) {
const initial = await page.property('url');
try {
await waitUntil(async () => {
const current = await page.property('url');
return current === url;
}, 20000, 1000);
return current !== initial;
}, timeout, 1000);
} catch (e) {
if (e && e.timeout) {
const current = await page.property('url');
Expand All @@ -16,4 +44,4 @@ async function waitForUrl(page, url) {
}
}

export default waitForUrl;
export { waitForUrls, waitForUrl, waitForRedirect };
91 changes: 63 additions & 28 deletions lib/scrapers/discount.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,9 @@
import phantom from 'phantom';

import waitForUrl from '../helpers/navigation';
import { waitForUrls, waitForRedirect } from '../helpers/navigation';
import { waitUntilElementFound, fillInput, clickButton } from '../helpers/elements-interactions';

async function scrape(credentials) {
console.log('discount: start scraping');

const baseUrl = 'https://start.telebank.co.il';

const instance = await phantom.create();
const page = await instance.createPage();
await page.open(`${baseUrl}/LoginPages/Logon?multilang=he&t=P&pageKey=home&bank=d`);
await waitUntilElementFound(page, 'submitButton');
await fillInput(page, 'tzId', credentials.id);
await fillInput(page, 'tzPassword', credentials.password);
await fillInput(page, 'aidnum', credentials.num);

console.log('discount: logging in');
await clickButton(page, 'submitButton');

try {
await waitForUrl(page, `${baseUrl}/apollo/core/templates/default/masterPage.html#/MY_ACCOUNT_HOMEPAGE`);
} catch (e) {
// TODO: notice change password url https://start.telebank.co.il/LoginPages/Logon
// TODO: notice invalid password url https://start.telebank.co.il/LoginPages/Logon?multilang=he&t=P&pagekey=home&bank=d#
throw new Error('couldn\'t complete scraping');
}

console.log('discount: login successful');

async function fetchAccountData(page) {
await page.includeJs('https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js');

const apiSiteUrl = 'https://start.telebank.co.il/Titan/gatewayAPI';
Expand Down Expand Up @@ -61,14 +36,74 @@ async function scrape(credentials) {
const txns = txnsData.CurrentAccountLastTransactions.OperationEntry;

const accountData = {
success: true,
accountNumber,
txns,
};
console.log(`discount: end scraping for account ${accountData.accountNumber}, found ${accountData.txns.length} transactions`);

return accountData;
}

async function scrape(credentials) {
console.log('discount: start scraping');

const baseUrl = 'https://start.telebank.co.il';

const instance = await phantom.create();
const page = await instance.createPage();
await page.open(`${baseUrl}/LoginPages/Logon?multilang=he&t=P&pageKey=home&bank=d`);
await waitUntilElementFound(page, 'submitButton');
await fillInput(page, 'tzId', credentials.id);
await fillInput(page, 'tzPassword', credentials.password);
await fillInput(page, 'aidnum', credentials.num);

console.log('discount: logging in');
await clickButton(page, 'submitButton');

await waitForRedirect(page);

const urls = {
default: `${baseUrl}/apollo/core/templates/default/masterPage.html#/MY_ACCOUNT_HOMEPAGE`,
invalidPassword: `${baseUrl}/LoginPages/Logon?multilang=he&t=P&pagekey=home&bank=d#`,
changePassword: `${baseUrl}/LoginPages/Logon`,
};

let loginResult;
try {
loginResult = await waitForUrls(page, urls);
} catch (e) {
console.error(e);
throw new Error('couldn\'t complete scraping');
}

let scrapeResult;
switch (loginResult) {
case 'default':
console.log('discount: login successful');
scrapeResult = await fetchAccountData(page);
break;
case 'invalidPassword':
console.log('discount: invalid password');
scrapeResult = {
success: false,
errorType: loginResult,
};
break;
case 'changePassword':
console.log('discount: need to change password');
scrapeResult = {
success: false,
errorType: loginResult,
};
break;
default:
throw new Error('unexpected login result');
}

await instance.exit();

return accountData;
return scrapeResult;
}

export default scrape;

0 comments on commit 77c9797

Please sign in to comment.