Skip to content

Instantly share code, notes, and snippets.

@simonmichael
Created March 3, 2018 15:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simonmichael/06eed26bcc85c76d1604373418bc6c58 to your computer and use it in GitHub Desktop.
Save simonmichael/06eed26bcc85c76d1604373418bc6c58 to your computer and use it in GitHub Desktop.
XpenseTracker -> hledger makefile
# XpenseTracker iOS app -> hledger import & reports
#
# Fetch csv files from XpenseTracker iphone app (requires curl, funzip).
# Procedure:
# connect phone and computer to same network
# make csv
# start XpenseTracker app
# enter each XpenseTracker log, as prompted on computer
# (first time: options, set appropriate CSV fields)
# options, export, export
# (if phone shows IP different from above, ctrl-c, adjust IP below, redo)
# computer prints ok and prompts for next log
#
PHONEIP=192.168.0.3
# one per XT log
CSVFILES=\
2017-business.csv \
2017-personal.csv \
# 2018-business.csv \
# 2018-personal.csv \
JOURNALS=\
2017-business.journal \
2017-personal.journal \
# 2018-business.journal \
# 2018-personal.journal \
REPORTS= \
out/2017-business-is.html \
out/2017-business-is-summary.html \
out/2017-personal-is.html \
out/2017-personal-is-summary.html \
out/2017-business-is.csv \
out/2017-business-is-summary.csv \
out/2017-personal-is.csv \
out/2017-personal-is-summary.csv \
REPORTOPTS=-MAT --flat --drop 1
REPORTOPTSSUMMARY=$(REPORTOPTS) depth:2
.PHONY: $(CSVFILES) $(JOURNALS) $(REPORTS)
default: journals reports open
# FETCH CSV
csv: $(CSVFILES)
CURLOPTS=-s #--connect-timeout 1
# Fetch, unzip, clean space-terminated account names, report success/failure
$(CSVFILES):
@echo "waiting for $(@:.csv= log) from $(PHONEIP) ..."
@(curl $(CURLOPTS) 'http://$(PHONEIP):8080/XT Export.zip' | funzip >$@.tmp 2>/dev/null && echo ok || (echo failed; false)) \
&& (mv $@.tmp $@ && perl -pi -e 's/ :/:/' $@) \
|| rm -f $@.tmp
# CONVERT ALL TXNS TO JOURNAL
journals: $(JOURNALS)
# recreate journal from csv, and include corresponding accounts file
$(JOURNALS):
[[ -x $@ ]] || printf "# converted from $(@:.journal=.csv)\n\ninclude $(@:.journal=.accounts)\n\n" > $@
hledger -f $(@:.journal=.csv) print >> $@
# IMPORT NEW TXNS TO JOURNAL
# # make 2018-business-import: import new csv txns to this journal
# %-import:
# hledger import -f $*.journal $*.csv
# # YEAR-import-dry: convert YEAR csv and show new transactions
# %-import-dry:
# hledger import -f $*.journal $**.csv --dry-run
# # YEAR-import-dry-watch: convert YEAR csv and show first N new transactions, repeatedly
# %-import-dry-watch:
# ls $**.csv* | entr bash -c "echo; date; echo; hledger import -f $*.journal $**.csv --dry-run | head -20"
# # YEAR-import-unknowns-watch: convert YEAR csv and show uncategorised new transactions, repeatedly
# %-import-unknowns-watch:
# ls $**.csv* | entr bash -c "echo; date; echo; hledger import -f $*.journal $**.csv --dry-run | hledger -I -f- print unknown"
# GENERATE CSV REPORTS FROM CSV
# # Generate per-log csv income statement reports from csv and open in Numbers.
# reports-from-csv: \
# 2017-business-monthly-is.csv \
# 2017-personal-monthly-is.csv \
# 2017-%-monthly-is.csv: #2017-$*.csv
# hledger -f 2017-$*.csv bal -M ^income ^expenses depth:2 -o $@ && open -a Numbers $@
# GENERATE CSV REPORTS FROM JOURNAL
# # Generate csv income statement reports from journals and open in Numbers.
# %-is.csv: #$*.journal
# hledger -f $*.journal is -M depth:2 -o $@ && open -a Numbers $@
# # hledger -f $*.journal bal ^income ^expenses -M depth:2 -o $@ && open -a Numbers $@
# GENERATE REPORTS FROM JOURNAL
reports: $(REPORTS)
# Generate per-year html income statement reports from journals
#$(REPORTS):
out/2017-business-is.html \
out/2017-personal-is.html:
hledger -f $(subst out/,,$(@:-is.html=.journal)) is -o $@ $(REPORTOPTS)
#%-summary.html:
out/2017-business-is-summary.html \
out/2017-personal-is-summary.html:
hledger -f $(subst out/,,$(subst -is-summary.html,.journal,$@)) is -o $@ $(REPORTOPTSSUMMARY)
out/2017-business-is.csv \
out/2017-personal-is.csv:
hledger -f $(subst out/,,$(@:-is.csv=.journal)) is -o $@ $(REPORTOPTS)
out/2017-business-is-summary.csv \
out/2017-personal-is-summary.csv:
hledger -f $(subst out/,,$(subst -is-summary.csv,.journal,$@)) is -o $@ $(REPORTOPTSSUMMARY)
# BROWSE HTML REPORTS
open:
open $(REPORTS)
# MISC
clean:
rm -f $(JOURNALS)
rm -f $(REPORTS)
@lestephane
Copy link

Is XpenseTracker still your goto app for expense tracking?

@simonmichael
Copy link
Author

I tried it out for a little while only. I've never used the phone consistently for expense tracking. Once in a while when it's the best option for remembering eg cash transactions till I get to the laptop, I'll jot them down in the notepad.

@lestephane
Copy link

My workflow is to shoot a photo on the spot, 'forward' it to a CASH YYYY-MM-DD Note in iOS Notes (you can select an existing note when forwarding, making it possible to have one note with multiple photos), and later for each day/note, create a PDF which I can put in my cash statements directory. Since I use hledger-flow, there are some extension points for additional steps (OCR, extracting PDF images into individual images again, etc..). I still have to look at each image and type the entries manually in a CSV file, but I'm hoping to use Google Cloud Vision OCR to help with that later as well, just haven't found an easy command-line way to interface with it.

@simonmichael
Copy link
Author

simonmichael commented Jun 6, 2019 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment