diff --git a/README.md b/README.md index 9438aa8..5774ed3 100644 --- a/README.md +++ b/README.md @@ -54,6 +54,12 @@ Die Session wird lokal gespeichert in: python main.py download --from 2025-01-01 --to 2025-12-31 ``` +Nur Rechnungen von gestern (Cron-geeignet): + +```powershell +python main.py download --yesterday +``` + ## Docker Compose Container-Image bauen: @@ -109,6 +115,7 @@ Alternative per Hilfsskript: ```bash chmod +x ./startDownload.sh ./startDownload.sh 2025-01-01 2025-12-31 +./startDownload.sh yesterday ``` Persistente Ordner: @@ -131,6 +138,7 @@ Optionen: - `--headless true|false`: Browser sichtbar oder unsichtbar - `--debug`: zeigt, wie viele Detailseiten und Rechnungslinks gefunden werden - `--debug-json [pfad]`: schreibt Laufdetails als JSON (ohne Pfad: Standarddatei) +- `--yesterday`: laedt nur Rechnungen vom Vortag - `configure --locale de-DE --timezone Europe/Berlin --amazon-language de_DE`: erzwingt deutsche Sprache (inkl. `language=de_DE`) und Berliner Zeitzone - `configure --notify-email ... --smtp-host ...`: aktiviert E-Mail-Benachrichtigung bei Session-Ablauf @@ -138,6 +146,14 @@ SMTP kann alternativ auch ueber Umgebungsvariablen gesetzt werden: - `NOTIFY_EMAIL`, `SMTP_HOST`, `SMTP_PORT`, `SMTP_USER`, `SMTP_PASSWORD`, `SMTP_FROM`, `SMTP_STARTTLS`, `SMTP_SSL` +## Beispieldateien + +Im Ordner `examples/` liegen Musterdateien: + +- `examples/config.json.example` (Beispiel fuer `~/.amazon_invoice_downloader/config.json`) +- `examples/.env.example` (SMTP/Notification ENV fuer Docker Compose) +- `examples/crontab.example` (taeglicher Vortag-Download per Cron) + ## Hinweise - Das Skript setzt pro Jahr den Amazon-Filter `timeFilter=year-YYYY` auf `your-orders/orders`, damit Zeitraeume wie Januar 2025 korrekt durchlaufen werden. diff --git a/examples/.env.example b/examples/.env.example new file mode 100644 index 0000000..2b9072a --- /dev/null +++ b/examples/.env.example @@ -0,0 +1,9 @@ +# Optional SMTP/notification settings for docker compose +NOTIFY_EMAIL=stefan.heyn@googlemail.com +SMTP_HOST=smtp.gmail.com +SMTP_PORT=587 +SMTP_USER=YOUR_SMTP_USER +SMTP_PASSWORD=YOUR_SMTP_APP_PASSWORD +SMTP_FROM=stefan.heyn@googlemail.com +SMTP_STARTTLS=true +SMTP_SSL=false diff --git a/examples/config.json.example b/examples/config.json.example new file mode 100644 index 0000000..442341a --- /dev/null +++ b/examples/config.json.example @@ -0,0 +1,19 @@ +{ + "marketplace": "de", + "download_dir": "/downloads", + "headless": true, + "locale": "de-DE", + "timezone": "Europe/Berlin", + "currency": "EUR", + "amazon_language": "de_DE", + "notify_email": "stefan.heyn@googlemail.com", + "smtp": { + "host": "smtp.gmail.com", + "port": 587, + "user": "YOUR_SMTP_USER", + "password": "YOUR_SMTP_APP_PASSWORD", + "from_addr": "stefan.heyn@googlemail.com", + "starttls": true, + "ssl": false + } +} diff --git a/examples/crontab.example b/examples/crontab.example new file mode 100644 index 0000000..21f2b2b --- /dev/null +++ b/examples/crontab.example @@ -0,0 +1,5 @@ +# Beispiel-Cron (taeglich um 03:15 Uhr) fuer Vortag-Download +# Vorher sicherstellen: Session existiert in ./state/storage_state.json +# Optional: ENV-Datei laden, z. B. per source /home/heynst/amazon_invoice_downloader/.env + +15 3 * * * cd /home/heynst/amazon_invoice_downloader && ./startDownload.sh yesterday >> /home/heynst/amazon_invoice_downloader/download-cron.log 2>&1 diff --git a/main.py b/main.py index 1209f59..b2a8fe2 100644 --- a/main.py +++ b/main.py @@ -5,7 +5,7 @@ import re import smtplib import time from dataclasses import dataclass -from datetime import date, datetime +from datetime import date, datetime, timedelta from email.message import EmailMessage from pathlib import Path from typing import Optional @@ -400,8 +400,15 @@ def download(args) -> None: if not STORAGE_STATE_PATH.exists(): raise SystemExit("Session fehlt. Bitte zuerst 'configure' ausfuehren.") - start_date = parse_iso_date(args.date_from) - end_date = parse_iso_date(args.date_to) + if args.yesterday: + yesterday = date.today() - timedelta(days=1) + start_date = yesterday + end_date = yesterday + else: + if not args.date_from or not args.date_to: + raise SystemExit("Bitte entweder --yesterday oder --from und --to angeben.") + start_date = parse_iso_date(args.date_from) + end_date = parse_iso_date(args.date_to) if start_date > end_date: raise SystemExit("'from' muss kleiner/gleich 'to' sein.") @@ -632,8 +639,9 @@ def build_parser() -> argparse.ArgumentParser: p_config.set_defaults(func=configure) p_dl = sub.add_parser("download", help="Rechnungen nach Zeitraum herunterladen") - p_dl.add_argument("--from", dest="date_from", required=True, help="Startdatum YYYY-MM-DD") - p_dl.add_argument("--to", dest="date_to", required=True, help="Enddatum YYYY-MM-DD") + p_dl.add_argument("--from", dest="date_from", help="Startdatum YYYY-MM-DD") + p_dl.add_argument("--to", dest="date_to", help="Enddatum YYYY-MM-DD") + p_dl.add_argument("--yesterday", action="store_true", help="Laedt nur Rechnungen von gestern") p_dl.add_argument("--output", help="Optionales Zielverzeichnis") p_dl.add_argument("--max-pages", type=int, default=25, help="Maximal zu scannende Bestellseiten") p_dl.add_argument("--headless", type=lambda s: s.lower() in {"1", "true", "yes"}, nargs="?", const=True, default=None) diff --git a/startDownload.sh b/startDownload.sh index d28a7c5..03b6957 100755 --- a/startDownload.sh +++ b/startDownload.sh @@ -4,22 +4,34 @@ set -euo pipefail SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" cd "$SCRIPT_DIR" -if [[ $# -lt 2 ]]; then - echo "Usage: $0 [additional args]" - echo "Beispiel: $0 2025-01-01 2025-12-31 --debug --debug-json /downloads/debug-run.json" +if [[ $# -lt 1 ]]; then + echo "Usage: $0 yesterday [additional args]" + echo " oder: $0 [additional args]" + echo "Beispiel: $0 yesterday --debug --debug-json /downloads/debug-run.json" + echo "Beispiel: $0 2025-01-01 2025-12-31" exit 1 fi -DATE_FROM="$1" -DATE_TO="$2" -shift 2 +DATE_MODE="$1" +if [[ "$DATE_MODE" == "yesterday" ]]; then + shift 1 + DATE_ARGS=(--yesterday) +else + if [[ $# -lt 2 ]]; then + echo "Fehlende Datumsparameter. Nutze entweder 'yesterday' oder FROM + TO." + exit 1 + fi + DATE_FROM="$1" + DATE_TO="$2" + shift 2 + DATE_ARGS=(--from "$DATE_FROM" --to "$DATE_TO") +fi mkdir -p state downloads docker compose run --rm amazon-invoice-downloader \ download \ - --from "$DATE_FROM" \ - --to "$DATE_TO" \ + "${DATE_ARGS[@]}" \ --output /downloads \ --headless true \ "$@"