-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhealthsync.py
executable file
·96 lines (76 loc) · 2.5 KB
/
healthsync.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
"""
config section
"""
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)
def main():
"""The Main Script"""
parser = argparse.ArgumentParser()
parser.add_argument(
"apis", nargs="*", help="Use withings, strava API to read weight"
)
parser.add_argument(
"-w",
"--weight",
required=False,
type=float,
action="store",
help="Input weight in KG (floating point value)",
)
args = parser.parse_args()
# TODO: Lusje maken om door de args.apis heen te lopen
if "debug" in args.apis:
set_logging_debug()
if args.weight is None:
logging.info("No weight on commandline, try to retrieve from withings.")
user_weight, user_fat = withings_api.get_withings_user_weight()
if user_weight is not None:
withings_read_ok = True
else:
withings_read_ok = False
else:
logging.info("Manual weight entry as parameter")
if not isfloat(args.weight):
logging.info("Received weight is not a floating-point number.")
print("Received weight is not a floating-point number.")
parser.print_help()
sys.exit()
else:
logging.info("Received weight is floating-point number.")
user_weight = args.weight
# Write the weight to Wahoo
wahoo_send_ok = wahoo_api.write_weight_wahoo(user_weight)
# Write the weight to intervals.icu
intervals_send_ok = intervals_api.set_intervals_weight(user_weight)
# 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, fitbit_send_ok
)
send_telegram.send_telegram_message(telegram_text)
sys.exit()
def set_logging_debug():
"""Logging level switching to debug"""
logging.basicConfig(
filename="weightsync.log", encoding="utf-8", level=logging.DEBUG
)
def isfloat(num):
"""Check if num is a floating-point"""
try:
float(num)
return True
except ValueError:
return False
if __name__ == "__main__":
sys.exit(main())