From 262e1e24d7d2d548c0a9601b7b87c48908c9b077 Mon Sep 17 00:00:00 2001 From: Ron Klinkien Date: Sat, 22 Feb 2020 19:02:05 +0100 Subject: [PATCH] Small fixes --- README.md | 4 +++- garminconnect/__init__.py | 21 +++++++++++++++------ garminconnect/__version__.py | 2 +- 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 0f70d0c..5f36920 100644 --- a/README.md +++ b/README.md @@ -29,9 +29,11 @@ from garminconnect import ( today = date.today() +client = Garmin(YOUR_EMAIL, YOUR_PASSWORD) + """Login to portal using specified credentials""" try: - client = Garmin(YOUR_EMAIL, YOUR_PASSWORD) + client.login() except ( GarminConnectConnectionError, GarminConnectAuthenticationError, diff --git a/garminconnect/__init__.py b/garminconnect/__init__.py index 17e8657..ee6676f 100644 --- a/garminconnect/__init__.py +++ b/garminconnect/__init__.py @@ -32,10 +32,12 @@ def __init__(self, email, password): self.password = password self.req = requests.session() self.logger = logging.getLogger(__name__) + self.display_name = "" + self.full_name = "" + self.unit_system = "" - self.login(self.email, self.password) - def login(self, email, password): + def login(self): """ Login to portal """ @@ -63,8 +65,8 @@ def login(self, email, password): } data = { - 'username': email, - 'password': password, + 'username': self.email, + 'password': self.password, 'embed': 'true', 'lt': 'e1s1', '_eventId': 'submit', @@ -112,6 +114,7 @@ def login(self, email, password): self.logger.debug("Fullname is %s", self.full_name) response.raise_for_status() + def parse_json(self, html, key): """ Find and return json data @@ -121,18 +124,21 @@ def parse_json(self, html, key): text = found.group(1).replace('\\"', '"') return json.loads(text) + def get_full_name(self): """ Return full name """ return self.full_name + def get_unit_system(self): """ Return unit system """ return self.unit_system + def get_stats(self, cdate): # cDate = 'YYY-mm-dd' """ Fetch available activity data @@ -151,7 +157,7 @@ def get_stats(self, cdate): # cDate = 'YYY-mm-dd' if response.json()['privacyProtected'] is True: self.logger.debug("Session expired - trying relogin") - self.login(self.email, self.password) + self.login() try: response = self.req.get(acturl, headers=self.headers) self.logger.debug("Activities response code %s, and json %s", response.status_code, response.json()) @@ -162,6 +168,7 @@ def get_stats(self, cdate): # cDate = 'YYY-mm-dd' return response.json() + def get_heart_rates(self, cdate): # cDate = 'YYYY-mm-dd' """ Fetch available heart rates data @@ -174,7 +181,7 @@ def get_heart_rates(self, cdate): # cDate = 'YYYY-mm-dd' response.raise_for_status() except requests.exceptions.HTTPError as err: self.logger.debug("Exception occured during heart rate retrieval - perhaps session expired - trying relogin: %s" % err) - self.login(self.email, self.password) + self.login() try: response = self.req.get(hearturl, headers=self.headers) self.logger.debug("Heart Rates response code %s, and json %s", response.status_code, response.json()) @@ -197,6 +204,7 @@ def __init__(self, status): super(GarminConnectConnectionError, self).__init__(status) self.status = status + class GarminConnectTooManyRequestsError(Exception): """Raised when rate limit is exceeded.""" @@ -205,6 +213,7 @@ def __init__(self, status): super(GarminConnectTooManyRequestsError, self).__init__(status) self.status = status + class GarminConnectAuthenticationError(Exception): """Raised when login returns wrong result.""" diff --git a/garminconnect/__version__.py b/garminconnect/__version__.py index e24a4a1..78b65fc 100644 --- a/garminconnect/__version__.py +++ b/garminconnect/__version__.py @@ -1,4 +1,4 @@ # -*- coding: utf-8 -*- """Python 3 API wrapper for Garmin Connect to get your statistics.""" -__version__ = "0.1.7" +__version__ = "0.1.8"