Skip to content

Commit

Permalink
Debug as an option
Browse files Browse the repository at this point in the history
  • Loading branch information
Menighin committed Jan 14, 2020
1 parent 668e569 commit c7847c5
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 10 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cei-crawler",
"version": "0.1.0",
"version": "0.1.1",
"description": "Crawler para pegar dados do Canal Eletronico do Investidor",
"main": "src/app.js",
"scripts": {
Expand Down
8 changes: 5 additions & 3 deletions src/lib/CeiCrawler.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const puppeteer = require('puppeteer');
const StockHistoryCrawler = require('./StockHistoryCrawler');
const typedefs = require("./typedefs");

class CeiCrawler {

Expand All @@ -26,7 +27,7 @@ class CeiCrawler {
*
* @param {String} username Username to login at CEI
* @param {String} password Password to login at CEI
* @param {{puppeteerLaunch: puppeteer.LaunchOptions}} options Options for CEI Crawler and Puppeteer
* @param {{puppeteerLaunch: puppeteer.LaunchOptions, wat: typedefs.foo}} options Options for CEI Crawler and Puppeteer
*/
constructor(username, password, options) {
this.username = username;
Expand All @@ -40,7 +41,8 @@ class CeiCrawler {
if (this._browser == null)
this._browser = await puppeteer.launch(this.options.puppeteerLaunch);

console.log('Logging at CEI...');
if ((this.options && this.options.trace) || false)
console.log('Logging at CEI...');
this._page = await this._browser.newPage();
await this._page.goto('https://cei.b3.com.br/CEI_Responsivo/');
await this._page.type('#ctl00_ContentPlaceHolder1_txtLogin', this.username, { delay: 10 });
Expand All @@ -57,7 +59,7 @@ class CeiCrawler {
*/
async getStockHistory(startDate, endDate) {
await this._login();
return await StockHistoryCrawler.getStockHistory(this._page, startDate, endDate);
return await StockHistoryCrawler.getStockHistory(this._page, this.options, startDate, endDate);
}

}
Expand Down
21 changes: 16 additions & 5 deletions src/lib/StockHistoryCrawler.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const puppeteer = require('puppeteer');
const PuppeteerUtils = require('./PuppeteerUtils');
const typedefs = require("./typedefs");

const PAGE = {
URL: 'https://cei.b3.com.br/CEI_Responsivo/negociacao-de-ativos.aspx',
Expand Down Expand Up @@ -35,10 +36,13 @@ class StockHistoryCrawler {
/**
*
* @param {puppeteer.Page} page - Logged page to work with
* @param {typedefs.CeiCrawlerOptions} [options] - Options for the crawler
* @param {Date} [startDate] - The start date of the history. If none passed, the default of CEI will be used
* @param {Date} [endDate] - The end date of the history. If none passed, the default of CEI will be used
*/
static async getStockHistory(page, startDate = null, endDate = null) {
static async getStockHistory(page, options = null, startDate = null, endDate = null) {

const traceOperations = (options && options.trace) || false;

const result = [];

Expand Down Expand Up @@ -74,7 +78,8 @@ class StockHistoryCrawler {
let cachedAccount = ''; // Used to wait for page to load
for (const institution of institutions) {

console.log(`Selecting institution ${institution.label} (${institution.value})`)
if (traceOperations)
console.log(`Selecting institution ${institution.label} (${institution.value})`)
await page.select(PAGE.SELECT_INSTITUTION, institution.value);

/* istanbul ignore next */
Expand All @@ -92,7 +97,9 @@ class StockHistoryCrawler {
const accounts = await accountsHandle.jsonValue();

for (const account of accounts) {
console.log(`Selecting account ${account}`)
if (traceOperations)
console.log(`Selecting account ${account}`);

await page.select(PAGE.SELECT_ACCOUNT, account);
await page.click(PAGE.SUBMIT_BUTTON);

Expand All @@ -109,9 +116,13 @@ class StockHistoryCrawler {
});

// Process the page
console.log(`Processing stock history data`);
if (traceOperations)
console.log(`Processing stock history data`);

const data = hasData ? await this._processStockHistory(page) : [];
console.log (`Found ${data.length} operations`);

if (traceOperations)
console.log (`Found ${data.length} operations`);

// Save the result
result.push({
Expand Down
14 changes: 14 additions & 0 deletions src/lib/typedefs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const puppeteer = require('puppeteer');

/**
* @namespace typedefs
*/

/**
* @typedef CeiCrawlerOptions
* @property {puppeteer.LaunchOptions} puppeteerLaunch - Puppeteer launch options
* @property {boolean} trace - Indicates if it should print trace messages. Helpful for debugging.
* @memberof typdefs
*/

exports.unused = {};
6 changes: 5 additions & 1 deletion test/app.test.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
const test = require('ava')
const CeiCrawler = require('../src/app')
const fs = require('fs')
const typedefs = require("../src/lib/typedefs");

test.before(t => {
if (!process.env.CEI_USERNAME || !process.env.CEI_PASSWORD) {
throw Error('You should set environment variables CEI_USERNAME and CEI_PASSWORD in order to run tests');
}

/** @type {typedefs.CeiCrawlerOptions} */
const ceiCrawlerOptions = {
puppeteerLaunch: {
headless: true,
timeout: 0
}
},
trace: false
}
t.context.ceiCrawler = new CeiCrawler(process.env.CEI_USERNAME, process.env.CEI_PASSWORD, ceiCrawlerOptions);
});
Expand Down

0 comments on commit c7847c5

Please sign in to comment.