151 lines
4.4 KiB
Plaintext
151 lines
4.4 KiB
Plaintext
|
#!/bin/bash
|
||
|
#
|
||
|
# functions
|
||
|
#
|
||
|
# Copyright 2022 crt0mega <crt0mega@c-r-t.tk>
|
||
|
#
|
||
|
# This program is free software; you can redistribute it and/or modify
|
||
|
# it under the terms of the GNU General Public License as published by
|
||
|
# the Free Software Foundation; either version 2 of the License, or
|
||
|
# (at your option) any later version.
|
||
|
#
|
||
|
# This program is distributed in the hope that it will be useful,
|
||
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
|
# GNU General Public License for more details.
|
||
|
#
|
||
|
# You should have received a copy of the GNU General Public License
|
||
|
# along with this program; if not, write to the Free Software
|
||
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||
|
# MA 02110-1301, USA.
|
||
|
#
|
||
|
#
|
||
|
|
||
|
declare -r _XRepositoryBase="https://www.xrepository.de/api/xrepository"
|
||
|
declare -r _XRepositoryURN="urn:de:bund:destatis:bevoelkerungsstatistik:schluessel:ags/metadaten"
|
||
|
declare -r agsFile=$cacheDir/ags.json
|
||
|
|
||
|
function help()
|
||
|
{
|
||
|
# Ausgabe einer kurzen Hilfe
|
||
|
|
||
|
printf "Syntax: melder [-dafuq]\n\n" >&2
|
||
|
printf " -d [AGS] = Ausgabe aller Warnungen im Dashboard des gespeicherten oder\n" >&2
|
||
|
printf " angegebenen AGS\n" >&2
|
||
|
printf " -a <AGS> = lege AGS (Amtlicher Gemeindeschlüssel) für Warnmeldungen fest\n" >&2
|
||
|
printf " -f <Format> = Ausgabeformat für Warnungen festlegen (Standard: @json)\n" >&2
|
||
|
printf " -u = aktualisiere AGS-liste\n" >&2
|
||
|
printf " -q <regex> = bestimme AGS zu Gemeindenamen\n\n" >&2
|
||
|
printf "Hilfe zu Ausgabeformaten und regulären Ausdrücken bietet die Manpage von jq.\n" >&2
|
||
|
|
||
|
|
||
|
exit 0
|
||
|
}
|
||
|
|
||
|
function chkDeps()
|
||
|
{
|
||
|
# Sind denn alle da?
|
||
|
|
||
|
declare -i err=0
|
||
|
declare -g WGET=$(command -v wget)
|
||
|
declare -g JQ=$(command -v jq)
|
||
|
declare -g XQ=$(command -v xq)
|
||
|
|
||
|
if [ -z $WGET ]; then
|
||
|
printf "\033[1;31mFehler: wget nicht installiert.\033[0m\n" >&2
|
||
|
((err++))
|
||
|
fi
|
||
|
|
||
|
if [ -z $JQ ]; then
|
||
|
printf "\033[1;31mFehler: jq nicht installiert.\033[0m\n" >&2
|
||
|
((err++))
|
||
|
fi
|
||
|
|
||
|
if [ -z $XQ ]; then
|
||
|
printf "\033[1;31mFehler: xq nicht installiert.\033[0m\n" >&2
|
||
|
((err++))
|
||
|
fi
|
||
|
|
||
|
if [ $err -gt 0 ]; then
|
||
|
exit 1
|
||
|
fi
|
||
|
}
|
||
|
|
||
|
function getAGS()
|
||
|
{
|
||
|
# Herunterladen der AGS
|
||
|
# TODO: Bietet die NINA API keine eigene Quelle für AGS an?
|
||
|
|
||
|
declare versionFile=$(mktemp)
|
||
|
declare -ga ags
|
||
|
declare -ga gemeinden
|
||
|
|
||
|
printf "Prüfe XRepository ...\n" >&2
|
||
|
|
||
|
$WGET "$_XRepositoryBase/$_XRepositoryURN" -qO $versionFile
|
||
|
|
||
|
if [ $? -eq 8 ]; then
|
||
|
printf "Download der Amtlichen Gemeindeschlüssel fehlgeschlagen.\n" >&2
|
||
|
exit 1
|
||
|
fi
|
||
|
|
||
|
declare agsLatestDownload=$($XQ $versionFile -x /objektData/dokumenteDerLetztenVersion[$xmlDocIndex]/datei/kennung)
|
||
|
|
||
|
# TODO: Bessere Lösung für das echo-Gefrickel finden
|
||
|
declare agsLatestURN=$(echo $agsLatestDownload|cut -d '/' -f 1)
|
||
|
|
||
|
if [ -f $agsFile ]; then
|
||
|
declare agsLocalURN=$($JQ -r .metadaten.kennung $agsFile)
|
||
|
if ! [ "$agsLatestURN" == "$agsLocalURN" ]; then
|
||
|
printf "Zwischengespeicherte AGS entsprechen nicht den Remote-Daten.\n" >&2
|
||
|
printf "Lokal: $agsLocalURN\nRemote: $agsLatestURN\nStarte Download ...\n" >&2
|
||
|
$WGET "$_XRepositoryBase/$agsLatestDownload" -qO $agsFile
|
||
|
else
|
||
|
printf "Zwischengespeicherte AGS noch aktuell.\n" >&2
|
||
|
fi
|
||
|
else
|
||
|
printf "Keine zwischengespeicherten AGS, lade herunter ...\n" >&2
|
||
|
$WGET "$_XRepositoryBase/$agsLatestDownload" -qO $agsFile
|
||
|
fi
|
||
|
|
||
|
# Aufräumen
|
||
|
rm $versionFile
|
||
|
}
|
||
|
|
||
|
function queryAGS()
|
||
|
{
|
||
|
# Abfrage des übergebenen Gemeindenamens gegen die JSON-Datei
|
||
|
# TODO: Auch hier was besseres als das echo-Gefrickel finden
|
||
|
|
||
|
declare query=\"$(echo -e "$*")\"
|
||
|
|
||
|
if ! [ -f $agsFile ]; then
|
||
|
read -p "Lokaler AGS-Cache nicht vorhanden. Soll er aktualisiert werden? (Y/N)" -N 1 result
|
||
|
printf "\n"
|
||
|
if [ "${result^^}" == "Y" ]; then getAGS; else exit 1; fi
|
||
|
fi
|
||
|
printf "AGS\t\tGemeinde\n---\t\t--------\n"
|
||
|
|
||
|
$JQ -rc ".daten[]|select(.[1]|test("$query"; \"i\"))|@tsv" $agsFile
|
||
|
}
|
||
|
|
||
|
function getGebietsschluessel()
|
||
|
{
|
||
|
# Warnmeldungen werden je Gebietskörperschaft ausgegeben, die JSON-Datei
|
||
|
# enthält jedoch nur Mitgliedsgemeinden und kreisfreie Städte
|
||
|
|
||
|
if ! [ $1 ]; then return; fi
|
||
|
|
||
|
echo "$(echo $1 | cut -c1-5)0000000"
|
||
|
}
|
||
|
|
||
|
function getDashboard()
|
||
|
{
|
||
|
# Gibt das aktuelle Dashboard zum angegebenen AGS zurück
|
||
|
|
||
|
if ! [ $1 ]; then return; fi
|
||
|
|
||
|
declare abfragegebiet=$(getGebietsschluessel $1)
|
||
|
$WGET "https://warnung.bund.de/api31/dashboard/$abfragegebiet.json" -qO - | $JQ -rc "$outputFormat"
|
||
|
}
|