forked from veerendra2/fitbit-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformatter.py
More file actions
470 lines (386 loc) · 15.6 KB
/
formatter.py
File metadata and controls
470 lines (386 loc) · 15.6 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
# -*- coding: utf-8 -*-
"""
Json Data Formatter
"""
from rich.console import Console
from rich.table import Table
from rich.text import Text
CONSOLE = Console()
def display_user_profile(user_data, as_json=False):
"""User data formatter"""
user = user_data["user"]
height_unit = "cm" if user["heightUnit"] == "METRIC" else "in"
weight_unit = "kg"
if user["weightUnit"] == "UK":
weight_unit = "stone"
elif user["weightUnit"] == "US":
weight_unit = "pounds"
if as_json:
return {
"user_profile": {
"first_name": user["firstName"],
"last_name": user["lastName"],
"date_of_birth": user["dateOfBirth"],
"age": user["age"],
"gender": user["gender"],
"height": f"{user['height']:.1f} {height_unit}",
"weight": f"{user['weight']:.1f} {weight_unit}",
"average_daily_steps": user["averageDailySteps"],
"member_since": user["memberSince"],
"timezone": user["timezone"],
}
}
table = Table(title=f"Hello, {user['displayName']} :wave:", show_header=False)
table.add_column("")
table.add_column("")
table.add_row(":bust_in_silhouette: First Name", user["firstName"])
table.add_row(":family: Last Name", user["lastName"])
table.add_row(":birthday: Date Of Birth", user["dateOfBirth"])
table.add_row(":hourglass_flowing_sand: Age", str(user["age"]))
table.add_row(":restroom: Gender", user["gender"])
table.add_row(":straight_ruler: Height", f"{user['height']:.1f} {height_unit}")
table.add_row(":weight_lifter: Weight", f"{user['weight']:.1f} {weight_unit}")
table.add_row(":footprints: Average Daily Steps", str(user["averageDailySteps"]))
table.add_row(":calendar: Member Since", user["memberSince"])
table.add_row(":clock1: Time Zone", user["timezone"])
CONSOLE.print(table)
return None
def display_sleep(sleep_data, as_json=False):
"""Sleep data formatter"""
if as_json:
return {
"sleep": [
{
"date": s["dateOfSleep"],
"deep_minutes": s["levels"]["summary"]
.get("deep", {})
.get("minutes"),
"light_minutes": s["levels"]["summary"]
.get("light", {})
.get("minutes"),
"rem_minutes": s["levels"]["summary"].get("rem", {}).get("minutes"),
"wake_minutes": s["levels"]["summary"]
.get("wake", {})
.get("minutes"),
"efficiency": s["efficiency"],
"time_in_bed_hours": round(s["timeInBed"] / 60, 1),
}
for s in sleep_data["sleep"]
]
}
table = Table(title="Sleep Data Summary :sleeping:", show_header=True)
table.add_column("Date :calendar:")
table.add_column("Deep Sleep :bed:")
table.add_column("Light Sleep :zzz:")
table.add_column("REM Sleep :crescent_moon:")
table.add_column("Wake Time :alarm_clock:")
table.add_column("Efficiency :100:")
table.add_column("Time in Bed :clock1:")
for sleep in sleep_data["sleep"]:
table.add_row(
sleep["dateOfSleep"],
f"{sleep['levels']['summary'].get('deep', {}).get('minutes', 'N/A')} min",
f"{sleep['levels']['summary'].get('light', {}).get('minutes', 'N/A')} min",
f"{sleep['levels']['summary'].get('rem', {}).get('minutes', 'N/A')} min",
f"{sleep['levels']['summary'].get('wake', {}).get('minutes', 'N/A')} min",
f"{sleep['efficiency']}%",
f"{sleep['timeInBed'] / 60:.1f} hr",
)
CONSOLE.print(table)
return None
def display_spo2(spo2_data, as_json=False):
"""SpO2 data formatter"""
if isinstance(spo2_data, dict):
spo2_data = [spo2_data]
if as_json:
return {
"spo2": [
{
"date": s.get("dateTime"),
"min": s.get("value", {}).get("min"),
"avg": s.get("value", {}).get("avg"),
"max": s.get("value", {}).get("max"),
}
for s in spo2_data
]
}
table = Table(title="SpO2 Data Summary :heart:", show_header=True)
table.add_column("Date :calendar:")
table.add_column("Minimum :red_circle:")
table.add_column("Average :blue_circle:")
table.add_column("Maximum :green_circle:")
for spo2 in spo2_data:
table.add_row(
spo2.get("dateTime", "N/A"),
str(spo2.get("value", {}).get("min", "N/A")),
str(spo2.get("value", {}).get("avg", "N/A")),
str(spo2.get("value", {}).get("max", "N/A")),
)
CONSOLE.print(table)
return None
def display_heart_data(heart_data, as_json=False):
"""Heart data formatter"""
if as_json:
return {
"heart": [
{
"date": a.get("dateTime"),
"resting_heart_rate": a.get("value", {}).get("restingHeartRate"),
"zones": [
{
"name": z.get("name"),
"min": z.get("min"),
"max": z.get("max"),
"minutes": z.get("minutes"),
"calories_out": (
round(z["caloriesOut"], 2)
if isinstance(z.get("caloriesOut"), (int, float))
else None
),
}
for z in a.get("value", {}).get("heartRateZones", [])
],
}
for a in heart_data.get("activities-heart", [])
]
}
table = Table(title="Heart Rate Time Series :heart:", show_header=True)
table.add_column("Date :calendar:")
table.add_column("Resting Heart Rate :heartpulse:")
table.add_column("Heart Rate Zones :dart:")
for activity in heart_data.get("activities-heart", []):
date = activity.get("dateTime", "N/A")
value = activity.get("value", {})
resting_heart_rate = value.get("restingHeartRate", "N/A")
zones_table = Table(show_header=True, header_style="bold magenta")
zones_table.add_column("Zone :dart:")
zones_table.add_column("Min :arrow_down:")
zones_table.add_column("Max :arrow_up:")
zones_table.add_column("Minutes :hourglass:")
zones_table.add_column("Calories Out (kcal) :fire:")
for zone in value.get("heartRateZones", []):
zones_table.add_row(
zone.get("name", "N/A"),
str(zone.get("min", "N/A")),
str(zone.get("max", "N/A")),
str(zone.get("minutes", "N/A")),
(
f"{zone.get('caloriesOut', 'N/A'):.2f}"
if isinstance(zone.get("caloriesOut"), (int, float))
else "N/A"
),
)
table.add_row(date, str(resting_heart_rate), zones_table)
CONSOLE.print(table)
return None
def display_azm_time_series(azm_data, as_json=False):
"""AZM Time Series data formatter"""
if as_json:
return {
"active_zone": [
{
"date": a.get("dateTime"),
"active_zone_minutes": a.get("value", {}).get("activeZoneMinutes"),
"fat_burn_minutes": a.get("value", {}).get(
"fatBurnActiveZoneMinutes"
),
"cardio_minutes": a.get("value", {}).get("cardioActiveZoneMinutes"),
"peak_minutes": a.get("value", {}).get("peakActiveZoneMinutes"),
}
for a in azm_data.get("activities-active-zone-minutes", [])
]
}
table = Table(title="AZM Time Series :runner:", show_header=True)
table.add_column("Date :calendar:")
table.add_column("Active Zone Minutes :stopwatch:")
table.add_column("Fat Burn :fire:")
table.add_column("Cardio :heart:")
table.add_column("Peak :mountain:")
for activity in azm_data.get("activities-active-zone-minutes", []):
date = activity.get("dateTime", "N/A")
value = activity.get("value", {})
table.add_row(
date,
str(value.get("activeZoneMinutes", "N/A")),
str(value.get("fatBurnActiveZoneMinutes", "N/A")),
str(value.get("cardioActiveZoneMinutes", "N/A")),
str(value.get("peakActiveZoneMinutes", "N/A")),
)
CONSOLE.print(table)
return None
def display_breathing_rate(breathing_rate_data, as_json=False):
"""Breathing Rate data formatter"""
if as_json:
return {
"breathing_rate": [
{
"date": br.get("dateTime"),
"breathing_rate": br.get("value", {}).get("breathingRate"),
}
for br in breathing_rate_data.get("br", [])
]
}
table = Table(title="Breathing Rate Summary 🫁", show_header=True)
table.add_column("Date :calendar:")
table.add_column("Breaths Per Minute :dash:")
for br in breathing_rate_data.get("br", []):
table.add_row(
br.get("dateTime", "N/A"),
str(br.get("value", {}).get("breathingRate", "N/A")),
)
CONSOLE.print(table)
return None
def display_hrv(hrv_data, as_json=False):
"""HRV data formatter"""
if as_json:
return {
"hrv": [
{
"date": h.get("dateTime"),
"daily_rmssd": h.get("value", {}).get("dailyRmssd"),
"deep_rmssd": h.get("value", {}).get("deepRmssd"),
}
for h in hrv_data.get("hrv", [])
]
}
table = Table(title="HRV Data Summary :heartpulse:", show_header=True)
table.add_column("Date :calendar:")
table.add_column("Daily RMSSD :chart_with_upwards_trend:")
table.add_column("Deep RMSSD :sleeping:")
for hrv in hrv_data.get("hrv", []):
table.add_row(
hrv.get("dateTime", "N/A"),
str(hrv.get("value", {}).get("dailyRmssd", "N/A")),
str(hrv.get("value", {}).get("deepRmssd", "N/A")),
)
CONSOLE.print(table)
return None
def _merge_body_data(body_data):
"""Merge weight, BMI, and body fat time series by date."""
merged = {}
resource_map = {
"weight": "body-weight",
"bmi": "body-bmi",
"fat": "body-fat",
}
for resource, response_key in resource_map.items():
for item in body_data.get(resource, {}).get(response_key, []):
date = item.get("dateTime")
if date not in merged:
merged[date] = {
"date": date,
"weight": None,
"bmi": None,
"fat": None,
}
merged[date][resource] = item.get("value")
return [merged[date] for date in sorted(merged)]
def display_body(body_data, as_json=False):
"""Body time series formatter"""
merged_body = _merge_body_data(body_data)
if as_json:
return {"body": merged_body}
table = Table(title="Body Time Series :balance_scale:", show_header=True)
table.add_column("Date :calendar:")
table.add_column("Weight :weight_lifter:")
table.add_column("BMI :straight_ruler:")
table.add_column("Body Fat % :chart_with_upwards_trend:")
for body in merged_body:
table.add_row(
str(body.get("date", "N/A")),
str(body.get("weight", "N/A")),
str(body.get("bmi", "N/A")),
str(body.get("fat", "N/A")),
)
CONSOLE.print(table)
return None
def display_devices(devices, as_json=False):
"""Devices list formatter"""
def format_mac(mac):
if mac == "N/A" or len(mac) % 2:
return mac
return ":".join(mac[i : i + 2] for i in range(0, len(mac), 2))
if as_json:
return {
"devices": [
{
"battery_level": device.get("batteryLevel"),
"device": device.get("deviceVersion"),
"type": device.get("type"),
"last_sync_time": device.get("lastSyncTime"),
"mac_address": format_mac(str(device.get("mac", "N/A"))),
}
for device in devices
]
}
table = Table(title="Devices List :link:", show_header=True)
table.add_column("Battery % :battery:")
table.add_column("Device :watch:")
table.add_column("Type :iphone:")
table.add_column("Last Sync Time :clock3:")
table.add_column("MAC Address :label:")
for device in devices:
mac_address = format_mac(str(device.get("mac", "N/A")))
table.add_row(
f"{str(device.get('batteryLevel', 'N/A'))}%",
str(device.get("deviceVersion", "N/A")),
str(device.get("type", "N/A")),
str(device.get("lastSyncTime", "N/A")),
mac_address,
)
CONSOLE.print(table)
return None
def display_activity(activity_data, unit_system, as_json=False):
"""Activity data formatter"""
dis_unit = "km" if unit_system != "US" else "miles"
if as_json:
return {
"activities": [
{
"date": day.get("date"),
"activities": [
{
"start_time": a.get("startTime"),
"name": a.get("name"),
"description": a.get("description"),
"distance": (
f"{a.get('distance', 'N/A')} {dis_unit}"
if a.get("distance") is not None
else None
),
"steps": a.get("steps"),
"calories": a.get("calories"),
"duration_minutes": round(a.get("duration", 0) / 60000, 1),
}
for a in day.get("activities", [])
],
}
for day in activity_data
]
}
table = Table(title="Daily Activities :runner:", show_header=True)
table.add_column("Date :calendar:")
table.add_column("Activities :clipboard:")
for activity_day in activity_data:
activity_table = Table(show_header=True, header_style="bold magenta")
activity_table.add_column("Start Time :alarm_clock:")
activity_table.add_column("Name :running_shirt_with_sash:")
activity_table.add_column("Description :memo:", width=30)
activity_table.add_column("Distance :straight_ruler:")
activity_table.add_column("Steps :footprints:")
activity_table.add_column("Calories (kcal) :fire:")
activity_table.add_column("Duration :hourglass:")
for activity in activity_day.get("activities", []):
duration_min = activity.get("duration", 0) / 60000
activity_table.add_row(
activity.get("startTime", ""),
activity.get("name", "N/A"),
Text(activity.get("description", "N/A"), overflow="fold"),
f"{activity.get('distance', 'N/A')} {dis_unit}",
str(activity.get("steps", "N/A")),
str(activity.get("calories", "N/A")),
f"{duration_min:.1f} min",
)
table.add_row(activity_day.get("date", ""), activity_table)
CONSOLE.print(table)
return None