Skip to content

Commit

Permalink
Small fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
cyberjunky committed Feb 22, 2020
1 parent 170de58 commit 262e1e2
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
21 changes: 15 additions & 6 deletions garminconnect/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
"""
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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())
Expand All @@ -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
Expand All @@ -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())
Expand All @@ -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."""

Expand All @@ -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."""

Expand Down
2 changes: 1 addition & 1 deletion garminconnect/__version__.py
Original file line number Diff line number Diff line change
@@ -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"

0 comments on commit 262e1e2

Please sign in to comment.