Last active
September 29, 2025 22:48
-
-
Save krydos/62a7c5de05a74606e2c56a3a6e321523 to your computer and use it in GitHub Desktop.
hledger notifications parser
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # each key is a regexp pretty much | |
| # and each value is a template name | |
| templates["PLACE NAME HERE"]="coffee" | |
| templates["COFFEE PLACE"]="coffee" | |
| templates["WOOLWORTHS"]="grocery" | |
| templates["COLES"]="grocery" | |
| templates["DIGITALOCEAN.COM"]="digitalocean" | |
| templates["STEAMGAMES"]="steam" | |
| function get-banks-notifications { | |
| termux-notification-list | jq -r '.[] | select(.key | contains("bankname1") or contains("bankname2")) | (.when + "===" + (.key | gsub("\n"; " ")) + "===" + (.content | gsub("\n"; " ")))' | |
| } | |
| function ledger-check { | |
| IFS=$'\n' | |
| for line in $(get-banks-notifications) | |
| do | |
| # if content is empty then nothing will be appended | |
| # after === so the str is going to end there. | |
| # no content - no need to go further | |
| if [[ "$line" =~ ===$ ]]; then | |
| continue | |
| fi | |
| parts=($(echo "$line" | tr '===' '\n')) | |
| hash=$(echo $line | sha1sum | head -c 40) | |
| content=${parts[2]} | |
| bank_name="bankname1" | |
| #bankname1 | |
| amount=$(echo $content | grep -o -E '\$[0-9.]+\s(paid|sent)' | grep -o -E '[0-9.]+') | |
| if [ -z "$amount" ]; | |
| then | |
| #bankname2 | |
| amount=$(echo $content | grep -o -E '[pP]aid\s\$[0-9.]+\sat' | grep -o -E '[0-9.]+') | |
| bank_name="bankname2" | |
| fi | |
| date=$(echo ${parts[0]} | awk '{print $1}' | sed 's/"//') | |
| datetime=$parts[0] | |
| template="${bank_name}_uncategorised" | |
| for key in "${!templates[@]}"; do | |
| if [[ $content =~ $key ]]; | |
| then | |
| template=${templates[$key]} | |
| fi | |
| done | |
| if [ -n "$amount" ]; | |
| then | |
| if ! cat $LEDGER_FILE | grep -q "$hash"; then | |
| new_record=$(cat $SCRIPT_DIR/ledger-templates/$template | sed "s/{{HASH}}/$hash/" | sed "s/{{DATE}}/$date/" | sed "s/{{AMOUNT}}/$amount/" | sed "s/{{DATETIME}}/$datetime/" | sed "s/{{CONTENT}}/$content/") | |
| echo "" >> $LEDGER_FILE | |
| echo "$new_record" >> $LEDGER_FILE | |
| echo "" >> $LEDGER_FILE | |
| fi | |
| fi | |
| done | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment