Automating Business Mileage with Google Timeline

Holy crap, like five posts and zero code! What does this guy actually do? ajgreen.never.codes amiright!? Ok…I’ll post about writing code.

We recently purchased several vending machines, side hustle, and this requires a fair amount of driving. In order to stay on the right side of the IRS mileage needs to be tracked and even though there are hundreds of apps and services that do this for you already using a fully fleshed out solution wouldn’t leave me with a side project to forget about in a few months so I’ve decided to build something myself! I kicked around developing an app to track these for me, but then I have moment of clarity and realized the all-mighty GOOGs is tracking this info for me already (CRY LOL) so all I need is a script that accepts an input file, parses the file and provides the date, time and distance driven. These, along with a business purpose, are required by the Feds to reduce audit liklihood so this is what I’ll extract. Mistake making time, It’s a pickle!

Assuming you’re logged in then getting to your daily data from Google is shockingly easy. By visiting your timeline one can use the date/time selector to traverse travels and via the little gear in the bottom right corner the grossest XML file you will ever see a KML file can be exported.

KML, Keyhole Markup Language, is a file format used to tag IRL Earth location data for use by various Google services. For our purposes coupling that format with PyKML will give us a simple script to run via terminal. The code is available here and relies on a local file to run. Rudimentarily I’m capturing the location data tagged as a drive and then outputting details from the representative objects all the while unncessarily peppering Regular Expressions in. The resulting strings are powerfully monochrome and underwhelming.

Well, that was a simple and fun way to spend a few hours. The next steps will be to capture the totals, create a gui wrapper and handling multiple files, but for now this will be it. I can grab a day’s worth of KML and get all I need.