os/pkgs/fetch-hostingde-invoices.nix

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

46 lines
1.1 KiB
Nix
Raw Permalink Normal View History

self:
with self; ''
set -e
PATH="$PATH:${jq}/bin:${curl}/bin:${coreutils}/bin:${gnugrep}/bin"
SECRET_FILE=$1
OUT_DIR=$2
STATE_FILE=$3
AUTH_TOKEN=$(cat $SECRET_FILE)
CURL_BODY=$(cat <<EOF
{
"limit": 30,
"sort": {
"field": "documentDate",
"order": "DESC"
},
"authToken": "$AUTH_TOKEN"
}
EOF)
CURL_URL="https://secure.hosting.de/api/billing/v1/json/documentsFind"
echo "fetching $CURL_URL";
documents=$(curl -X POST -H "Content-Type: application/json" -H "Accept: application/json" -d "$CURL_BODY" "$CURL_URL" | jq '.response.data.[].documentId' -rc)
for doc in $documents; do
if [ $(grep "$doc" "$STATE_FILE" | wc -l) -gt 0 ]; then
echo "Skipping already fetched $doc"
continue
fi
CURL_BODY=$(cat <<EOF
{
"id": "$doc",
"authToken": "$AUTH_TOKEN"
}
EOF)
CURL_URL="https://secure.hosting.de/api/billing/v1/json/accountingDocumentPdfGet"
echo "fetching $doc";
url=$(curl -X POST -H "Content-Type: application/json" -H "Accept: application/json" -d "$CURL_BODY" "$CURL_URL" | jq '.response' -rc)
curl $url >> "$OUT_DIR/$doc.pdf"
echo "$doc" >> $STATE_FILE
done
''