{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Working with the Fitbit API" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "First things first, all credit to [Michael Galarnyk](https://towardsdatascience.com/using-the-fitbit-web-api-with-python-f29f119621ea)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Getting the imports right for Sphinx " ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "import sys, os, pathlib\n", "import pandas as pd\n", "from datetime import datetime\n", "import matplotlib.pyplot as plt\n", "import seaborn as sns\n", "\n", "ROOT_DIR = str(pathlib.Path(os.path.realpath(\"__file__\")).parents[2])\n", "sys.path.insert(0, ROOT_DIR)\n", "\n", "from hfkpy.fitbit.get_data import client\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Grab the tokens" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [], "source": [ "token_path = pathlib.Path(ROOT_DIR) / \"tokens.csv\"\n", "tokens = pd.read_csv(token_path)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Instantiate the Fitbit API client" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "auth2_client = client(tokens)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Intraday Fitbit data" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The [intraday Fitbit data](https://dev.fitbit.com/build/reference/web-api/intraday/get-activity-intraday-by-date/) can be collected down to the second interval. This data includes calories | distance | elevation | floors | steps. To get multiple days of data we can pull intraday and concatenate over our date range of interest." ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"{'activities-heart': [{'dateTime': '2022-04-07', 'value': {'customHeartRateZones': [], 'heartRateZones': [{'caloriesOut': 2876.27486, 'max': 111, 'min': 30, 'minutes': 1405, 'name': 'Out of Range'}, {...\"" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "oneDate = datetime(year=2022, month=4, day=7)\n", "\n", "oneDayData = auth2_client.intraday_time_series(\n", " \"activities/heart\", oneDate, detail_level=\"1min\"\n", ")\n", "\n", "str(oneDayData)[:200] + \"...\"\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### View the data as a dataframe" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " | time | \n", "value | \n", "
---|---|---|
0 | \n", "00:00:00 | \n", "66 | \n", "
1 | \n", "00:01:00 | \n", "62 | \n", "
2 | \n", "00:02:00 | \n", "70 | \n", "
3 | \n", "00:03:00 | \n", "68 | \n", "
4 | \n", "00:04:00 | \n", "63 | \n", "