Skip to content

Commit

Permalink
Merge pull request #32 from brunoosilva/treasure
Browse files Browse the repository at this point in the history
Adicionando a Extrato do Tesouro Direto
  • Loading branch information
Menighin authored Dec 20, 2020
2 parents 4d3004f + 74c46b5 commit 5af36eb
Show file tree
Hide file tree
Showing 5 changed files with 584 additions and 5 deletions.
74 changes: 73 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,77 @@ Resultado:
}
```

#### getTreasure(_date_)
Método que processa todos os dados disponíveis sobre Tesouro Direto em um período e retorna como uma lista e também uma lista das transações.
```javascript
let treasures = await ceiCrawler.getTreasures(date);
```
Resultado:
```javascript
[
{
"institution": "3 - XP INVESTIMENTOS CCTVM S/A",
"account": "123456",
"treasures": [
{
"code": "Tesouro IPCA+ 2045",
"expirationDate": "2045-05-15T03:00:00.000Z",
"investedValue": 12.34,
"grossValue": 13.43,
"netValue": 10.12,
"quantity": 0.01,
"blocked": 0,
"transactions": [
{
"tradeDate": "2020-11-27T03:00:00.000Z",
"quantity": 0.01,
"price": 1234.56,
"notional": 12.34,
"profitability": "IPCA + 4,05%",
"grossProfitability": "IPCA + 566,89%",
"grossProfitabilityPercent": 12.34,
"grossValue": 45.67,
"investmentTerm": 18,
"taxBracket": 23.4,
"taxIrValue": 0.12,
"taxIofValue": 1.94,
"feeB3Value": 0,
"feeInstitutionValue": 0,
"netValue": 42.67
}
]
}
]
}
]
```
#### getTreasureOptions()
Retorna as opções dos formulários da página de tesouro direto
```javascript
const treasureOptions = await ceiCrawler.getTreasureOptions();
```
Resultado:
```javascript
{
"institutions": [
{
"value": "123",
"label": "123 - RICO INVESTIMENTOS - GRUPO XP",
"accounts": [
"12345"
]
},
{
"value": "321",
"label": "321 - INTER DTVM LTDA",
"accounts": [
"54321"
]
}
]
}
```

## Opções
Na criação de um `CeiCrawler` é possivel especificar alguns valores para o parâmetro `options` que modificam a forma que o crawler funciona. As opções são:

Expand Down Expand Up @@ -367,7 +438,8 @@ try {
- [x] Histórico de ações
- [x] Dividendos
- [x] Carteira de ações
- [x] Tesouro Direto
- [x] Tesouro Direto (Resumido)
- [x] Tesouro Direto (Analítico)

## Licença
MIT
26 changes: 23 additions & 3 deletions src/lib/CeiCrawler.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const StockHistoryCrawler = require('./StockHistoryCrawler');
const DividendsCrawler = require('./DividendsCrawler');
const WalletCrawler = require('./WalletCrawler');
const TreasureCrawler = require('./TreasureCrawler');
const typedefs = require("./typedefs");
const { CeiCrawlerError, CeiErrorTypes } = require('./CeiCrawlerError');
const FetchCookieManager = require('./FetchCookieManager');
Expand All @@ -26,7 +27,7 @@ class CeiCrawler {
set options(options) { this._options = options; }

/**
*
*
* @param {String} username - Username to login at CEI
* @param {String} password - Password to login at CEI
* @param {typedefs.CeiCrawlerOptions} options - Options for CEI Crawler and Fetch
Expand Down Expand Up @@ -62,13 +63,13 @@ class CeiCrawler {
/* istanbul ignore next */
if ((this.options && this.options.trace) || false)
console.log('Logging at CEI...');

const getPageLogin = await this._cookieManager.fetch("https://ceiapp.b3.com.br/CEI_Responsivo/login.aspx");
const doomLoginPage = cheerio.load(await getPageLogin.text());

doomLoginPage('#ctl00_ContentPlaceHolder1_txtLogin').attr('value', this.username);
doomLoginPage('#ctl00_ContentPlaceHolder1_txtSenha').attr('value', this.password);

const formData = CeiUtils.extractFormDataFromDOM(doomLoginPage, [
'ctl00$ContentPlaceHolder1$smLoad',
'__EVENTTARGET',
Expand Down Expand Up @@ -183,6 +184,25 @@ class CeiCrawler {
return await WalletCrawler.getWalletOptions(this._cookieManager, this._options);
}

/**
* Returns the treasure for each account in CEI
* @param {Date} [date] - The date to get the wallet
* @returns {Promise<typedefs.TreasureItem[]>} - List of available Treasure information
*/
async getTreasures(date) {
await this._login();
return await TreasureCrawler.getTreasure(this._cookieManager, this.options, date);
}

/**
* Returns the options for the treasure
* @returns {Promise<typedefs.TreasureOptions>} - Options for treasure
*/
async getTreasureOptions() {
await this._login();
return await TreasureCrawler.getTreasureOptions(this._cookieManager, this._options);
}

}

module.exports = CeiCrawler;
Loading

0 comments on commit 5af36eb

Please sign in to comment.