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.