-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathaccesslink_example.py
More file actions
executable file
·169 lines (122 loc) · 5.43 KB
/
accesslink_example.py
File metadata and controls
executable file
·169 lines (122 loc) · 5.43 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
#!/usr/bin/env python
from __future__ import print_function
from utils import load_config, save_config, pretty_print_json
from accesslink import AccessLink
try:
input = raw_input
except NameError:
pass
CONFIG_FILENAME = "config.yml"
class PolarAccessLinkExample(object):
"""Example application for Polar Open AccessLink v3."""
def __init__(self):
self.config = load_config(CONFIG_FILENAME)
if "access_token" not in self.config:
print("Authorization is required. Run authorization.py first.")
return
self.accesslink = AccessLink(client_id=self.config["client_id"],
client_secret=self.config["client_secret"])
self.running = True
self.show_menu()
def show_menu(self):
while self.running:
print("\nChoose an option:\n" +
"-----------------------\n" +
"1) Get user information\n" +
"2) Check available data\n" +
"3) Revoke access token\n" +
"4) Today nightly recharge\n" +
"5) Sleep\n" +
"6) Exit\n" +
"-----------------------")
self.get_menu_choice()
def get_menu_choice(self):
choice = input("> ")
{
"1": self.get_user_information,
"2": self.check_available_data,
"3": self.revoke_access_token,
"4": self.today_nightly_recharge,
"5": self.today_sleep,
"6": self.exit,
}.get(choice, self.get_menu_choice)()
def get_user_information(self):
user_info = self.accesslink.users.get_information(user_id=self.config["user_id"],
access_token=self.config["access_token"])
pretty_print_json(user_info)
def check_available_data(self):
available_data = self.accesslink.pull_notifications.list()
if not available_data:
print("No new data available.")
return
print("Available data:")
pretty_print_json(available_data)
for item in available_data["available-user-data"]:
if item["data-type"] == "EXERCISE":
self.get_exercises()
elif item["data-type"] == "ACTIVITY_SUMMARY":
self.get_daily_activity()
elif item["data-type"] == "PHYSICAL_INFORMATION":
self.get_physical_info()
def revoke_access_token(self):
self.accesslink.users.delete(user_id=self.config["user_id"],
access_token=self.config["access_token"])
del self.config["access_token"]
del self.config["user_id"]
save_config(self.config, CONFIG_FILENAME)
print("Access token was successfully revoked.")
self.exit()
def exit(self):
self.running = False
def get_exercises(self):
transaction = self.accesslink.training_data.create_transaction(user_id=self.config["user_id"],
access_token=self.config["access_token"])
if not transaction:
print("No new exercises available.")
return
resource_urls = transaction.list_exercises()["exercises"]
for url in resource_urls:
exercise_summary = transaction.get_exercise_summary(url)
print("Exercise summary:")
pretty_print_json(exercise_summary)
transaction.commit()
def get_daily_activity(self):
transaction = self.accesslink.daily_activity.create_transaction(user_id=self.config["user_id"],
access_token=self.config["access_token"])
if not transaction:
print("No new daily activity available.")
return
resource_urls = transaction.list_activities()["activity-log"]
for url in resource_urls:
activity_summary = transaction.get_activity_summary(url)
print("Activity summary:")
pretty_print_json(activity_summary)
transaction.commit()
def get_physical_info(self):
transaction = self.accesslink.physical_info.create_transaction(user_id=self.config["user_id"],
access_token=self.config["access_token"])
if not transaction:
print("No new physical information available.")
return
resource_urls = transaction.list_physical_infos()["physical-informations"]
for url in resource_urls:
physical_info = transaction.get_physical_info(url)
print("Physical info:")
pretty_print_json(physical_info)
transaction.commit()
def today_nightly_recharge(self):
nightly_recharge = self.accesslink.nightly_recharge.get_nightly_recharge_by_date(access_token=self.config["access_token"])
if not nightly_recharge:
print("Today has no nightly recharge")
return
print("Today nightly recharge:")
pretty_print_json(nightly_recharge)
def today_sleep(self):
sleep = self.accesslink.sleep.get_sleep_by_date(access_token=self.config["access_token"])
if not sleep:
print("Today has no sleep")
return
print("Today sleep:")
pretty_print_json(sleep)
if __name__ == "__main__":
PolarAccessLinkExample()