-
-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathdaily-steps.py
118 lines (103 loc) · 3.93 KB
/
daily-steps.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
from datetime import date, datetime, timedelta
from garminconnect import Garmin
from notion_client import Client
from dotenv import load_dotenv
import os
def get_all_daily_steps(garmin):
"""
Get last x days of daily step count data from Garmin Connect.
"""
startdate = date.today() - timedelta(days=1)
daterange = [startdate + timedelta(days=x)
for x in range((date.today() - startdate).days)] # excl. today
daily_steps = []
for d in daterange:
daily_steps += garmin.get_daily_steps(d.isoformat(), d.isoformat())
return daily_steps
def daily_steps_exist(client, database_id, activity_date):
"""
Check if daily step count already exists in the Notion database.
"""
query = client.databases.query(
database_id=database_id,
filter={
"and": [
{"property": "Date", "date": {"equals": activity_date}},
{"property": "Activity Type", "title": {"equals": "Walking"}}
]
}
)
results = query['results']
return results[0] if results else None
def steps_need_update(existing_steps, new_steps):
"""
Compare existing steps data with imported data to determine if an update is needed.
"""
existing_props = existing_steps['properties']
activity_type = "Walking"
return (
existing_props['Total Steps']['number'] != new_steps.get('totalSteps') or
existing_props['Step Goal']['number'] != new_steps.get('stepGoal') or
existing_props['Total Distance (km)']['number'] != new_steps.get('totalDistance') or
existing_props['Activity Type']['title'] != activity_type
)
def update_daily_steps(client, existing_steps, new_steps):
"""
Update an existing daily steps entry in the Notion database with new data.
"""
total_distance = new_steps.get('totalDistance')
if total_distance is None:
total_distance = 0
properties = {
"Activity Type": {"title": [{"text": {"content": "Walking"}}]},
"Total Steps": {"number": new_steps.get('totalSteps')},
"Step Goal": {"number": new_steps.get('stepGoal')},
"Total Distance (km)": {"number": round(total_distance / 1000, 2)}
}
update = {
"page_id": existing_steps['id'],
"properties": properties,
}
client.pages.update(**update)
def create_daily_steps(client, database_id, steps):
"""
Create a new daily steps entry in the Notion database.
"""
total_distance = steps.get('totalDistance')
if total_distance is None:
total_distance = 0
properties = {
"Activity Type": {"title": [{"text": {"content": "Walking"}}]},
"Date": {"date": {"start": steps.get('calendarDate')}},
"Total Steps": {"number": steps.get('totalSteps')},
"Step Goal": {"number": steps.get('stepGoal')},
"Total Distance (km)": {"number": round(total_distance / 1000, 2)}
}
page = {
"parent": {"database_id": database_id},
"properties": properties,
}
client.pages.create(**page)
def main():
load_dotenv()
# Initialize Garmin and Notion clients using environment variables
garmin_email = os.getenv("GARMIN_EMAIL")
garmin_password = os.getenv("GARMIN_PASSWORD")
notion_token = os.getenv("NOTION_TOKEN")
database_id = os.getenv("NOTION_STEPS_DB_ID")
# Initialize Garmin client and login
garmin = Garmin(garmin_email, garmin_password)
garmin.login()
client = Client(auth=notion_token)
daily_steps = get_all_daily_steps(garmin)
for steps in daily_steps:
steps_date = steps.get('calendarDate')
existing_steps = daily_steps_exist(client, database_id, steps_date)
if existing_steps:
if steps_need_update(existing_steps, steps):
update_daily_steps(client, existing_steps, steps)
else:
create_daily_steps(client, database_id, steps)
if __name__ == '__main__':
main()
# %%