Summary

Use python-fitbit to interact with the Fitbit API and get sleep and intraday activity data. Store as json files in data/raw.


In [1]:
%load_ext pypath_magic

In [2]:
%pypath -a /Users/rbussman/Projects/sleep-bit


UsageError: '/Users/rbussman/Projects/sleep-bit' is already in the user path.

In [3]:
from src.data import get_fitbit
import matplotlib.pyplot as plt
%matplotlib inline

import seaborn as sns
sns.set_context('poster')

import pandas as pd
import time

In [4]:
daterange = pd.date_range('2017-03-30', '2017-08-10')

Intraday steps


In [5]:
# fitbit limits API calls to 150 per hour
rate_limit = 140
api_calls = 0
for date in daterange:
    fitbit_data = get_fitbit.FitbitData(date, 'intraday')
    sleep_log = fitbit_data.download_from_fitbit()
    fitbit_data.write_to_disk(sleep_log)
    api_calls += 1
    if api_calls > rate_limit:
        print("We've exceeded the fitbit rate limit. Pausing for 1 hour.")
        time.sleep(3600)
        api_calls = 0

Sleep logs


In [5]:
# fitbit limits API calls to 150 per hour
rate_limit = 140
api_calls = 0
for date in daterange:
    fitbit_data = get_fitbit.FitbitData(date, 'sleep')
    sleep_log = fitbit_data.download_from_fitbit()
    fitbit_data.write_to_disk(sleep_log)
    api_calls += 1
    if api_calls > rate_limit:
        print("We've exceeded the fitbit rate limit. Pausing for 1 hour.")
        time.sleep(3600)
        api_calls = 0

In [ ]: