Scripting Habitica with Python

Posted on Posted in Uncategorized

Scripting Habitica with Python

Like a lot of people working in IT, I'm always curious to try new productivity tools and see if they help me be more organized and productive. One site that I have found helps me be more together is "Habitica", which allows you to list both daily habits and one off tasks like a quest menu, and gain virtual experience for ticking them off (and virtual level ups). Personally I think it's the best online todo list out there, and would probably be a hit even without the XP bar and "boss battle" functionality. Additionally there's a fairly good app for iOS available that has the same functionality as the web interface.

One other important thing.... it also has a public API and is scriptable. So for example you could expose an online tool that added tasks onto your daily checklist automatically when clients send an email or place an order. Or if you wanted to stick to a fitness schedule, but found the daily tasks to be too inflexible for the schedule you wanted to use.  In my case, I wanted to have schedule of slowly increasing difficulty that broke up tasks automatically past a certain threshold. The API can be a bit vague on how to actually send a query, so I've included my code here for anyone who wants to see an example of how to send a command that adds a todo.

import json
import requests
import path
import pickle
import os

intCounter = 20
intBreak = 40

strFilename = os.path.expanduser('~') + "workout.temp"

if os.path.isfile(strFilename):
    with open(strFilename, 'r') as f:
        intCounter = pickle.load(f)

intTempCounter = intCounter

while intTempCounter > 0:
    block = 0
    if intTempCounter >= intBreak:
        block = intBreak
        intTempCounter -= intBreak
    else:
        block = intTempCounter
        intTempCounter = 0
    dctJson = {"text":"Pushups - " + str(block), "type":"todo"}
    headers = {'x-api-user': '<user>', 'x-api-key': '<key>'}
    r = requests.post('https://habitica.com/api/v3/tasks/user', headers=headers, json=dctJson)
    print r.status_code
    print 'Workout Counter = ' + str(block)
        
intCounter += 1
with open(strFilename, 'wb') as f:
    pickle.dump(intCounter, f)

If you're curious my interpreter is the standard Anaconda python 2.7 installation available here. There will be a follow up post on automating power-shell commands in Windows, though if you're running Linux this could easily be solved via a cron job.