Skip to content

Commit 77aa9b7

Browse files
committed
ottimizzazione dello script: semplificata l'estrazione dei dati e migliorata la gestione dei file
1 parent 3535b66 commit 77aa9b7

1 file changed

Lines changed: 7 additions & 9 deletions

File tree

ondate-calore.sh

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,28 +43,27 @@ else
4343
fi
4444

4545
# estrai data aggiornamento dichiarata sul sito
46-
<"${folder}"/processing/output.html scrape -e "//*[contains(text(), 'Ultima Versione Aggiornata')]" | grep -oP '\d.+\d' >"${folder}"/processing/check
47-
46+
<"${folder}"/processing/output.html scrape -e "//*[contains(text(), 'Ultima Versione Aggiornata')]" | grep -oP '\d.+\d' >"${folder}"/processing/check
4847

4948
# if file ${folder}/processing/check is equal to ${folder}/data/check, then exit
5049
if cmp -s "${folder}"/processing/check "${folder}"/data/check; then
5150
echo "La data di aggiornamento non è cambiata. Lo script verrà interrotto."
5251
else
5352
mv "${folder}"/processing/check "${folder}"/data/check
54-
# estrai header, e converti data in formato ISO
55-
<"${folder}"/processing/output.html scrape -be '//table/thead' | xq -r '.html.body.thead.tr.td[]."#text"' | paste -sd, | sed -E 's#([0-9]{2})/([0-9]{2})/([0-9]{4})#\3-\2-\1#g' >"${folder}"/processing/ondate-calore.csv
53+
# estrai header
54+
<"${folder}"/processing/output.html scrape -be '//table/thead' | xq -r '.html.body.thead.tr.td[]."#text"' | paste -sd, >"${folder}"/processing/ondate-calore.csv
5655

5756
# estrai dati
58-
<"${folder}"/processing/output.html scrape -be '//table/tbody/tr' | xq -c '.html.body.tr[]|{name:.td[0].a."#text",prima:.td[1].span."@class",seconda:.td[2].span."@class",terza:.td[3].span."@class"}' | mlr --j2c cat | tail -n +2 >>"${folder}"/processing/ondate-calore.csv
57+
<"${folder}"/processing/output.html scrape -be '//table/tbody/tr' | xq -c '.html.body.tr[]|{name:.td[0].a."#text",prima:.td[1].span."@class",seconda:.td[2].span."@class",terza:.td[3].span."@class"}' | mlr --j2c cat | tail -n +2 >>"${folder}"/processing/ondate-calore.csv
5958

6059
# estrai URL PDF
61-
<"${folder}"/processing/output.html scrape -be '//table/tbody/tr' | xq -c '.html.body.tr[]|{name:.td[0].a."#text",URL:.td[0].a."@href"}' | mlr --j2c label citta,URL then put '$URL=sub($URL,"http:","https:")' >"${folder}"/data/ondate-calore_PDF.csv
60+
<"${folder}"/processing/output.html scrape -be '//table/tbody/tr' | xq -c '.html.body.tr[]|{name:.td[0].a."#text",URL:.td[0].a."@href"}' | mlr --j2c label citta,URL then put '$URL=sub($URL,"http:","https:")' >"${folder}"/data/ondate-calore_PDF.csv
6261

6362
# URL completo per i PDF
6463
mlr -I --csv put '$URL="https://www.salute.gov.it".$URL' "${folder}"/data/ondate-calore_PDF.csv
6564

6665
# trasforma struttura dati da wide a long
67-
mlr --csv --from "${folder}"/processing/ondate-calore.csv label citta then reshape -r "[0-9]" -o data,livello then sort -r data -f citta then put '$data_estrazione="'"$data"'"' >"${folder}"/data/ondate-calore_latest.csv
66+
mlr --csv --from "${folder}"/processing/ondate-calore.csv label citta then reshape -r "[0-9]" -o data,livello then sort -r data -f citta then put '$data_estrazione="'"$data"'"' >"${folder}"/data/ondate-calore_latest.csv
6867

6968
# aggiungi URL PDF
7069
mlr --csv join --ul -j citta -f "${folder}"/data/ondate-calore_latest.csv then unsparsify "${folder}"/data/ondate-calore_PDF.csv >"${folder}"/processing/tmp.csv
@@ -123,7 +122,7 @@ if($delta_giorno_dopo>0) {
123122
' "${folder}"/processing/tmp.csv >"${folder}"/elaborazioni/ondate-calore_oggi.csv
124123

125124
# estrai le coordinate geografiche delle città per il join successivo
126-
mlr --csv cut -f citta,latitude "${folder}"/data/citta-anagrafica.csv > "${folder}"/processing/latitude.csv
125+
mlr --csv cut -f citta,latitude "${folder}"/data/citta-anagrafica.csv >"${folder}"/processing/latitude.csv
127126

128127
# aggiungi le coordinate geografiche all'archivio pulito
129128
mlr --csv join --ul -j citta -f "${folder}"/elaborazioni/ondate-calore_archivio_clean.csv then unsparsify then sort -f citta,data "${folder}"/processing/latitude.csv >"${folder}"/processing/tmp.csv
@@ -133,4 +132,3 @@ rm "${folder}"/processing/latitude.csv
133132

134133
# rinomina il file
135134
mv "${folder}"/processing/tmp.csv "${folder}"/elaborazioni/ondate-calore_archivio_clean.csv
136-

0 commit comments

Comments
 (0)