wallos-fetcher/wallos-fetch.sh

124 lines
3.7 KiB
Bash
Executable File
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
VERSION="2025.05"
HOST="https://your-api-host.com"
ENDPOINT="/api/subscriptions/get_subscriptions.php"
API_KEY="${API_KEY:-$WALLOS_API_KEY}"
MODE="md"
FULL=false
OUTPUT=""
TMP_FILE=$(mktemp)
# 🚨 Dependencies check
for dep in curl jq; do
command -v "$dep" >/dev/null 2>&1 || {
echo "❌ Missing required dependency: $dep"
exit 1
}
done
show_help() {
cat << EOF
Wallos Fetcher Script - v$VERSION
Usage: ./wallos-fetch.sh [OPTIONS]
Options:
--json Output raw JSON
--md Output Markdown table
--csv Output CSV table
--full Include more fields
--output <file> Custom output filename
--help Show this help message
--version Show version
EOF
}
parse_args() {
while [[ "$#" -gt 0 ]]; do
case "$1" in
--json) MODE="json" ;;
--md) MODE="md" ;;
--csv) MODE="csv" ;;
--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 output filename
[[ -z "$OUTPUT" ]] && OUTPUT="subscriptions.${MODE}"
}
fetch_data() {
URL="${HOST}${ENDPOINT}?api_key=${API_KEY}"
RESPONSE=$(curl -s -w "\nHTTP_STATUS:%{http_code}" "$URL")
STATUS=$(echo "$RESPONSE" | sed -n 's/^HTTP_STATUS://p')
BODY=$(echo "$RESPONSE" | sed '/^HTTP_STATUS:/d')
echo "$BODY" > "$TMP_FILE"
if [[ "$STATUS" != "200" ]]; then
echo "❌ API request failed with status: $STATUS"
rm -f "$TMP_FILE"
exit 1
fi
}
output_json() {
mv "$TMP_FILE" "$OUTPUT"
echo "💾 Saved JSON to $OUTPUT"
}
output_markdown() {
{
echo "# 🧾 Active Subscriptions Overview"
echo "_Last updated: $(date +'%Y-%m-%d')_"
echo ""
} > "$OUTPUT"
if $FULL; then
echo "| ID | Name | Price | Currency | Next Payment | Cycle | Auto Renew | Category | Payment Method |" >> "$OUTPUT"
echo "|----|------|-------|----------|---------------|-------|-------------|----------|----------------|" >> "$OUTPUT"
jq -r '.subscriptions[] | [.id, .name, .price, .currency_id, .next_payment, .cycle, .auto_renew, .category_name, .payment_method_name] | @tsv' "$TMP_FILE" |
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, .next_payment, .category_name, .payment_method_name] | @tsv' "$TMP_FILE" |
while IFS=$'\t' read -r id name price next cat pay; do
echo "| $id | $name | $price | $next | $cat | $pay |" >> "$OUTPUT"
done
fi
echo "💾 Saved Markdown to $OUTPUT"
rm -f "$TMP_FILE"
}
output_csv() {
if $FULL; 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' "$TMP_FILE" >> "$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' "$TMP_FILE" >> "$OUTPUT"
fi
echo "💾 Saved CSV to $OUTPUT"
rm -f "$TMP_FILE"
}
# 🏁 Main
parse_args "$@"
fetch_data
case "$MODE" in
json) output_json ;;
md) output_markdown ;;
csv) output_csv ;;
esac