TL;DR: Check out presto-card-cli on GitHub.
If you live in Toronto and use public transportation, you probably heard about PRESTO cards and maybe you have one.
If you wonder what I’m talking about here, PRESTO is an electronic payment system that eliminates the need for tickets, tokens, passes and cash used on participating public transit systems in the province of Ontario, Canada, specifically in Greater Toronto, Hamilton, and Ottawa.
I bought my first card just a few weeks ago and since I don’t buy unlimited passes, I’m interested in my balance and found that it would be cool if I could get it by just typing a command in a terminal.
If you have NodeJS 6+ installed and use macOS, you should be able to get it working without too much hassle by executing
yarn global add presto-card-cli or
npm -g i presto-card-cli.
The tool can provide the balance for multiple PRESTO accounts but it requires to set up them before.
To set up an account, you need to provide your username and password using the
set-credentials command. The tool will make use of the system’s keychain.
That choice was actually driven by presto-card-js, the library that
Unfortunately, PRESTO does not provide any public API, so building
presto-card-js required some reverse engineering to be able to execute the right HTTP requests to login and then to be able to parse the dashboard HTML.
This tool uses node-keytar, a native Node module to get, add, replace, and delete passwords in system’s keychain.
On macOS the passwords are managed by the Keychain, on Linux they are managed by the Secret Service API/libsecret, and on Windows they are managed by Credential Vault.
To mitigate some issues I got using macOS High Sierra with this library, the tool is also using node-keychain as a fallback if it fails on a macOS environment.
node-keytar sounded like an excellent idea at first but since it’s a native module, it seems to make the installation on Windows more complicated.
Currently, it only works if you have PRESTO online account with a registered card and it works only with the default one if you have multiple cards associated with a single account.
If you have only one PRESTO account and don’t want to type
presto-card balance <username> each time, you can create an alias. For example:
alias pb='presto-card balance <username>'. That allows getting your card balance with just two keystrokes.
I’m quite happy with that first version and I can now check my balance just before leaving work by just typing a command in a terminal.
I think that dockerizing this tool to make it more portable would be awesome but managing credentials in container may be tricky. It could also benefit from additional features.
Check out presto-card-cli on GitHub and feel free to submit pull requests to make this even better.