Skip to content

Commit

Permalink
Merge pull request #2 from viniciusgava/feature/add-support-to-others…
Browse files Browse the repository at this point in the history
…-file-format

Add support to others file format
  • Loading branch information
viniciusgava authored Nov 17, 2019
2 parents 8e8c19d + cb74c81 commit b036baf
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 19 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
build-docker:cleanup
docker build --no-cache -t viniciusgava/itauscraper:latest .
docker build -t viniciusgava/itauscraper:latest .

publish-image:
docker push viniciusgava/itauscraper:latest
Expand Down
25 changes: 16 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Itau Scraper
Download Itaú OFX file using node and Puppeteer.
Download Itaú exportable files using node and Puppeteer.
Available file formats:
- OFC 1.0 - Money 1995 a Money 1999
- OFC 1.06 - Quicken 6
- OFX - Money 2000 *(DEFAULT)*
- TXT - It's a CSV with semi-colon

## Usage
```bash
Expand All @@ -23,14 +28,16 @@ docker run -v $(pwd):/usr/itauscrapper/download \
Usage: node run.js [options]
Options:
--help Show help [boolean]
--version Show version number [boolean]
--branch, -b Itaú branch number, format: 0000 [string] [required]
--account, -c Itaú account number, format: 00000-0 [string] [required]
--password, -p Itaú account digital password(6 digits) [number] [required]
--days, -d Transaction log days [number] [required]
--node_env Node environment
[choices: "development", "production", "docker"] [default: "production"]
--help Show help [boolean]
--version Show version number [boolean]
--branch, -b Itaú branch number, format: 0000 [string] [required]
--account, -c Itaú account number, format: 00000-0 [string] [required]
--password, -p Itaú account digital password(6 digits) [number] [required]
--days, -d Transaction log days [number] [required]
--file_format, -f File format to export
[choices: "ofc1", "ofc106", "ofx", "txt"] [default: "ofx"]
--node_env Node environment
[choices: "development", "production", "docker"] [default: "production"]
```

## Crontab
Expand Down
2 changes: 1 addition & 1 deletion config/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@
},
"download": {
"path": "./download",
"filename": "ITAU_${searchDate.year}-${searchDate.month}-${searchDate.day}_${options.days}_days_before_${searchDate.timestamp}.ofx"
"filename": "ITAU_${searchDate.year}-${searchDate.month}-${searchDate.day}_${options.days}_days_before_${searchDate.timestamp}"
}
}
25 changes: 17 additions & 8 deletions itauscraper.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const stepLogin = async (page, options) => {
console.log('Logged!')
}

const stepExportOfx = async (page, options) => {
const stepExport = async (page, options) => {
console.log('Opening statement page...')
// Go to extrato page
await page.evaluate(() => { document.querySelector('.sub-mnu').style.display = 'block' })
Expand All @@ -49,7 +49,7 @@ const stepExportOfx = async (page, options) => {
// Select frame
const frame = page.frames().find(frame => frame.name() === 'CORPO')

// Go to ofx export page
// Go to export page
await frame.waitFor('a[title="Salvar em outros formatos"]')
console.log('Opening export page...')
await frame.click('a[title="Salvar em outros formatos"]')
Expand All @@ -70,19 +70,18 @@ const stepExportOfx = async (page, options) => {
await frame.type('#Dia', searchDate.day)
await frame.type('#Mes', searchDate.month)
await frame.type('#Ano', searchDate.year)
console.log('Selecting export document type..: OFX')
await frame.click('.TRNinput[value=OFX]')
console.log('Selecting export document type..: ' + options.file_format)
await frame.click(getFileFormatSelector(options))

const finalFilePath = path.resolve(
options.download.path,
eval('`' + options.download.filename + '`') // eslint-disable-line
)

console.log('Export document final path: ', finalFilePath)

console.log('Starting download...')
await download(frame, 'img[alt="Continuar"]', finalFilePath)
const finalFilePathWithExtension = await download(frame, 'img[alt="Continuar"]', finalFilePath)
console.log('Download has been finished.')
console.log('Export document final path: ', finalFilePathWithExtension)
}

const stepClosePossiblePopup = async (page) => {
Expand Down Expand Up @@ -111,6 +110,10 @@ const sleep = (ms) => {
return new Promise(resolve => setTimeout(resolve, ms))
}

const getFileFormatSelector = (options) => {
return '.TRNinput[value=' + options.file_format.toUpperCase() + ']'
}

const download = async (page, selector, finalFilePath) => {
const downloadPath = path.resolve(os.tmpdir(), 'download', uuid())
mkdirp(downloadPath)
Expand All @@ -121,9 +124,14 @@ const download = async (page, selector, finalFilePath) => {

const filename = await waitForFileToDownload(downloadPath)
const tempFilePath = path.resolve(downloadPath, filename)
const extension = path.extname(tempFilePath)

finalFilePath += extension

console.log('Moving file to final path.')
await fs.moveSync(tempFilePath, finalFilePath)

return finalFilePath
}

const waitForFileToDownload = async (downloadPath) => {
Expand All @@ -141,6 +149,7 @@ const scraper = async (options) => {
console.log('Account Branch Number:', options.branch)
console.log('Account number:', options.account)
console.log('Transaction log days:', options.days)
console.log('File Format:', options.file_format)

console.debug('Puppeter - options', options.puppeteer)
const browser = await puppeteer.launch(options.puppeteer)
Expand All @@ -151,7 +160,7 @@ const scraper = async (options) => {

await stepLogin(page, options)
await stepClosePossiblePopup(page)
await stepExportOfx(page, options)
await stepExport(page, options)

await browser.close()

Expand Down
6 changes: 6 additions & 0 deletions run.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ var argv = require('yargs')
required: true,
type: 'number'
})
.option('file_format', {
alias: 'f',
describe: 'File format to export',
default: 'ofx',
choices: ['ofc1', 'ofc106', 'ofx', 'txt']
})
.option('node_env', {
describe: 'Node environment',
default: 'production',
Expand Down

0 comments on commit b036baf

Please sign in to comment.