Check your PRESTO card balance in a terminal using presto-card-cli

2017-12-23

TL;DR: Check out presto-card-cli on GitHub.

What?

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.

Here’s come presto-card-cli!

Get started

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.

demo gif

Cool! What happens behind the scenes?

And why?

Why NodeJS?

That may be a legitimate question since it’s probably not the best choice to build a CLI tool that would ideally work on all machines without headaches. Plus, JavaScript is not the language I know the most even if I use it among other ones almost every day.

That choice was actually driven by presto-card-js, the library that presto-card-cli uses as a back end and that was written in JavaScript because its first use case was building a mobile app.

Additionally the fact that there are awesome libraries such as inquirer and commander made NodeJS a good candidate to get the job done in a limited amount of time.

How does it get data from PRESTO?

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.

Credentials management

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.

Using 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.

Limitations and tips

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.

Conclusion

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.