Skip to content

Commit

Permalink
Fixes IPO + Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Menighin committed Feb 28, 2021
1 parent 895fa15 commit d770eb1
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 37 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ Mande também seu nome e usuário do GitHub (caso tenha) que eu coloco aqui no R
<p align="center">
<img src="./sponsor/picpay.png" width="150">
</p>
<p align="center">
<strong>PIX:</strong> joao.menighin@gmail.com
</p>

## Advertisement
Criei o `cei-crawler` para um projeto de acompanhamento de investimentos. Caso esteja procurando algo nesse sentido, confira o [Stoincs](https://www.stoincs.com.br)!
Expand Down
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": "2.2.1",
"version": "2.3.0",
"description": "Crawler para pegar dados do Canal Eletronico do Investidor",
"main": "src/app.js",
"repository": {
Expand Down
53 changes: 21 additions & 32 deletions src/lib/IPOCrawler.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class IPOCrawler {
* @param {FetchCookieManager} cookieManager - FetchCookieManager to work with
* @param {Date} [startDate] - The start date of the history of ipo data. If none passed, the mininum available date will be used.
* @param {Date} [endDate] - The end date of the history of ipo data. If none passed, the maximum available date will be used.
* @returns {Promise<typedefs.IPOData>} - List of available ipo information
* @returns {Promise<typedefs.IPOData[]>} - List of available ipo information
*/
static async getIPOTransactions(cookieManager, options = null, startDate = null, endDate = null) {
const getPage = await cookieManager.fetch(PAGE.URL);
Expand All @@ -95,29 +95,23 @@ class IPOCrawler {
const minDate = CeiUtils.getDateFromInput(minDateStr);
const maxDate = CeiUtils.getDateFromInput(maxDateStr);

if (startDate !== null) {
// Prevent date out of bound if parameter is set
if (startDate < minDate)
startDate = minDate;
}
else
// If parameter is not set, set min date
startDate = startDate || minDate;
// Prevent date out of bound if parameter is set
if (startDate < minDate)
startDate = minDate;

if (endDate !== null) {
// Prevent date out of bound if parameter is set
if (endDate > maxDate)
endDate = maxDate;
}
else
// If parameter is not set, set max date
endDate = endDate || maxDate;
// Prevent date out of bound if parameter is set
if (endDate > maxDate)
endDate = maxDate;

// Iterate over the range of dates and fetch the IPO transactions
for (var date = new Date(startDate); date <= endDate; date.setDate(date.getDate() + 1))
for (let date = new Date(startDate); date <= endDate; date.setDate(date.getDate() + 1))
{
var date_result = await this._getIPOTransactions(cookieManager, options, date);
date_result.forEach( (el) => result.push(el) );
if (traceOperations)
console.log(`Fetching operations from ${date}...`);
let dateResult = await this._getIPOTransactions(cookieManager, options, date);
dateResult.forEach(el => result.push(el));
}

return result;
Expand All @@ -127,7 +121,7 @@ class IPOCrawler {
* Gets ipo data available on CEI page.
* @param {FetchCookieManager} cookieManager - FetchCookieManager to work with
* @param {Date} [date] - The date of the IPO transactions.
* @returns {Promise<typedefs.IPOTransactions>} - List of available ipo transactions.
* @returns {Promise<typedefs.IPOTransaction[]>} - List of available ipo transactions.
*/
static async _getIPOTransactions(cookieManager, options, date) {
const getPage = await cookieManager.fetch(PAGE.URL);
Expand All @@ -139,7 +133,6 @@ class IPOCrawler {

// Set date
domPage(PAGE.DATE_INPUT).attr('value', CeiUtils.getDateForInput(date));


// Get all institutions to iterate
const institutions = domPage(PAGE.SELECT_INSTITUTION_OPTIONS)
Expand All @@ -154,7 +147,7 @@ class IPOCrawler {

/* istanbul ignore next */
if (traceOperations)
console.log(`Selecting institution ${institution.label} (${institution.value})`)
console.log(`Selecting data of institution ${institution.label} (${institution.value}) in date ${date}`);

domPage(PAGE.SELECT_INSTITUTION).attr('value', institution.value);

Expand All @@ -170,15 +163,13 @@ class IPOCrawler {
});

const transactions = await this._getDataPage(req, cookieManager, traceOperations);
transactions.forEach(element =>
result.push(
{
institution : institution.label,
date : CeiUtils.getDateForInput(date),
transactions : transactions
}
))

result.push(
{
institution : institution.label,
date : new Date(date.setHours(12, 0, 0, 0)),
transactions : transactions
}
);
}

return result;
Expand Down Expand Up @@ -218,7 +209,6 @@ class IPOCrawler {
* @param {cheerio.Root} dom DOM of page
* @param {FetchCookieManager} cookieManager - FetchCookieManager to work with
* @param {Boolean} traceOperations - Whether to trace operations or not
* @returns {typedef.IPOData} - The IPO transactions data.
*/
static async _getDataPage(req, cookieManager, traceOperations) {
while(true) {
Expand Down Expand Up @@ -250,7 +240,6 @@ class IPOCrawler {
/**
* Process the table given the parameters
* @param {cheerio.Root} dom DOM table of ipo transactions
* @returns {typedef.IPOTransactions}
*/
static _processTable(dom) {
const headers = Object.keys(IPO_TABLE_HEADERS);
Expand Down
33 changes: 33 additions & 0 deletions src/lib/typedefs.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,4 +172,37 @@
* @property {InstitutionOption[]} institutions - Array of available institutions with its accounts
*/

/**
* @typedef IPOTransaction
* @property {String} company - The name of the company
* @property {String} offerName - The IPO offer name
* @property {String} code - The company code in the IPO
* @property {String} isin - The IPO ISIN
* @property {String} type - The type of the IPO
* @property {String} buyMethod - The buy method of the operation
* @property {Number} reservedAmount - The reserved amount in the IPO
* @property {Number} reservedValue - The reserved value
* @property {Number} maxPrice - Max price the IPO got
* @property {Number} price - Current price
* @property {Number} allocAmount - The amount allocated
* @property {Number} allocValue - The value allocated
* @property {Date} date - The date of the reservation
* @memberof typdefs
*/

/**
* @typedef IPOData
* @property {String} institution - The name of the institution the operation happened
* @property {Date} date - The date of the operations
* @property {IPOTransaction[]} transactions - The IPO transactions for this institution and date
* @memberof typdefs
*/

/**
* @typedef IPOOptions
* @property {String} minDate - Minimum date which data is available
* @property {String} maxDate - Maximum date which data is available
* @property {InstitutionOption[]} institutions - Array of available institutions with its accounts
*/

exports.unused = {};
19 changes: 15 additions & 4 deletions test/app.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,22 +88,22 @@ test.serial('treasure', async t => {
t.true(result.length > 0);
});

test.serial('stockHistoryOptions', async t => {
test.serial('stock-history-options', async t => {
const result = await t.context.ceiCrawlerCap.getStockHistoryOptions();
t.true(result.minDate.length > 0);
});

test.serial('walletOptions', async t => {
test.serial('wallet-options', async t => {
const result = await t.context.ceiCrawlerCap.getWalletOptions();
t.true(result.minDate.length > 0);
});

test.serial('dividendsOptions', async t => {
test.serial('dividends-options', async t => {
const result = await t.context.ceiCrawlerCap.getDividendsOptions();
t.true(result.minDate.length > 0);
});

test.serial('treasureOptions', async t => {
test.serial('treasure-options', async t => {
const result = await t.context.ceiCrawlerCap.getTreasureOptions();
t.true(result.institutions.length > 0);
});
Expand All @@ -129,3 +129,14 @@ test.serial('request-timeout', async t => {
});
t.true(error.type === CeiErrorTypes.NAVIGATION_TIMEOUT);
});

test.serial('ipo-operations', async t => {
const lastThreeDays = new Date(new Date().getTime() - 1000 * 60 * 60 * 24 * 3);
const result = await t.context.ceiCrawlerCap.getIPOTransactions(lastThreeDays);
t.true(result.length > 0);
});

test.serial('ipo-options', async t => {
const result = await t.context.ceiCrawlerCap.getIPOOptions();
t.true(result.minDate.length > 0);
});

0 comments on commit d770eb1

Please sign in to comment.