From 92f43ac9d9a582e1f0863754a7c250415d434597 Mon Sep 17 00:00:00 2001 From: Nick Heppler Date: Mon, 21 Apr 2025 21:01:13 -0400 Subject: [PATCH] git commit -m "feat: initial version of Wallos Fetcher script" --- wallos-fetch.sh | 161 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 161 insertions(+) create mode 100755 wallos-fetch.sh diff --git a/wallos-fetch.sh b/wallos-fetch.sh new file mode 100755 index 0000000..e7d38bb --- /dev/null +++ b/wallos-fetch.sh @@ -0,0 +1,161 @@ +#!/bin/bash + +VERSION="2025.04" + +# Configuration +HOST="https://your-api-host.com" +ENDPOINT="/api/subscriptions/get_subscriptions.php" +API_KEY="your_api_key_here" + +# Defaults +MODE="md" +FULL=false +OUTPUT="" # Will set default per mode later + +# Show help +show_help() { + cat << EOF +โ„น๏ธ Wallos Fetcher Script - v$VERSION + +Usage: ./wallos-fetch.sh [OPTIONS] + +Fetch and export subscription data from a Wallos instance. + +Options: + --json Output raw JSON + --md Output Markdown table + --full Include more fields in CSV/Markdown + --output Custom output filename + --help Show this help message and exit + --version Show version and exit + +Default output is Markdown. +EOF +} + +# Argument parser +while [[ "$#" -gt 0 ]]; do + case "$1" in + --json) MODE="json" ;; + --md) MODE="md" ;; + --full) FULL=true ;; + --output) + shift + OUTPUT="$1" + ;; + --help) show_help; exit 0 ;; + --version) echo "โ“ wallos-fetch v$VERSION"; exit 0 ;; + *) echo "Unknown option: $1" && show_help && exit 1 ;; + esac + shift +done + +# Set default filenames if none specified +if [[ -z "$OUTPUT" ]]; then + case "$MODE" in + json) OUTPUT="subscriptions.json" ;; + md) OUTPUT="subscriptions.md" ;; + csv) OUTPUT="subscriptions.csv" ;; + esac +fi + +# Construct the API URL +URL="${HOST}${ENDPOINT}?api_key=${API_KEY}" + +# Perform the request +RESPONSE=$(curl -s -w "\nHTTP_STATUS:%{http_code}" "$URL") +BODY=$(echo "$RESPONSE" | sed -n '1,/^HTTP_STATUS:/p' | sed '$d') +STATUS=$(echo "$RESPONSE" | tr -d '\n' | sed -e 's/.*HTTP_STATUS://') + +echo "HTTP Status: $STATUS" + +if [ "$STATUS" -eq 200 ]; then + echo "$BODY" > temp_response.json + + if [ "$MODE" == "json" ]; then + mv temp_response.json "$OUTPUT" + echo "๐Ÿ’พ Saved JSON to $OUTPUT" + +elif [ "$MODE" == "md" ]; then + # Write markdown header and description + { + echo "# ๐Ÿงพ Active Subscriptions Overview" + echo "" + echo "This document lists all recurring subscriptions tied to this household or individual. It includes costs, renewal dates, categories, and payment methods. This record is provided as a reference for financial planning, digital legacy management, or to assist family members in case of emergencies or estate matters." + echo "" + echo "_Last updated: $(date +'%Y-%m-%d')_" + echo "" + } > "$OUTPUT" + + if [ "$FULL" = true ]; then + echo "| ID | Name | Price | Currency | Next Payment | Cycle | Auto Renew | Category | Payment Method |" >> "$OUTPUT" + echo "|----|------|-------|----------|---------------|-------|-------------|----------|----------------|" >> "$OUTPUT" + jq -r ' + .subscriptions[] | [ + .id, + .name, + (.price | tostring), + .currency_id, + .next_payment, + .cycle, + .auto_renew, + (.category_name | gsub(">"; ">") | gsub("&"; "&")), + .payment_method_name + ] | @tsv + ' temp_response.json | while IFS=$'\t' read -r id name price curr next cycle renew cat pay; do + echo "| $id | $name | $price | $curr | $next | $cycle | $renew | $cat | $pay |" >> "$OUTPUT" + done + else + echo "| ID | Name | Price | Next Payment | Category | Payment Method |" >> "$OUTPUT" + echo "|----|------|-------|---------------|----------|----------------|" >> "$OUTPUT" + jq -r ' + .subscriptions[] | [ + .id, + .name, + (.price | tostring), + .next_payment, + (.category_name | gsub(">"; ">") | gsub("&"; "&")), + .payment_method_name + ] | @tsv + ' temp_response.json | while IFS=$'\t' read -r id name price next cat pay; do + echo "| $id | $name | $price | $next | $cat | $pay |" >> "$OUTPUT" + done + fi + rm -f temp_response.json + echo "๐Ÿ’พ Saved Markdown table to $OUTPUT" + + else + if [ "$FULL" = true ]; then + echo '"id","name","price","currency_id","next_payment","cycle","auto_renew","category_name","payment_method_name"' > "$OUTPUT" + jq -r ' + .subscriptions[] | [ + .id, + .name, + .price, + .currency_id, + .next_payment, + .cycle, + .auto_renew, + .category_name, + .payment_method_name + ] | @csv + ' temp_response.json >> "$OUTPUT" + else + echo '"id","name","price","next_payment","category_name","payment_method_name"' > "$OUTPUT" + jq -r ' + .subscriptions[] | [ + .id, + .name, + .price, + .next_payment, + .category_name, + .payment_method_name + ] | @csv + ' temp_response.json >> "$OUTPUT" + fi + rm -f temp_response.json + echo "๐Ÿ’พ Saved CSV to $OUTPUT" + fi +else + echo "โŒ Request failed. Response not saved." +fi