Friday, January 28, 2011

App store purchases


How can you get information about what apps you have and what you've spent on them?

  • Look at the files in the "Mobile Applications" folder.
  • Print from the Apps view in iTunes (use the Song listing option to get the most information).
  • Use Spentonapps (I'm sure you're very enthusiastic about giving them your email and password so they can comb through your email account.)
  • Do some tedious email-mining.

Naturally, I choose the most tedious method.

I searched my gmail for iTunes receipt messages and applied a label to them. I don't usually store my email locally, so I set up gmail and apple's mail client to IMAP sync only one of my gmail labels (it would take way too long to download all messages). I figured out how to do this thanks to a comment by Matt at StackExchange:
if you have a lot of email, turn on the "Advanced IMAP Controls" Labs feature, then, under "Manage Labels", turn off IMAP access for all the folders except the one you want to download.
Thanks Matt! Once I had searched for the iTunes receipt messages, I applied a label to them and downloaded only those messages.

Next, I selected all the messages and saved them as plain text in a file called "receiptPlainText.txt". Then, some nasty bash:

cat receiptPlainText.txt | grep -v "Order Total:" | grep -B1 -A1 "Report a Problem" >purchasesOnly.txt

b=`cat purchasesOnly.txt | grep -v "Report a Problem"| sed 's/^Free/\$0.00/' | sed 's/, v[^,]*//' | tr -d ","`

echo $b | tr -d "*" | sed 's/ -- /\*/g' | tr "*" "\n" | grep "Seller"| sed 's/ Seller:/HBPH/' | sed 's/ \$\([0-9\.]\{4\}\)/, \1/g' | sed 's/HBPH/,/g' | sed 's/^/\"/' | sed 's/$/\"/' | sed 's/, /\", \"/g' >appPurchases.csv


The breakdown:

grep -v "Order Total:"
  • remove prices that are totals
grep -B1 -A1 "Report a Problem"
  • identify regions of text that are individual purchases

b=`
  • remove newlines by saving the text as a variable
grep -v "Report a Problem"|
  • remove unneeded line
sed 's/^Free/\$0.00/'
  • make line structures comparable
sed 's/, v[^,]*//'
  • remove version info
tr -d ","`
  • remove commas
tr -d "*"
  • remove any asterisks in app names
sed 's/ -- /\*/g'
  • put a marker between records
tr "*" "\n"
  • replace marker with newline
grep "Seller"
  • remove records that aren't apps (all apps have sellers)
sed 's/ Seller:/HBPH/'
  • replace Seller with marker for comma insertion
sed 's/ \$\([0-9\.]\{4\}\)/, \1/g'
  • insert comma before price; remove $
sed 's/HBPH/,/g'
  • replace marker with comma
sed 's/^/\"/' | sed 's/$/\"/' | sed 's/, /\", \"/g'
  • put quotes around each field
As a side note, people get huffy about using cat before a command that can simply take a file name as an argument (the "Useless Use of Cat" award). However, I find it useful when I'm working on commands in a modular way. If I need to move the first grep statement around, I don't need to move the file name too.

I analyzed my data, and wow, I've bought a lot of apps. I couldn't bear to leave the left axis on, sorry.

It would be nice to have the date purchased too.

No comments: