April 1, 2016

Tool to get your Opal card travel data

Filed under: Technical — Tags: , , — James Bunton @ 12:00 am

The New South Wales public transport system uses the Opal smartcard as its payment mechanism. This means their servers store every trip that you make, including the start/end times and locations.

They need this data for billing, so there’s not much we can do about it from a privacy point of view. However you may be interested in getting your own copy of your data for making pretty graphs or other analyses.

Introducing…

Enter opal-card-tool.

Requirements:

  • python3
  • python-lxml
  • python-requests
  • gnuplot (optional for graph support)
  • An account on www.opal.com.au

Example usage

$ opal-card-tool --help
usage: opal-card-tool [-h] [--num-days NUM_DAYS] [--card-number CARD_NUMBER]
                      (--load | --show-cards | --show-transactions | --graph-commuter)

Opal card activity fetcher

optional arguments:
  -h, --help            show this help message and exit
  --num-days NUM_DAYS   restrict to NUM_DAYS of output
  --card-number CARD_NUMBER
                        Opal card number or index (eg: 0,1,etc
  --load                load any new data from the Opal website
  --show-cards          show a list of cards
  --show-transactions   show transactions for card
  --graph-commuter      draw commuter graph for card with gnuplot

$ opal-card-tool --load
Username: example
Password: example
Loading transactions for 1234567890123456 . done

$ opal-card-tool --show-transactions|head -n2
number,timestamp,amount,details,fare,fare_applied,fare_discount,journey_number,mode
1234,2016-01-01 09:00:00,-$4.50,Some St to Some Other St,$4.50,,$0.00,0,bus

Other interesting things

Each time you run opal-card-tool --load it fetches any new transactions from Opal and adds them to ~/.cache/opal-card-tool/pickle. Your username and password are also saved here, so keep that file safe! The tool does not connect to the internet unless you ask it to load additional data with this flag.

You can easily get a CSV of your card data for easy analysis with a spreadsheet or other software:
opal-card-tool --card-number XX --show-transactions > transactions.csv

There’s also the --graph-commuter option. I use this to see how long I spend at work.

Leave a Reply

Your email address will not be published. Required fields are marked *