From 2067de29f570c3ea3e5d8e9f256f999562d761cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lauri=20H=C3=A4rsil=C3=A4?= Date: Tue, 29 Jun 2021 23:38:25 +0300 Subject: [PATCH 1/2] Made gather_keys_oauth2.py play nice with https --- .gitignore | 3 +++ gather_keys_oauth2.py | 9 ++++++--- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 8dff601..13e6e6d 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,6 @@ htmlcov # Editors .idea + + +keys/ \ No newline at end of file diff --git a/gather_keys_oauth2.py b/gather_keys_oauth2.py index 39a19f8..9b8df47 100755 --- a/gather_keys_oauth2.py +++ b/gather_keys_oauth2.py @@ -14,7 +14,7 @@ class OAuth2Server: def __init__(self, client_id, client_secret, - redirect_uri='http://127.0.0.1:8080/'): + redirect_uri='https://127.0.0.1:18088/'): """ Initialize the FitbitOauth2Client """ self.success_html = """

You are now authorized to access the Fitbit API!

@@ -42,6 +42,9 @@ def browser_authorize(self): # Same with redirect_uri hostname and port. urlparams = urlparse(self.redirect_uri) + cherrypy.server.ssl_certificate = "keys/cert.pem" + cherrypy.server.ssl_private_key = "keys/privkey.pem" + cherrypy.server.ssl_module = 'builtin' cherrypy.config.update({'server.socket_host': urlparams.hostname, 'server.socket_port': urlparams.port}) @@ -82,8 +85,8 @@ def _shutdown_cherrypy(self): if __name__ == '__main__': - if not (len(sys.argv) == 3): - print("Arguments: client_id and client_secret") + if (len(sys.argv) < 3): + print("Arguments: client_id and client_secret [redirect_url]") sys.exit(1) server = OAuth2Server(*sys.argv[1:]) From a6cc47bcfa9702886e7c7e88c11c5ffdee398cdb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lauri=20H=C3=A4rsil=C3=A4?= Date: Wed, 30 Jun 2021 02:30:59 +0300 Subject: [PATCH 2/2] Implemented sleep list APImethod, switched to use fitbit API 1.2 --- fitbit/api.py | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/fitbit/api.py b/fitbit/api.py index 1b458b1..c3399e1 100644 --- a/fitbit/api.py +++ b/fitbit/api.py @@ -20,7 +20,7 @@ class FitbitOauth2Client(object): API_ENDPOINT = "https://api.fitbit.com" AUTHORIZE_ENDPOINT = "https://www.fitbit.com" - API_VERSION = 1 + API_VERSION = 1.2 request_token_url = "%s/oauth2/token" % API_ENDPOINT authorization_url = "%s/oauth2/authorize" % AUTHORIZE_ENDPOINT @@ -188,7 +188,7 @@ class Fitbit(object): METRIC = 'en_UK' API_ENDPOINT = "https://api.fitbit.com" - API_VERSION = 1 + API_VERSION = 1.2 WEEK_DAYS = ['SUNDAY', 'MONDAY', 'TUESDAY', 'WEDNESDAY', 'THURSDAY', 'FRIDAY', 'SATURDAY'] PERIODS = ['1d', '7d', '30d', '1w', '1m', '3m', '6m', '1y', 'max'] @@ -809,6 +809,23 @@ def get_sleep(self, date): ) return self.make_request(url) + def list_sleep(self, date): + """ + Get Sleep Logs List + The Get Sleep Logs List endpoint returns a list of a user's sleep logs (including naps) before or after a given day with offset, limit, and sort order. + date should be a datetime.date object. + """ + url = "{0}/{1}/user/-/sleep/list.json?beforeDate={year}-{month}-{day}&offset={offset}&limit={limit}&sort={sort}".format( + *self._get_common_args(), + year=date.year, + month=date.month, + day=date.day, + offset=0, + limit=2, + sort="desc" + ) + return self.make_request(url) + def log_sleep(self, start_time, duration): """ https://dev.fitbit.com/docs/sleep/#log-sleep