Command line time tracking with Python and the Intervals API

John Reeve | March 1st, 2016 | , , , ,

Command line time tracking with Python

I ride my bike to work daily and log my mileage using a GPS and Strava. I ride the exact same route to work in the morning, and the same route home in the evening. A few months ago, the tedium of tracking every single commute was starting to take the fun out of it.

The web developer in me knew there had to be an easier way to track my mileage. I exported the data from one of my rides and started tinkering. I was able to write a Python code script that parses the data file, changes the date, and posts it to Strava. Problem solved. I run the script once for each day I ride. I’m back to enjoying my commute. 

If us Web developers can write code to simplify our daily responsibilities, we’ll do it. And so I got to thinking, why not simplify tracking my time by writing some Python code to do it for me?

The code I wrote submits time entries to our online time tracking software, Intervals, using the RESTful API. All that’s needed is an Intervals account and a command line. I simply execute the script, passing it three parameters — task number, hours, and a description.

Here’s what it looks like when it runs:
[jreeve@g1sb ~]# ./tracktime.py 20867 .25 "meeting"
Time entry created on task #20867: Design meetings

And here is the Python code script:

#!/usr/bin/python

import requests
import sys
import datetime
import json

url = "https://api.myintervals.com"
token = "xyz"
headers={"Accept":"application/json"}

localid = sys.argv[1]
time = sys.argv[2]
if len(sys.argv) > 3:
    description = sys.argv[3]
else:
    description = ''

today = str(datetime.date.today())
worktypeid = 384
billable = 1

#query task resource for id
response = requests.get(url + "/task/?localid=" + localid, auth=(token, ""), headers=headers)
taskid = response.json()["task"][0]["id"]
tasktitle = response.json()["task"][0]["title"]

#query me resource for personid
response = requests.get(url + "/me/", auth=(token, ""), headers=headers)
personid = response.json()["personid"]

#query time resource to add time
payload = {"taskid":taskid, "personid":personid, "date":today, "time":time, "worktypeid":worktypeid, "billable":billable, "description":description}
response = requests.post(url + "/time/", auth=(token, ""), data=json.dumps(payload), headers={"Content-type":"application/json"})
code = response.json()["code"]
if code == 201:
    print "Time entry created on task #" + localid + ": " + tasktitle
else:
    print "ERROR"
    print response.text

Note: The worktypeid will need to be populated with a value that corresponds to a valid worktype on your Intervals account.

If you are looking to leverage your web development skills to employ command line time tracking, try it out. Copy the code above and use it as a starting point. Don’t have an Intervals account? Create one here.

Leave a Reply

Intervals Blog

A collection of useful tips, tales and opinions based on decades of collective experience designing and developing web sites and web-based applications.

What is Intervals?

Intervals is online time, task and project management software built by and for web designers, developers and creatives.
Learn more…

John Reeve
Author Profile
John Reeve

John is a co-founder, web designer and developer at Pelago. His blog posts are inspired by everyday encounters with designers, developers, creatives and small businesses in general. John is an avid reader and road cyclist.
» More about John
» Read posts by John

Jennifer Payne
Author Profile
Jennifer Payne

Jennifer is the Director of Quality and Efficiency at Pelago. Her blog posts are based largely on her experience working with teams to improve harmony and productivity. Jennifer is a cat person.
» More about Jennifer
» Read posts by Jennifer

Michael Payne
Author Profile
Michael Payne

Michael is a co-founder and product architect at Pelago. His contributions stem from experiences managing the development process behind web sites and web-based applications such as Intervals. Michael drives a 1990 Volkswagen Carat with a rebuilt 2.4 liter engine from GoWesty.
» More about Michael
» Read posts by Michael

help.myintervals.com
Videos, tips & tricks