Skip to content
This repository has been archived by the owner on Nov 14, 2020. It is now read-only.

Commit

Permalink
Fix for AttributeError: 'NoneType' object has no attribute 'groups' (…
Browse files Browse the repository at this point in the history
…another incarnation)

Fixed issue #4
#4
  • Loading branch information
jaroslawhartman committed May 21, 2017
1 parent e585831 commit 9c99875
Showing 1 changed file with 19 additions and 19 deletions.
38 changes: 19 additions & 19 deletions garmin.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,12 @@ def _get_session(self, record=None, email=None, password=None):
# "initialFocus": "true",
# "locale": "en"
}

# I may never understand what motivates people to mangle a perfectly good protocol like HTTP in the ways they do...
preResp = session.get("https://sso.garmin.com/sso/login", params=params)
if preResp.status_code != 200:
raise APIException("SSO prestart error %s %s" % (preResp.status_code, preResp.text))

data["lt"] = re.search("name=\"lt\"\s+value=\"([^\"]+)\"", preResp.text).groups(1)[0]

# print "Received lt: " + data["lt"]

ssoResp = session.post("https://sso.garmin.com/sso/login", params=params, data=data, allow_redirects=False)
if ssoResp.status_code != 200 or "temporarily unavailable" in ssoResp.text:
raise APIException("SSO error %s %s" % (ssoResp.status_code, ssoResp.text))
Expand All @@ -90,31 +87,28 @@ def _get_session(self, record=None, email=None, password=None):
if "renewPassword" in ssoResp.text:
raise APIException("Reset password", block=True, user_exception=UserException(UserExceptionType.RenewPassword, intervention_required=True))

ticket_match = re.search("ticket=([^']+)'", ssoResp.text)

if not ticket_match:
raise APIException("Invalid login", block=True, user_exception=UserException(UserExceptionType.Authorization, intervention_required=True))
ticket = ticket_match.groups(1)[0]

# print "Ticket: " + ticket
# self.print_cookies(cookies=session.cookies)

# ...AND WE'RE NOT DONE YET!

gcRedeemResp = session.get("https://connect.garmin.com/post-auth/login", params={"ticket": ticket}, allow_redirects=False)
gcRedeemResp = session.get("https://connect.garmin.com/post-auth/login", allow_redirects=False)
if gcRedeemResp.status_code != 302:
raise APIException("GC redeem-start error %s %s" % (gcRedeemResp.status_code, gcRedeemResp.text))

# self.print_cookies(cookies=session.cookies)
url_prefix = "https://connect.garmin.com"

# There are 6 redirects that need to be followed to get the correct cookie
# ... :(
max_redirect_count = 7
current_redirect_count = 1
while True:
gcRedeemResp = session.get(gcRedeemResp.headers["location"], allow_redirects=False)

# self.print_cookies(cookies=session.cookies)
url = gcRedeemResp.headers["location"]

# Fix up relative redirects.
if url.startswith("/"):
url = url_prefix + url
url_prefix = "/".join(url.split("/")[:3])
gcRedeemResp = session.get(url, allow_redirects=False)

if current_redirect_count >= max_redirect_count and gcRedeemResp.status_code != 200:
raise APIException("GC redeem %d/%d error %s %s" % (current_redirect_count, max_redirect_count, gcRedeemResp.status_code, gcRedeemResp.text))
Expand All @@ -126,6 +120,8 @@ def _get_session(self, record=None, email=None, password=None):

self._sessionCache.Set(record.ExternalID if record else email, session.cookies)

# self.print_cookies(session.cookies)

return session

def print_cookies(self, cookies):
Expand Down Expand Up @@ -156,7 +152,11 @@ def upload_file(self, f, session):
try:
resp = res.json()["detailedImportResult"]
except ValueError:
raise APIException("Bad response during GC upload: %s %s" % (res.status_code, res.text))
if(res.status_code == 204): # HTTP result 204 - "no content"
sys.stderr.write('No data to upload, try to use --fromdate and --todate\n')
else:
print "Bad response during GC upload: " + str(res.status_code)
raise APIException("Bad response during GC upload: %s %s" % (res.status_code, res.text))

return (res.status_code == 200 or res.status_code == 201)
return (res.status_code == 200 or res.status_code == 201 or res.status_code == 204)

0 comments on commit 9c99875

Please sign in to comment.