Lamapoll_DSGVO_script/lamapoll-DSGVO.sh

79 lines
4.2 KiB
Bash

#! /bin/bash
file_path="/tmp/api.keys"h
if ! test -f $file_path; then
echo "Keine API Keys gefunden. Bitte eine 'api.keys' Datei in /tmp/ ablegen"
exit 1
fi
echo "Im Folgenden werden die Daten abgefragt nach denen gesucht werden soll. Bitte mit ENTER bestätigen."
read -p "Vorname [wird nicht gesucht wenn leer]: " VORNAME
VORNAME=${VORNAME:-}
read -p "Nachname [wird nicht gesucht wenn leer]: " NACHNAME
NACHNAME=${NACHNAME:-}
read -p "Username [wird nicht gesucht wenn leer]: " USERNAME
USERNAME=${USERNAME:-}
read -p "Email [wird nicht gesucht wenn leer]: " EMAIL
EMAIL=${EMAIL:-}
echo "Starte Suche, kann eine Weile dauern"
old_IFS="$IFS"
IFS=$'\n'
for line in $(cat "$file_path"); do
## WMDE-Survey Respondents ("Adressbuch")
rm -f /tmp/survey-$line-adress.json
echo "[]" > /tmp/survey-$line-adress.json
for POLL in $(curl -s -X GET -H "Authorization: Bearer $line" https://app.lamapoll.de/api/v2/polls?limit=1000 | jq -r '.[] | .id'); do
curl -s -X GET -H "Authorization: Bearer $line" https://app.lamapoll.de/api/v2/polls/${POLL}/respondents -s | jq -r '.' > /tmp/survey-$line-adress-$POLL.json
jq '. += (inputs)' /tmp/survey-$line-adress.json /tmp/survey-$line-adress-$POLL.json > temp.json && mv temp.json /tmp/survey-$line-adress.json && rm /tmp/survey-$line-adress-$POLL.json ; done
# filter surveys for values and extract id, pollId, name, email
rm -f /tmp/results-survey-$line-adress.json
echo "[]" > /tmp/results-survey-$line-adress.json
vars=("VORNAME" "NACHNAME" "USERNAME" "EMAIL")
for var_name in "${vars[@]}"; do
value="${!var_name}"
if [[ -n "$value" ]]; then
echo "$var_name = $value"
cat /tmp/survey-$line-adress.json | jq --arg TEST "$value" '.[] | select((.name | test($TEST; "i")) or (.email | test($TEST; "i"))) | {pollId, id, name, email}' > /tmp/survey-$line-adress-$var_name.json
jq '. += [(inputs)]' /tmp/results-survey-$line-adress.json /tmp/survey-$line-adress-$var_name.json > temp.json && mv temp.json /tmp/results-survey-$line-adress.json && rm /tmp/survey-$line-adress-$var_name.json
fi
done
# filter double finds
jq 'sort_by(.pollId, .id) | unique_by(.pollId, .id)' /tmp/results-survey-$line-adress.json > /tmp/tmp.json && mv /tmp/tmp.json /tmp/results-survey-$line-adress.json
## WMDE-Survey Results ("Freitextsuche")
rm -f /tmp/survey-$line-freetext.json
echo "[]" > /tmp/survey-$line-freetext.json
for POLL in $(curl -s -X GET -H "Authorization: Bearer $line" https://app.lamapoll.de/api/v2/polls?limit=1000 | jq -r '.[] | .id'); do
curl -s -X GET -H "Authorization: Bearer $line" https://app.lamapoll.de/api/v2/polls/${POLL}/results -s | jq --arg POLL "${POLL}" -r '. | .pollId = $POLL' > /tmp/survey-$line-freetext-$POLL.json
jq '. += [(inputs)]' /tmp/survey-$line-freetext.json /tmp/survey-$line-freetext-$POLL.json > temp.json && mv temp.json /tmp/survey-$line-freetext.json && rm /tmp/survey-$line-freetext-$POLL.json ; done
cat /tmp/survey-$line-freetext.json | tr -cd '\11\12\40-\176' > /tmp/survey-$line-freetext-removed.json
rm /tmp/survey-$line-freetext.json
mv /tmp/survey-$line-freetext-removed.json /tmp/survey-$line-freetext.json
rm -f /tmp/results-survey-$line-freetext.json
echo "[]" > /tmp/results-survey-$line-freetext.json
vars=("VORNAME" "NACHNAME" "USERNAME" "EMAIL")
for var_name in "${vars[@]}"; do
value="${!var_name}"
if [[ -n "$value" ]]; then
cat /tmp/survey-$line-freetext.json | jq --arg TEST "$value" '.[] | {name: .name, pollId: .pollId, results: .results[] | select(.groups[].labels[] | test($TEST; "i")) | {question: .question, questionId: .questionId, labels: [.groups[].labels[] | select(test($TEST; "i"))]}}' > /tmp/survey-$line-freetext-$var_name.json
jq '. += [(inputs)]' /tmp/results-survey-$line-freetext.json /tmp/survey-$line-freetext-$var_name.json > temp.json && mv temp.json /tmp/results-survey-$line-freetext.json && rm /tmp/survey-$line-freetext-$var_name.json
fi
done
done
IFS="$old_IFS"
echo "Suche beendet. Ergebnisse sind:"
old_IFS="$IFS"
IFS=$'\n'
for line in $(cat "$file_path"); do
cat /tmp/results-survey-$line-adress.json
cat /tmp/results-survey-$line-freetext.json
done
IFS="$old_IFS"