Skip to content

Instantly share code, notes, and snippets.

@saymont
Created November 25, 2019 18:58
Show Gist options
  • Save saymont/b90eb6e8c34e9b26ca57f053ff98b87a to your computer and use it in GitHub Desktop.
Save saymont/b90eb6e8c34e9b26ca57f053ff98b87a to your computer and use it in GitHub Desktop.
Script para sincronização de documentos com o Portal Consyst-e
###############################################################################
# Script para sincronização de documentos com o Portal Consyst-e
# Documentação da integração: https://portal.consyste.com.br/doc/api
###############################################################################
param (
[Parameter(Mandatory=$true)][string]$AuthToken, # o token de autenticação no Consyst-e
[string]$OutDir = '/tmp/csv', # o diretório onde o CSV será salvo
[string]$Kind = 'nfe', # o tipo de documento a consultar ('nfe' ou 'cte')
[string]$Query = 'recebido_em: [now-30d TO *]' # consulta a rodar
)
$ErrorActionPreference = 'Stop'
$baseUri = 'https://portal.consyste.com.br/api/v1'
# cria o diretório de saída, se não existir
New-Item -ItemType Directory -Force -Path $OutDir | Out-Null
# função para chamar a API da Consyst-e
function Call-Consyste {
param (
[Parameter(Position=0)][string]$path,
$OutFile
)
$uri = "$baseUri$path"
$pp = $progressPreference
$progressPreference = 'silentlyContinue'
Invoke-RestMethod -Headers @{ 'X-Consyste-Auth-Token' = $AuthToken } -Uri $uri -OutFile $OutFile
$progressPreference = $pp
}
# inicia um cronômetro
$sw1 = New-Object Diagnostics.Stopwatch
$sw1.Start()
# Lista de campos a retornar
$CamposParametros = "emit_cnpj,dest_cnpj,chave,numero,emitido_em,manifestacao_cd,manifestacao_realizada_em,manifestacao_justificativa,ultima_manifestacao_cd,ultima_manifestacao_data,ultima_manifestacao_descricao"
# Chama a API com a consulta solicitada
$Url = "/$Kind/lista/recebidos?q=$([uri]::EscapeDataString($Query))&campos=$CamposParametros"
$Res = Call-Consyste $Url
$Documentos = $Res.documentos
$ProximaPagina = $Res.proxima_pagina
# Continua Listagem se necessário
while ($Documentos.Count -lt $Res.total) {
$Continua = Call-Consyste "/$Kind/lista/continua/$ProximaPagina"
$Documentos = [array]$Documentos + $Continua.documentos
$ProximaPagina = $Continua.proxima_pagina
Write-Progress "Coletando documentos " -PercentComplete ($Documentos.Count / $Res.Total * 100)
}
Write-Progress "Coletando documentos" -Completed
# Cria o arquivo csv com as colunas da planilha
$FileName = Get-Date -Format "MM-dd-yyyy-HHmmss"
$CamposParametros | Out-File -Append "$OutDir\$FileName.csv" -Encoding UTF8
# Popula a panilha
# Deve-se colocar os campos na mesma ordem que está no $CamposParametros
Foreach ($doc in $Documentos){
"$($doc.emit_cnpj),$($doc.dest_cnpj),$($doc.chave),$($doc.numero),$($doc.emitido_em),$($doc.manifestacao_cd),$($doc.manifestacao_realizada_em),$($doc.manifestacao_justificativa),$($doc.ultima_manifestacao_cd),$($doc.ultima_manifestacao_data),$($doc.ultima_manifestacao_descricao)" | Out-File -Append "$OutDir\$FileName.csv" -Encoding UTF8
}
# encerra o cronômetro
$sw1.Stop()
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host
Write-Host "Obtidos $($Documentos.Count) documentos em $($sw1.Elapsed)"
@Adeana
Copy link

Adeana commented Nov 25, 2019

Keybase proof

I hereby claim:

  • I am adeana on github.
  • I am adeana (https://keybase.io/adeana) on keybase.
  • I have a public key ASD7hem_QLOqBdQ_0KOmvxFnqzv1CEV9BqFQw62kXqTpDQo

To claim this, I am signing this object:

json
{
"body": {
"key": {
"eldest_kid": "0120fb85e9bf40b3aa05d43fd0a3a6bf1167ab3bf508457d06a150c3ada45ea4e90d0a",
"host": "keybase.io",
"kid": "0120fb85e9bf40b3aa05d43fd0a3a6bf1167ab3bf508457d06a150c3ada45ea4e90d0a",
"uid": "8f53f3f884e3643f1f1b5a840e081119",
"username": "adeana"
},
"merkle_root": {
"ctime": 1574708197,
"hash": "727d445224705813b7528305b03a322936798ddf1ad5551b4340093ac40df751148872fb727a4f6b09fa22506c598f0b335d0ae8566d9d040c207491fcdd2729",
"hash_meta": "9ede77d8a7627a929aa8c31742c4704a8a89046ab5cf25a0ebd7203fb8c62f86",
"seqno": 10851523
},
"service": {
"entropy": "oPIb4rpXN3WsDd4iJeEjWyq5",
"name": "github",
"username": "adeana"
},
"type": "web_service_binding",
"version": 2
},
"client": {
"name": "keybase.io go client",
"version": "5.0.0"
},
"ctime": 1574709810,
"expire_in": 504576000,
"prev": "ef1aafdb57278ae5b5d5cb2a6190bb25ca834e84d08971bd3dc7e7157b0c227b",
"seqno": 6,
"tag": "signature"
}

with the key ASD7hem_QLOqBdQ_0KOmvxFnqzv1CEV9BqFQw62kXqTpDQo (https://keybase.io/adeana), yielding the signature:

hKRib2R5hqhkZXRhY2hlZMOpaGFzaF90eXBlCqNrZXnEIwEg+4Xpv0CzqgXUP9Cjpr8RZ6s79QhFfQahUMOtpF6k6Q0Kp3BheWxvYWTESpcCBsQg7xqv21cniuW11csqYZC7JcqDToTQiXG9PcfnFXsMInvEIEgR1UEvYVWCDPC+8iPoIXPwr1jXPiBq5O59U9UBghc1AgHCo3NpZ8RAh+fggal1jrRyDxdoEpUJvBDp5/kRyVOOxJpMS7zFKQV0L2uItDggzAatnsrXbyVD5Yg5qE5ZH5Ce5lx5tIOGBahzaWdfdHlwZSCkaGFzaIKkdHlwZQildmFsdWXEIEvAInqTEvLkVSHI4Azv5j9K1z3d8CV0TJRhHjrDQohFo3RhZ80CAqd2ZXJzaW9uAQ==

And finally, I am proving ownership of the github account by posting this as a gist.

My publicly-auditable identity:

https://keybase.io/adeana

From the command line:

Consider the keybase command line program (https://keybase.io/download).

bash

look me up

keybase id adeana

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment