paperless: add state to hostingde invoice fetch script

This commit is contained in:
b12f 2024-11-20 10:10:04 +01:00
parent 01712acef7
commit 196744c4b4
Signed by: b12f
GPG key ID: 729956E1124F8F26
2 changed files with 15 additions and 4 deletions

View file

@ -121,7 +121,7 @@ in {
services.cron = { services.cron = {
enable = true; enable = true;
systemCronJobs = [ systemCronJobs = [
"30 1 * * * paperless ${pkgs.fetch-hostingde-invoices}/bin/fetch-hostingde-invoices '${config.age.secrets."hosting-de-invoice-sync-api-key".path}' '${consumptionDir}'" "30 1 * * * paperless ${pkgs.fetch-hostingde-invoices}/bin/fetch-hostingde-invoices '${config.age.secrets."hosting-de-invoice-sync-api-key".path}' '${consumptionDir}' /var/lib/fetch-hostingde-invoices/ids"
]; ];
}; };
@ -134,6 +134,7 @@ in {
"d '${backupDir}' 0700 paperless users - -" "d '${backupDir}' 0700 paperless users - -"
"d '${consumptionDir}' 0700 paperless users - -" "d '${consumptionDir}' 0700 paperless users - -"
"d /tmp/paperless 0700 paperless users - -" "d /tmp/paperless 0700 paperless users - -"
"d /var/lib/fetch-hostingde-invoices 0700 paperless users - -"
]; ];
age.secrets."rclone-pubsolar.conf" = { age.secrets."rclone-pubsolar.conf" = {
@ -148,7 +149,10 @@ in {
services.restic.backups = { services.restic.backups = {
paperless = { paperless = {
paths = [backupDir]; paths = [
backupDir
"/var/lib/fetch-hostingde-invoices"
];
initialize = true; initialize = true;
passwordFile = config.age.secrets."restic-password".path; passwordFile = config.age.secrets."restic-password".path;
# See https://www.hosting.de/blog/verschluesselte-backups-mit-rclone-und-restic-in-nextcloud/ # See https://www.hosting.de/blog/verschluesselte-backups-mit-rclone-und-restic-in-nextcloud/

View file

@ -2,16 +2,17 @@ self:
with self; '' with self; ''
set -e set -e
PATH="$PATH:${jq}/bin:${curl}/bin" PATH="$PATH:${jq}/bin:${curl}/bin:${coreutils}/bin:${gnugrep}/bin"
SECRET_FILE=$1 SECRET_FILE=$1
OUT_DIR=$2 OUT_DIR=$2
STATE_FILE=$3
AUTH_TOKEN=$(cat $SECRET_FILE) AUTH_TOKEN=$(cat $SECRET_FILE)
CURL_BODY=$(cat <<EOF CURL_BODY=$(cat <<EOF
{ {
"limit": 10, "limit": 30,
"sort": { "sort": {
"field": "documentDate", "field": "documentDate",
"order": "DESC" "order": "DESC"
@ -24,6 +25,11 @@ 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) 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 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 CURL_BODY=$(cat <<EOF
{ {
"id": "$doc", "id": "$doc",
@ -34,5 +40,6 @@ EOF)
echo "fetching $doc"; echo "fetching $doc";
url=$(curl -X POST -H "Content-Type: application/json" -H "Accept: application/json" -d "$CURL_BODY" "$CURL_URL" | jq '.response' -rc) 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" curl $url >> "$OUT_DIR/$doc.pdf"
echo "$doc" >> $STATE_FILE
done done
'' ''