Skip to content

Commit

Permalink
Added bodyfit from withings to fitbit
Browse files Browse the repository at this point in the history
  • Loading branch information
TheGabeMan committed Aug 9, 2024
1 parent 7662432 commit 92bb3d4
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 37 deletions.
87 changes: 57 additions & 30 deletions fitbit_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@
https://dev.fitbit.com/build/reference/web-api/troubleshooting-guide/oauth2-tutorial/
"""

# from genericpath import isfile
import json
import logging
import os
import sys

# import datetime
from datetime import datetime
import base64
import webbrowser
Expand All @@ -18,8 +15,6 @@
from dotenv import load_dotenv
import requests

# from requests.exceptions import Timeout

load_dotenv()

# Strava API access
Expand All @@ -34,19 +29,17 @@
"activities": "https://api.fitbit.com/1/user/-/activities/date/" + dateparam + ".json",
"weight": "https://api.fitbit.com/1/user/-/body/log/weight/date/" + dateparam + ".json",
"set_weight": "https://api.fitbit.com/1/user/-/body/log/weight.json",
"set_bodyfat": "https://api.fitbit.com/1/user/-/body/log/fat.json",
"water": "https://api.fitbit.com/1/user/-/foods/log/date/" + dateparam + ".json",
"sleep": "https://api.fitbit.com/1.2/user/-/sleep/date/" + dateparam + ".json",
"user": "https://api.fitbit.com/1/user/-/profile.json",
"heartrate": "https://api.fitbit.com/1/user/-/activities/heart/date/" + dateparam + "/1d.json",
}


def main():
"""Main function for testing"""
fitbit_access_token = fitbit_read_user()
fitbit_set_weight(82, fitbit_access_token)
fitbit_introspect(fitbit_access_token)

# # def main():
# """Main function for testing"""
# # fitbit_set_weight(user_weight, user_fat)
# # fitbit_introspect(fitbit_access_token)

def fitbit_read_user():
"""Read user info info from fitbit"""
Expand All @@ -64,40 +57,75 @@ def fitbit_read_user():
# urlDict["user"] = https://api.fitbit.com/1/user/-/profile.json
response = requests.get(urlDict["user"], headers=headers, timeout=10)
print("User info:", response.json()["user"]["fullName"])
print("User info:", response.json()["user"]["weight"])
logging.info("User info")
logging.info("User weight")

# urlDict["weight"] = https://api.fitbit.com/1/user/-/body/log/weight/date/
response = requests.get(urlDict["weight"], headers=headers, timeout=10)
print("Weight URL:", response.json()["weight"])
return fitbit_access_token


def fitbit_set_weight(weight, fitbit_access_token):
def fitbit_set_weight(user_weight,user_fat):
"""Write user weight to fitbit"""

''' Get tokens '''
fitbit_access_token = fitbit_read_user()
today = datetime.now().strftime("%Y-%m-%d")
data = {"weight": weight, "date": today}
content_length = len(str(data))

''' Now writing body weight to fitbit '''
dataweight = {"weight": user_weight, "date": today}
content_length_weight = len(str(dataweight))

headers = {
"Authorization": f"Bearer {fitbit_access_token}",
"accept": "application/json",
"content-length": str(content_length),
"content-length": str(content_length_weight),
}
response = requests.post(
urlDict["set_weight"], data=data, headers=headers, timeout=10
urlDict["set_weight"], data=dataweight, headers=headers, timeout=10
)
if response.status_code != 201:
print("There was an error writing to Fitbit API:")
logging.info("There was an error writing to Fitbit API:")
logging.info(response.json())
return False

print(f"Succesful writing weight {user_weight} to FitBit API")
logging.info("Succesful writing weight %s to FitBit API", user_weight)

''' Now writing body fat to fitbit '''
datafat = {"fat": user_fat, "date": today}
content_length_fat = len(str(datafat))

headers = {
"Authorization": f"Bearer {fitbit_access_token}",
"accept": "application/json",
"content-length": str(content_length_weight),
}
response = requests.post(
urlDict["set_bodyfat"], data=datafat, headers=headers, timeout=10
)
print(response.json())

if response.status_code != 201:
print("There was an error writing fat to Fitbit API:")
logging.info("There was an error writing fat to Fitbit API:")
logging.info(response.json())
return False

print(f"Succesful writing weight {user_fat} to FitBit API")
logging.info("Succesful writing weight %s to FitBit API", user_fat)
return True



def fitbit_introspect(fitbit_access_token):
"""check token permissions"""
"""check token permissions for problem solving """
url = "https://api.fitbit.com/1.1/oauth2/introspect"
headers = {
"Authorization": f"Bearer {fitbit_access_token}",
"Content-Type": "application/x-www-form-urlencoded",
}
data = f"token={fitbit_access_token}"
print(data)

response = requests.post(url, data=data, headers=headers, timeout=10)
print(response.json())

Expand Down Expand Up @@ -131,7 +159,8 @@ def fitbit_refresh(token):
return output["access_token"]

print("Authenication failed")
print(output)
logging.info("Authenication failed")
logging.info(output)
sys.exit()


Expand All @@ -155,9 +184,10 @@ def fitbit_authenticate():
}

response = requests.get(url, params=params, timeout=10)

webbrowser.open(response.url, new=2)
fitbit_code = input(
"Copy the authorization code located between the code parameter name and the string #_=_"
"Copy the authorization code located between the code parameter name and the string #_=_ : "
)

params = {
Expand All @@ -175,14 +205,11 @@ def fitbit_authenticate():
fitbit_token_url, params=params, headers=headers, timeout=10
)
output = response.json()
logging.info(response.json())
if response.status_code == 200:
json.dump(output, open(fitbit_cfg, "w", encoding="utf8"))
return output["access_token"]

print("Authenication failed")
print(output)
logging.info(output)
sys.exit()


if __name__ == "__main__":
sys.exit(main())
14 changes: 9 additions & 5 deletions healthsync.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
"""
config section
"""

# from genericpath import isfile
import sys
import argparse
import logging
import wahoo_api
import strava_api
import intervals_api
import withings_api
import fitbit_api
import send_telegram

logging.basicConfig(filename="weightsync.log", encoding="utf-8", level=logging.INFO)
Expand Down Expand Up @@ -37,7 +36,7 @@ def main():

if args.weight is None:
logging.info("No weight on commandline, try to retrieve from withings.")
user_weight = withings_api.get_withings_user_weight()
user_weight, user_fat = withings_api.get_withings_user_weight()
if user_weight is not None:
withings_read_ok = True
else:
Expand All @@ -63,9 +62,14 @@ def main():
# Write the weight to Strava
strava_send_ok = strava_api.set_strava_weight(user_weight)

# Write weight and fat to fitbit
fitbit_send_ok = fitbit_api.fitbit_set_weight(user_weight, user_fat)



telegram_text = send_telegram.create_body_text(
user_weight, withings_read_ok, wahoo_send_ok, intervals_send_ok,
strava_send_ok
user_weight, withings_read_ok, wahoo_send_ok, intervals_send_ok,
strava_send_ok, fitbit_send_ok
)
send_telegram.send_telegram_message(telegram_text)

Expand Down
3 changes: 2 additions & 1 deletion send_telegram.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def send_telegram_message(message):


def create_body_text(
user_weight, withings_read_ok, wahoo_send_ok, intervals_send_ok, strava_send_ok
user_weight, withings_read_ok, wahoo_send_ok, intervals_send_ok, strava_send_ok, fitbit_send_ok
):
"""Create a body text for the telegram message"""
today = date.today().strftime("%d-%m-%Y")
Expand All @@ -47,4 +47,5 @@ def create_body_text(
text += "Data sent to Wahoo " + str(wahoo_send_ok) + "\n"
text += "Data sent to Intervals " + str(intervals_send_ok) + "\n"
text += "Data sent to Strava " + str(strava_send_ok) + "\n"
text += "Data sent to Fitbit " + str(fitbit_send_ok) + "\n"
return text
3 changes: 2 additions & 1 deletion withings_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ def get_withings_user_weight():
"""Read user weight from withings"""
withings_data = get_withings_data()
user_weight = withings_data["weight"]
return user_weight
user_fat = withings_data["bodyFat"]
return user_weight, user_fat


def get_withings_data():
Expand Down

0 comments on commit 92bb3d4

Please sign in to comment.