{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Step 1 - Downloading Fitbit data via the API\n", "\n", "For this initial step, we are using [python-fitbit](https://github.com/orcasgit/python-fitbit), a Python client accessing the Fitbit API. We furthermore require an exisiting Fitbit OAuth 2.0 Client (Consumer) ID and Client (Consumer) Secret. These can be obtained by registering an app [here](https://dev.fitbit.com/apps/new).\n", "\n", "Let's make sure that all Python dependencies needed below can be loaded:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import fitbit # clone https://github.com/orcasgit/python-fitbit\n", "from bin.parse_credentials import parse_client_credentials, parse_tokens # clone https://github.com/JungeAlexander/fitbit-data" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Afterwards, save the Client ID and Client Secret to a text file named `client_id_secret.txt` located in the same directory as this Jupyter notebook. `client_id_secret.txt` should look like this:\n", "\n", "```\n", "id = 123ABC\n", "secret = 1234567890abcdef1234567890abcdef123456789\n", "```\n", "\n", "Load the credentials into two variables using a function shipped with this repository: " ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [], "source": [ "client_id, client_secret = parse_client_credentials('client_id_secret.txt')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Furthermore, API access and refresh tokens are required for python-fitbit to access Fitbit data. These can be obtained using the script `gather_keys_oauth2.py` shipped with python-fitbit.\n", "\n", "Execute the following command to write the output of `gather_keys_oauth2.py` to `access_refresh_tokens.txt`:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [], "source": [ "!./gather_keys_oauth2.py $client_id $client_secret >access_refresh_tokens.txt 2>/dev/null" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now load access and refresh tokens into two variables:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": true }, "outputs": [], "source": [ "access_token, refresh_token = parse_tokens('access_refresh_tokens.txt')" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [], "source": [ "authd_client = fitbit.Fitbit(client_id, client_secret, oauth2=True,\n", " access_token=access_token,\n", " refresh_token=refresh_token)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Finally we can download Fitbit data using the API. Let's look at my sleep and step data from last Sunday as an example:" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "{'dateTime': '2016-03-13', 'value': '351'}" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sleep_ts = authd_client.time_series('sleep/minutesAsleep', period='3m')\n", "sleep_ts['sleep-minutesAsleep'][-7]" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "{'dateTime': '2016-03-13', 'value': '15468'}" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "steps_ts = authd_client.time_series('activities/steps', period='3m')\n", "steps_ts['activities-steps'][-7]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Looks like I didn't sleep too much the night of Saturday to Sunday and I gathered more than 15k steps on Sunday (mostly playing Ultimate Frisbee, I presume)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Next steps and further reading\n", "\n", "Next, I would like to download additional data from Fitbit gathered over a longer period of time, store them in suitable data structures and create some basic visualizations.\n", "\n", "For additional information regarding the Fitbit API and which data can be downloaded, check the [API documentation](https://dev.fitbit.com/docs/basics/). Also the [python-fitbit documentation](http://python-fitbit.readthedocs.org/en/latest/#) is worth a visit." ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.5.1" } }, "nbformat": 4, "nbformat_minor": 0 }