-
Notifications
You must be signed in to change notification settings - Fork 2
/
gerar-csv.sh
executable file
·63 lines (55 loc) · 2.18 KB
/
gerar-csv.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/bin/bash -x
JSON=portal.json
CSV_HIST=covid19-br.csv
updatedAt=last-update.txt
proxy="http://201.91.82.155:3128"
echo "Fazendo download do arquivo JSON"
curl -# --proxy $proxy "https://qd28tcd6b5.execute-api.sa-east-1.amazonaws.com/prod/PortalGeral" -H "X-Parse-Application-Id: unAFkcaNDeXajurGB7LChj8SgQYS2ptm" --compressed -o $JSON
[ $(jq -r '.results[0].updatedAt' $JSON) == $(cat $updatedAt) ] && cat $updatedAt && exit 0
PORTAL_ARQUIVO=$(jq -r '.results[0].arquivo.name' $JSON)
echo "Fazendo download do arquivo"
curl -f -# -O --proxy $proxy -J $(jq -r '.results[0].arquivo.url' $JSON)
[ $? -ne 0 ] && exit 1
[ -f "$CSV_HIST.gz" ] && gzip -d -c $CSV_HIST.gz > $CSV_HIST
case "${PORTAL_ARQUIVO##*.}" in
"xlsx")
echo "Convertendo o XLSX em CSV"
xlsx2csv -f "%Y-%m-%d" $PORTAL_ARQUIVO $PORTAL_ARQUIVO.csv
DELETE_XLSXCSV=1
if [[ $PORTAL_ARQUIVO == *"HIST"* ]]; then
echo "> HIST"
cat $PORTAL_ARQUIVO.csv > $CSV_HIST
else
echo "> HOJE"
if [[ $(head -1 $CSV_HIST) == $(head -1 $PORTAL_ARQUIVO.csv) ]]; then
tail -n+2 $PORTAL_ARQUIVO.csv >> $CSV_HIST
else
echo "> O cabeçalho dos arquivos CSV estão diferentes. Não será possível mesclar conteúdo no arquivo principal"
DELETE_XLSXCSV=0
fi
fi
[ $DELETE_XLSXCSV -eq 1 ] && rm $PORTAL_ARQUIVO.csv
;;
"csv")
echo "Substituindo o arquivo CSV"
cat $PORTAL_ARQUIVO > $CSV_HIST
rm $PORTAL_ARQUIVO
;;
"rar" | "zip" | "7z")
echo "Descompactando o arquivo"
7z x -aoa $PORTAL_ARQUIVO
mv $(find . -maxdepth 1 -name "HIST*.csv" -print | sort | head -1) $CSV_HIST
tmpcsv=$(find . -maxdepth 1 -name "HIST*.csv")
tail -n+2 -q $tmpcsv >> $CSV_HIST
rm $tmpcsv
;;
*)
echo "Pela 301a vez no ano, o Ministério da Saúde mudou o formato do dado disponibilizado"
exit 1
;;
esac
sha1sum -c --status "$CSV_HIST.sha1"
[ $? -ne 0 ] && sha1sum "$CSV_HIST" > $CSV_HIST.sha1 && gzip -f $CSV_HIST
[ -f "$CSV_HIST" ] && rm -rf $CSV_HIST
jq -r '.results[0].updatedAt' $JSON > $updatedAt
exit 0