Skip to content

Commit

Permalink
Added DAS barcode SMS sending
Browse files Browse the repository at this point in the history
  • Loading branch information
engmsilva committed Mar 21, 2023
1 parent 59362c6 commit df759e4
Show file tree
Hide file tree
Showing 10 changed files with 503 additions and 55 deletions.
9 changes: 9 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#app
DEFAULT_CNPJ_INPUT=63543580000183

#twilio
TWILIO_ENABLE_SEND=false
TWILIO_ACCOUNT_SID=
TWILIO_AUTH_TOKEN=
TWILIO_CELL_FROM=
TWILIO_CELL_TO=
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
node_modules/

# files
.local.env
.pdf
bin/*.pdf
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Scraping DAS MEI
**Scraping DAS MEI** é um aplicativo de linha de comando em *Node* que usa técnica de scraping do módulo [puppeteer](https://github.com/puppeteer/puppeteer/tree/main) para fazer raspagem no site [Receita Federal](http://www8.receita.fazenda.gov.br/SimplesNacional/Aplicacoes/ATSPO/pgmei.app/Identificacao) do *Documento de Arrecadação do Simples Nacional MEI*.

O aplicativo quando usado no **modo background**, pode ser habilitado para enviar o código de barra da guia de arrecadação do DAS por SMS. Para usar a função de envio de SMS é necessário ter uma conta no [Twilio](https://www.twilio.com/).

## Modo de Raspagem

O aplicativo possui dois modos de interação para fazer a raspagem dos dados, *modo interativo* e o *modo background*.
Expand Down Expand Up @@ -43,6 +45,23 @@ $ npm start
? Informe o ano:
? Informe o mês:
```
**nota:** defina o valor da variável de ambiente `DEFAULT_CNPJ_INPUT` no arquivo `.env` para carregar um número padrão para o CNPJ.

## Envio do Código de Barra da Guia de Arrecadação do DAS por SMS

Por padrão esta função é desabilitada. Para habilitar o envio de SMS no **modo background** é preciso definir os valores das varáveis de ambiente do arquivo `.env`.

A informações abaixo podem serem encontradas no [console da sua conta no Twilio](https://console.twilio.com/).

```
TWILIO_ENABLE_SEND=true
TWILIO_ACCOUNT_SID=<your_twilio_account_sid>
TWILIO_AUTH_TOKEN=<your_twilio_auth_token>
TWILIO_CELL_FROM=<your_twilio_phone_number>
TWILIO_CELL_TO=<your_phone_registered_in_twilio>
```



## Considerações

Expand Down
6 changes: 5 additions & 1 deletion bin/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
#!/usr/bin/env node
import inquirer from 'inquirer';
import dotenv from 'dotenv';
import { validateCNPJ } from './validateCNPJ.js';
import { validateYear, validateMonth } from './validateDate.js';
import { scraping } from './scraping.js';

// Load environment variables from .env file
dotenv.config();

console.log = function() {};

async function main() {
Expand All @@ -27,7 +31,7 @@ async function main() {
type: 'input',
name: 'cnpj',
message: 'Informe o CNPJ:',
default: '63543580000183',
default: process.env.DEFAULT_CNPJ_INPUT,
async validate(value) {
const valid = await validateCNPJ(value);
return valid[0] || valid[1];
Expand Down
7 changes: 7 additions & 0 deletions bin/loggers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import winston from 'winston';

export const logger = winston.createLogger({
level: "info",
format: winston.format.simple(),
transports: [new winston.transports.Console()],
});
81 changes: 36 additions & 45 deletions bin/readBarcodePDF.js
Original file line number Diff line number Diff line change
@@ -1,66 +1,57 @@
import { PDFBarcodeJs } from 'pdf-barcode';
import winston from 'winston';
import { addDac } from './addDAC.js';

const logger = winston.createLogger({
level: 'info',
format: winston.format.simple(),
transports: [
new winston.transports.Console()
]
});
import { PDFBarcodeJs } from "pdf-barcode";
import { logger } from './loggers.js';
import { addDac } from "./addDAC.js";
import { sendSMS } from "./sendSMS.js";

// path to save the DAS tab
const downloadPath = process.cwd() + '/bin';
const downloadPath = process.cwd() + "/bin";

const configs = {
scale: {
once: true,
value: 3,
start: 3,
step: 0.6,
stop: 4.8
once: true,
value: 3,
start: 3,
step: 0.6,
stop: 4.8,
},
resultOpts: {
singleCodeInPage: true,
multiCodesInPage: false,
maxCodesInPage: 1
singleCodeInPage: true,
multiCodesInPage: false,
maxCodesInPage: 1,
},
patches: [
"x-small",
"small",
"medium"
],
patches: ["x-small", "small", "medium"],
improve: true,
noisify: true,
quagga: {
inputStream: {},
locator: {
halfSample: false
},
decoder: {
readers: [
"i2of5_reader",
],
multiple: false
},
locate: true
}
inputStream: {},
locator: {
halfSample: false,
},
decoder: {
readers: ["i2of5_reader"],
multiple: false,
},
locate: true,
},
};

//Reads the barcode from a PDF file
function readBarcodePDF(header) {
if (typeof header['content-disposition'] === "string") {
if(header['content-disposition'].includes('filename')) {
const filename = header['content-disposition'].replace("attachment; filename=", "");
if (typeof header["content-disposition"] === "string") {
if (header["content-disposition"].includes("filename")) {
const filename = header["content-disposition"].replace(
"attachment; filename=",
""
);
const filePath = new URL(`file:///${downloadPath + "/" + filename}`).href;
PDFBarcodeJs.decodeSinglePage(filePath, 1, configs, (response) => {
const barcode = response.codes[0];
const barcodeWithDAC = addDac(barcode);
logger.info(`Barcode: ${barcodeWithDAC}`);
const barcode = response.codes[0];
const barcodeWithDAC = addDac(barcode);
sendSMS(barcodeWithDAC);
logger.info(`Barcode: ${barcodeWithDAC}`);
});
};
}
}
}

export { readBarcodePDF };
export { readBarcodePDF };
10 changes: 1 addition & 9 deletions bin/scraping.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
import puppeteer from 'puppeteer-extra';
import winston from 'winston';
import { executablePath } from 'puppeteer';
import StealthPlugin from 'puppeteer-extra-plugin-stealth';
import AdblockerPlugin from 'puppeteer-extra-plugin-adblocker';
import UserPreferencesPlugin from 'puppeteer-extra-plugin-user-preferences';
import { readBarcodePDF } from './readBarcodePDF.js';

const logger = winston.createLogger({
level: 'info',
format: winston.format.simple(),
transports: [
new winston.transports.Console()
]
});
import { logger } from './loggers.js';

// path to save the DAS tab
const downloadPath = process.cwd() + '/bin';
Expand Down
25 changes: 25 additions & 0 deletions bin/sendSMS.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { createRequire } from 'module';
import dotenv from 'dotenv';
import { logger } from './loggers.js';

// Load environment variables from .env file
dotenv.config();

function sendSMS(msg) {
//Send sms if TWILIO ENABLE SEND environment variable is set to true
if(process.env.TWILIO_ENABLE_SEND.toLowerCase() === 'true') {
const accountSid = process.env.TWILIO_ACCOUNT_SID;
const authToken = process.env.TWILIO_AUTH_TOKEN;
const require = createRequire(import.meta.url);
const client = require('twilio')(accountSid, authToken);
client.messages
.create({
body: `Código de barra da guia do DAS: ${msg}`,
from: process.env.TWILIO_CELL_FROM,
to: process.env.TWILIO_CELL_TO
})
.then(message => logger.info(message.body));
}
};

export { sendSMS };
Loading

0 comments on commit df759e4

Please sign in to comment.