Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions accesslink/accesslink.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ def __init__(self, client_id, client_secret, redirect_url=None):
self.training_data = endpoints.TrainingData(oauth=self.oauth)
self.physical_info = endpoints.PhysicalInfo(oauth=self.oauth)
self.daily_activity = endpoints.DailyActivity(oauth=self.oauth)
self.nightly_recharge = endpoints.NightlyRecharge(oauth=self.oauth)
self.sleep = endpoints.Sleep(oauth=self.oauth)

@property
def authorization_url(self):
Expand Down
2 changes: 2 additions & 0 deletions accesslink/endpoints/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
from .daily_activity import DailyActivity
from .physical_info import PhysicalInfo
from .training_data import TrainingData
from .nightly_recharge import NightlyRecharge
from .sleep import Sleep
32 changes: 32 additions & 0 deletions accesslink/endpoints/nightly_recharge.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/usr/bin/env python

from datetime import datetime
from requests import HTTPError
from .resource import Resource


class NightlyRecharge(Resource):
"""This resource allows partners to access their nightly recharges information.

https://www.polar.com/accesslink-api/#nightly-recharge
"""

def list_nightly_recharges(self, access_token):
"""List Nightly Recharge data of user for the last 28 days.

:param access_token: access token of the user
"""
return self._get(endpoint="/users/nightly-recharge",
access_token=access_token)

def get_nightly_recharge_by_date(self, access_token, date=datetime.today()):
"""Get Users Nightly Recharge data for given date.

:param access_token: access token of the user
:param date: datetime instance
"""
try:
return self._get(endpoint=f"/users/nightly-recharge/{date.strftime('%Y-%m-%d')}",
access_token=access_token)
except HTTPError:
return None
40 changes: 40 additions & 0 deletions accesslink/endpoints/sleep.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/usr/bin/env python

from datetime import datetime
from requests import HTTPError
from .resource import Resource


class Sleep(Resource):
"""This resource allows partners to access their sleep information.

https://www.polar.com/accesslink-api/#sleep
"""

def list_sleeps(self, access_token):
"""List sleep data of user for the last 28 days.

:param access_token: access token of the user
"""
return self._get(endpoint="/users/sleep",
access_token=access_token)

def list_sleeps_available(self, access_token):
"""Get the dates with sleep start and end times, where user has sleep data available in the last 28 days.

:param access_token: access token of the user
"""
return self._get(endpoint="/users/sleep/available",
access_token=access_token)

def get_sleep_by_date(self, access_token, date=datetime.today()):
"""Get Users sleep data for given date.

:param access_token: access token of the user
:param date: datetime instance
"""
try:
return self._get(endpoint=f"/users/sleep/{date.strftime('%Y-%m-%d')}",
access_token=access_token)
except HTTPError:
return None
28 changes: 26 additions & 2 deletions accesslink_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ def show_menu(self):
"1) Get user information\n" +
"2) Check available data\n" +
"3) Revoke access token\n" +
"4) Exit\n" +
"4) Today nightly recharge\n" +
"5) Sleep\n" +
"6) Exit\n" +
"-----------------------")
self.get_menu_choice()

Expand All @@ -48,7 +50,9 @@ def get_menu_choice(self):
"1": self.get_user_information,
"2": self.check_available_data,
"3": self.revoke_access_token,
"4": self.exit
"4": self.today_nightly_recharge,
"5": self.today_sleep,
"6": self.exit,
}.get(choice, self.get_menu_choice)()

def get_user_information(self):
Expand Down Expand Up @@ -140,6 +144,26 @@ def get_physical_info(self):

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()