Cookie Notice

As far as I know, and as far as I remember, nothing in this page does anything with Cookies.

2014/02/21

Using Fitbit to keep me moving

I have a FitBit Ultra (which I have discussed before) that I use to keep track of my daily activity, trying to force myself into higher and higher numbers, toward the AHA-suggested 10,000 steps per day.

In all honesty, I have dropped my personal goal from 10,000 steps to 6000, because 6000 is an amount that I can adjust my day to get, while I have to spend hours of my evening walking around to get myself up to 10,000. With 6000, I have a lower barrier, so I don't beat myself up with disappointment as easily. I still do, because I still have <3000-step days, but with 6000, I feel I have a fighting chance.

I have a FitBit but I don't pay for the upgraded API, but I can check every hour. And now I do. 

#!/usr/bin/env python

from oauth import oauth
import datetime 
import httplib
import os
import pymongo
import simplejson as json
import time

import fitbit
import locked
import pushover as p

def main():
    checker = locked.locked()   # determines if screen is locked
    if not checker.is_locked():
        from pymongo import MongoClient # connect to mongodb
        client = MongoClient()          # connect to mongodb
        db = client.fitbit              # connect to fitbit DB 
        reads = db.daily_reads          # connect to collection 
        now = datetime.datetime.now()       # what time is now?
        datestr = now.strftime("%Y-%m-%d")  # now in date format
        isostr  = now.isoformat()           # now in ISO format
        fb = fitbit.make_class()                    # connect 
        fbr = fb.activities_date_json( datestr )    # get updates
        summary         = fbr['summary']            # get summary
        steps  = summary['steps']                   # pull out steps 
        new_read = {}               # create new read
        new_read["date"] = datestr     # date == datestr
        new_read["time"] = isostr      # time == ISO
        new_read["steps"] = steps      # steps is steps to date
        
        if 0 == reads.find( { "date" : datestr } ).count(): # if nada 
            for read in reads.find():       # clear out 
                id = read["_id"]
                reads.remove( { "_id" : id } )
            reads.insert( new_read )        # insert 
        else:
            old_read = reads.find_one()
            delta_steps = new_read["steps"] - old_read["steps"]
            if delta_steps < 100:
                msg = "You took " + str(delta_steps) 
                msg = msg + " steps in the last hour. "
                msg = msg + "Go take a walk."
                pu = p.pushover()
                pu.send_message( msg )
            id = old_read["_id"]
            reads.update( { '_id' : id } , new_read )

if __name__ == '__main__':
    main()

There are three modules that are mine: the fitbit module which comes from my fitbit_tools repository; locked module which uses xscreensaver-command to determine if the screen is locked (if I'm not at my desk, my step counter isn't being updates; and pushover, which uses the Pushover service to keep me informed. I could and maybe should have it do speech with Festival or pop to my screens with Notify or Snarl, which also are desktop-centered tools, but this is what I have.

Similarly, perhaps this task is better suited to a key-value store like Redis than MongoDB's document store. At most I'm having one entry per day and the first entry of a day clears out all previous entries.

But, notwithstanding all the perhaps-suboptimal design decisions, this is an example of how coding and quantifying yourself can help you improve your life.