Skip to content

Instantly share code, notes, and snippets.

@leandrofabianjr
Created July 2, 2024 21:24
Show Gist options
  • Select an option

  • Save leandrofabianjr/c61e5da131ffd354742d2b5173cde74b to your computer and use it in GitHub Desktop.

Select an option

Save leandrofabianjr/c61e5da131ffd354742d2b5173cde74b to your computer and use it in GitHub Desktop.
Carregar todas as notas do site Nota Paraná
import axios from 'axios';
import { writeFileSync } from 'fs';
async function postRequestWithCookies(dateString) {
const cookiesJson = { // TODO: Substituir dados de cookie
_ga: '',
_ga_DSWHNB508V: '',
JSESSIONID: '',
THE_REQUESTED_URI: 'https://notaparana.pr.gov.br/nfprweb/Extrato',
THE_TOKEN: '',
};
const cookie = Object.entries(cookiesJson)
.map(([key, value]) => `${key}=${value}`)
.join('; ');
const url = `https://notaparana.pr.gov.br/nfprweb/NotasFiscaisAjax?periodo=${encodeURIComponent(
dateString
)}&situacao=1&_=XXXXXXXXXXXXX`;// TODO: Substituir datetime da URL
const response = await axios.get(url, {
headers: { cookie },
responseEncoding: 'latin1',
});
return response.data;
}
function getMonthsArray(startDate, endDate = new Date()) {
const endYear = endDate.getFullYear();
const endMonth = endDate.getMonth();
const monthsArray = [];
for (let year = startDate.getFullYear(); year <= endYear; year++) {
for (let month = 0; month < 12; month++) {
if (year === endYear && month > endMonth) {
break;
}
monthsArray.push(new Date(year, month));
}
}
return monthsArray;
}
async function getAllData(monthsArray) {
const data = {};
for (let date of monthsArray) {
const dateStr = date.toLocaleString('default', {
day: '2-digit',
month: '2-digit',
year: 'numeric',
});
const year = date.getFullYear();
const month = date.getMonth();
if (!data.hasOwnProperty(year)) data[year] = {};
data[year][month] = await postRequestWithCookies(dateStr);
console.log(date.getFullYear(), date.getMonth());
}
return data;
}
function getNestedProperties(obj, parentPath = '') {
let properties = [];
for (let key in obj) {
if (obj.hasOwnProperty(key)) {
let path = parentPath ? `${parentPath}.${key}` : key;
properties.push(path);
if (typeof obj[key] === 'object' && obj[key] !== null) {
properties = properties.concat(getNestedProperties(obj[key], path));
}
}
}
return properties;
}
function getAllJsonProperties(jsonObject) {
let properties = [];
for (let year in jsonObject) {
for (let month in year) {
for (let obj of jsonObject[year][month])
properties = new Set([...getNestedProperties(obj), ...properties]);
}
}
return Array.from(properties);
}
function buildHtml(jsonObject) {
const properties = getAllJsonProperties(jsonObject);
return `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Dados Nota Paraná</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous"></script>
<script src="https://code.jquery.com/jquery-3.7.1.min.js" integrity="sha256-/JqT3SQfawRcv/BIHPThkBvs0OEvtFFmqPF/lYI/Cxo=" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://cdn.datatables.net/2.0.8/css/dataTables.dataTables.css" />
<script src="https://cdn.datatables.net/2.0.8/js/dataTables.js"></script>
<style>
td div.scrolling-cell {
overflow: auto;
max-height: 3rem;
min-width: 30rem;
}
</style>
</head>
<body>
<table id="myTable">
<thead>
<tr>
${properties.map((p) => `<th>${p}</th>`).join('\n')}
</tr>
</thead>
<tbody>
${Object.getOwnPropertyNames(jsonObject)
.map((year) =>
Object.getOwnPropertyNames(jsonObject[year])
.map((month) =>
jsonObject[year][month]
.map(
(obj) =>
`<tr>${properties
.map(
(p) =>
'<td>' +
({
hintSituacao: `<div class="scrolling-cell">${
obj?.[p] ?? ''
}</div>`,
}?.[p] ??
obj?.[p] ??
'') +
'</td>'
)
.join('\n')}</tr>`
)
.join('\n')
)
.join('\n')
)
.join('\n')}
</tbody>
</table>
</body>
<script>
$(document).ready( function () {
$('#myTable').DataTable();
} );
</script>
</html>
`;
}
const startDate = new Date(2016, 0);
startDate.getFullYear();
const monthsArray = getMonthsArray(startDate);
getAllData(monthsArray)
.then((data) => writeFileSync('dados.html', buildHtml(data)))
.catch((err) => console.error(err));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment