From 0c10a7dbe2753ea63478d2fc19a4fe6a40fe7426 Mon Sep 17 00:00:00 2001 From: Ian Mancini Date: Sat, 2 May 2020 18:26:21 -0300 Subject: [PATCH] Add weather scripts --- .weather-schema | 3 ++ weather-get | 118 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 121 insertions(+) create mode 100644 .weather-schema create mode 100755 weather-get diff --git a/.weather-schema b/.weather-schema new file mode 100644 index 0000000..c9fecc0 --- /dev/null +++ b/.weather-schema @@ -0,0 +1,3 @@ +#!/bin/sh + +export SCHEMA="Ciudad;Fecha;Hora;Estado;Visibilidad;Temperatura;Sensacion;Humedad;Viento;Presion" diff --git a/weather-get b/weather-get new file mode 100755 index 0000000..87e957c --- /dev/null +++ b/weather-get @@ -0,0 +1,118 @@ +#!/bin/sh + +set -Eeuo pipefail + +source ./.weather-schema +FIELD_COUNT=$(echo $SCHEMA | grep -o ";" | wc -l) +URL=https://ssl.smn.gob.ar/dpd/zipopendata.php?dato=tiepre +TMPDIR=/tmp/weather +OUT=$HOME/.weather +CITY="La Plata" + +print=false + +help() { + cat <&2 + help + exit 1 + ;; + esac +done + +# Create dir if not exists +if [ ! -d /tmp/weather ]; then + mkdir -p $TMPDIR +fi + +# Get the dataset +curl -s "$URL" --output $TMPDIR/weather.zip +unzip -q $TMPDIR/weather.zip -d $TMPDIR + +# Sanitize +iconv -f latin1 -t utf8 $TMPDIR/*.txt | tr -d ' ' > $TMPDIR/final.txt + +# Cut city data +data=$(cat $TMPDIR/final.txt | grep -i "$CITY" || echo "notfound") + +if [ "$data" = "notfound" ]; then + echo "Error: City/Weather station not found" + cleanup + exit 1 +fi + +# Process string +processed="" +for (( i = 0; i <= $FIELD_COUNT; i++ )); do + field_number=$(($i +1)) + field=$(echo $SCHEMA | cut -d ";" -f $field_number) + value=$(echo $data | cut -d ";" -f $field_number) + + line="${field}=$(echo ${value/\//} | sed -e 's/^[ ]*//')" + + if [ ! $i = $FIELD_COUNT ]; then + processed="${processed}${line}\n" + else + processed="${processed}${line}" + fi +done + +# Print or save? +if [ "$print" = true ]; then + echo -e "$processed" +else + rm $OUT + echo -e "$processed" >> $OUT +fi + +# Cleanup +cleanup