From 22b1316d52f2db14e3a7f89e5acf47b2fb1077fe Mon Sep 17 00:00:00 2001 From: Ruchika K Date: Fri, 11 Jun 2021 14:31:45 -0500 Subject: [PATCH] Commit with working code using the fitbit sdk Signed-off-by: Ruchika K --- main.ipynb | 165 ++++++++++++++++++ main_gathertokens.ipynb | 116 ++++++++++++ .../.ipynb_checkpoints/base-checkpoint.txt | 3 + requirements/base.txt | 1 + 4 files changed, 285 insertions(+) create mode 100644 main.ipynb create mode 100644 main_gathertokens.ipynb create mode 100644 requirements/.ipynb_checkpoints/base-checkpoint.txt diff --git a/main.ipynb b/main.ipynb new file mode 100644 index 0000000..ab9e2fb --- /dev/null +++ b/main.ipynb @@ -0,0 +1,165 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "03a32ee1-ac32-41f8-a6f9-a68713dee09b", + "metadata": {}, + "source": [ + "### Obtain tokens and ID \n", + "\n", + "ID and Secret from your fitbit portal (application registration page)\n", + "\n", + "Tokens are obtained by running the notebook main_gatheringtokens.ipynb" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "id": "a5d85b83-f34a-4a7a-b2f6-91b10b670802", + "metadata": {}, + "outputs": [], + "source": [ + "# This is a python file you need to have in the same directory as your code so you can import it\n", + "import gather_keys_oauth2 as Oauth2\n", + "import fitbit\n", + "#import pandas as pd \n", + "import datetime\n", + "# You will need to put in your own CLIENT_ID and CLIENT_SECRET as the ones below are fake\n" + ] + }, + { + "cell_type": "markdown", + "id": "f10c70e9-87fb-40d0-ab85-94e38e8a397c", + "metadata": {}, + "source": [ + "### Setup the client \n", + "### Reference https://python-fitbit.readthedocs.io/en/latest/#quickstart\n" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "id": "dda940c6-ee67-4385-8ebe-4e5d1e1105b0", + "metadata": {}, + "outputs": [], + "source": [ + "authd_client = fitbit.Fitbit(CLIENT_ID, CLIENT_SECRET ,\n", + " access_token=ACCESS_TOKEN, refresh_token=REFRESH_TOKEN)" + ] + }, + { + "cell_type": "markdown", + "id": "ea83b58e-59ff-45e5-94aa-3fa6af044d65", + "metadata": {}, + "source": [ + "### Obtain device list " + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "id": "afcc82e8-cf8f-4d62-9d89-0b76057be0df", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[{'battery': 'Empty',\n", + " 'batteryLevel': 0,\n", + " 'deviceVersion': 'MobileTrack',\n", + " 'features': [],\n", + " 'id': '1829675410',\n", + " 'lastSyncTime': '2021-06-10T11:10:07.000',\n", + " 'type': 'TRACKER'}]" + ] + }, + "execution_count": 23, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "authd_client.get_devices()" + ] + }, + { + "cell_type": "markdown", + "id": "d1d1e701-3c52-4729-9ecf-776d94f0f8a7", + "metadata": {}, + "source": [ + "### Obtain activitied" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "id": "bff96f43-522c-453e-966c-f632d173916c", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'activities': [],\n", + " 'goals': {'activeMinutes': 30,\n", + " 'caloriesOut': 2063,\n", + " 'distance': 5,\n", + " 'steps': 10000},\n", + " 'summary': {'activeScore': -1,\n", + " 'activityCalories': 0,\n", + " 'caloriesBMR': 708,\n", + " 'caloriesOut': 708,\n", + " 'distances': [{'activity': 'total', 'distance': 0},\n", + " {'activity': 'tracker', 'distance': 0},\n", + " {'activity': 'loggedActivities', 'distance': 0},\n", + " {'activity': 'veryActive', 'distance': 0},\n", + " {'activity': 'moderatelyActive', 'distance': 0},\n", + " {'activity': 'lightlyActive', 'distance': 0},\n", + " {'activity': 'sedentaryActive', 'distance': 0}],\n", + " 'fairlyActiveMinutes': 0,\n", + " 'lightlyActiveMinutes': 0,\n", + " 'marginalCalories': 0,\n", + " 'sedentaryMinutes': 845,\n", + " 'steps': 0,\n", + " 'veryActiveMinutes': 0}}" + ] + }, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "authd_client.activities()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "88f99265-c00d-44ef-a4de-bccb9abe1457", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "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.7.7" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/main_gathertokens.ipynb b/main_gathertokens.ipynb new file mode 100644 index 0000000..31f5e55 --- /dev/null +++ b/main_gathertokens.ipynb @@ -0,0 +1,116 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "ab4ac113-8148-4398-9133-f842bd2f4f81", + "metadata": {}, + "outputs": [], + "source": [ + "!pip3 install -r requirements/base.txt\n", + "!pip3 install datetime" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a5d85b83-f34a-4a7a-b2f6-91b10b670802", + "metadata": {}, + "outputs": [], + "source": [ + "# This is a python file you need to have in the same directory as your code so you can import it\n", + "import gather_keys_oauth2 as Oauth2\n", + "import fitbit\n", + "#import pandas as pd \n", + "import datetime\n", + "# You will need to put in your own CLIENT_ID and CLIENT_SECRET as the ones below are fake\n", + "CLIENT_ID=''\n", + "CLIENT_SECRET=''" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "6ef9de56-1d11-4f89-93eb-d9104b727b36", + "metadata": {}, + "outputs": [], + "source": [ + "server=Oauth2.OAuth2Server(CLIENT_ID, CLIENT_SECRET)\n", + "server.browser_authorize()\n", + "ACCESS_TOKEN=str(server.fitbit.client.session.token['access_token'])\n", + "REFRESH_TOKEN=str(server.fitbit.client.session.token['refresh_token'])\n", + "print(\"-----------ACCESS TOKEN-------------\")\n", + "print(ACCESS_TOKEN)\n", + "print(\"-----------REFRESH TOKEN-------------\")\n", + "print(REFRESH_TOKEN)\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a2f3755a-ac67-436d-ab43-67d2264dce3b", + "metadata": {}, + "outputs": [], + "source": [ + "\n", + "import datetime\n", + "yesterday = str((datetime.datetime.now() - datetime.timedelta(days=1)).strftime(\"%Y%m%d\"))\n", + "yesterday2 = str((datetime.datetime.now() - datetime.timedelta(days=1)).strftime(\"%Y-%m-%d\"))\n", + "today = str(datetime.datetime.now().strftime(\"%Y%m%d\"))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a4c738cc-0868-439f-9c2c-b0ae08ac7019", + "metadata": {}, + "outputs": [], + "source": [ + "auth2_client=fitbit.Fitbit(CLIENT_ID,CLIENT_SECRET,oauth2=True,access_token=ACCESS_TOKEN,refresh_token=REFRESH_TOKEN)\n", + "fit_statsHR = auth2_client.intraday_time_series('activities/heart', base_date=yesterday2, detail_level='1sec')\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "feea57b4-7310-4e81-a50f-54eac5fdbe81", + "metadata": {}, + "outputs": [], + "source": [ + "import json\n", + "json_formatted_str = json.dumps(fit_statsHR, indent=2)\n", + "print(json_formatted_str)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c890ff67-49c3-44ff-8065-9edc80f70444", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "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.7.7" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/requirements/.ipynb_checkpoints/base-checkpoint.txt b/requirements/.ipynb_checkpoints/base-checkpoint.txt new file mode 100644 index 0000000..1ff26aa --- /dev/null +++ b/requirements/.ipynb_checkpoints/base-checkpoint.txt @@ -0,0 +1,3 @@ +python-dateutil>=1.5 +requests-oauthlib>=0.7 +cherrypy \ No newline at end of file diff --git a/requirements/base.txt b/requirements/base.txt index 1331f7b..1ff26aa 100644 --- a/requirements/base.txt +++ b/requirements/base.txt @@ -1,2 +1,3 @@ python-dateutil>=1.5 requests-oauthlib>=0.7 +cherrypy \ No newline at end of file