This repository has been archived by the owner on Nov 14, 2020. It is now read-only.
forked from ikasamah/withings-garmin
-
Notifications
You must be signed in to change notification settings - Fork 16
/
garmin.py
179 lines (137 loc) · 7.31 KB
/
garmin.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# -*- coding: utf-8 -*-
from sessioncache import SessionCache
from datetime import datetime, timedelta
import urllib.request, urllib.error, urllib.parse
import urllib.request, urllib.parse, urllib.error
import datetime
import requests
import re
import sys
import json
class LoginSucceeded(Exception):
pass
class LoginFailed(Exception):
pass
class GarminConnect(object):
LOGIN_URL = 'https://connect.garmin.com/signin'
UPLOAD_URL = 'https://connect.garmin.com/modern/proxy/upload-service/upload/.fit'
_sessionCache = SessionCache(lifetime=timedelta(minutes=30), freshen_on_get=True)
def create_opener(self, cookie):
this = self
class _HTTPRedirectHandler(urllib.request.HTTPRedirectHandler):
def http_error_302(self, req, fp, code, msg, headers):
if req.get_full_url() == this.LOGIN_URL:
raise LoginSucceeded
return urllib.request.HTTPRedirectHandler.http_error_302(self, req, fp, code, msg, headers)
return urllib.request.build_opener(_HTTPRedirectHandler, urllib.request.HTTPCookieProcessor(cookie))
##############################################
# From https://github.com/cpfair/tapiriik
def _get_session(self, record=None, email=None, password=None):
session = requests.Session()
# JSIG CAS, cool I guess.
# Not quite OAuth though, so I'll continue to collect raw credentials.
# Commented stuff left in case this ever breaks because of missing parameters...
data = {
"username": email,
"password": password,
"_eventId": "submit",
"embed": "true",
# "displayNameRequired": "false"
}
params = {
"service": "https://connect.garmin.com/modern",
# "redirectAfterAccountLoginUrl": "http://connect.garmin.com/modern",
# "redirectAfterAccountCreationUrl": "http://connect.garmin.com/modern",
# "webhost": "olaxpw-connect00.garmin.com",
"clientId": "GarminConnect",
"gauthHost": "https://sso.garmin.com/sso",
# "rememberMeShown": "true",
# "rememberMeChecked": "false",
"consumeServiceTicket": "false",
# "id": "gauth-widget",
# "embedWidget": "false",
# "cssUrl": "https://static.garmincdn.com/com.garmin.connect/ui/src-css/gauth-custom.css",
# "source": "http://connect.garmin.com/en-US/signin",
# "createAccountShown": "true",
# "openCreateAccount": "false",
# "usernameShown": "true",
# "displayNameShown": "false",
# "initialFocus": "true",
# "locale": "en"
}
headers = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36',
"Referer": "https://jhartman.pl",
'origin': 'https://sso.garmin.com'
}
# 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/signin", params=params, headers=headers)
if preResp.status_code != 200:
raise APIException("SSO prestart error %s %s" % (preResp.status_code, preResp.text))
ssoResp = session.post("https://sso.garmin.com/sso/login", params=params, data=data, allow_redirects=False, headers=headers)
if ssoResp.status_code != 200 or "temporarily unavailable" in ssoResp.text:
raise APIException("SSO error %s %s" % (ssoResp.status_code, ssoResp.text))
if ">sendEvent('FAIL')" in ssoResp.text:
raise APIException("Invalid login", block=True, user_exception=UserException(UserExceptionType.Authorization, intervention_required=True))
if ">sendEvent('ACCOUNT_LOCKED')" in ssoResp.text:
raise APIException("Account Locked", block=True, user_exception=UserException(UserExceptionType.Locked, intervention_required=True))
if "renewPassword" in ssoResp.text:
raise APIException("Reset password", block=True, user_exception=UserException(UserExceptionType.RenewPassword, intervention_required=True))
# self.print_cookies(cookies=session.cookies)
# ...AND WE'RE NOT DONE YET!
gcRedeemResp = session.get("https://connect.garmin.com/modern", allow_redirects=False, headers=headers)
if gcRedeemResp.status_code != 302:
raise APIException("GC redeem-start error %s %s" % (gcRedeemResp.status_code, gcRedeemResp.text))
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:
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))
if gcRedeemResp.status_code == 200 or gcRedeemResp.status_code == 404:
break
current_redirect_count += 1
if current_redirect_count > max_redirect_count:
break
self._sessionCache.Set(record.ExternalID if record else email, session.cookies)
# self.print_cookies(session.cookies)
session.headers.update(headers)
return session
def print_cookies(self, cookies):
print("Cookies")
for key, value in list(cookies.items()):
print("Key: " + key + ", " + value)
def login(self, username, password):
session = self._get_session(email=username, password=password)
try:
dashboard = session.get("http://connect.garmin.com/modern")
userdata_json_str = re.search(r"VIEWER_SOCIAL_PROFILE\s*=\s*JSON\.parse\((.+)\);$", dashboard.text, re.MULTILINE).group(1)
userdata = json.loads(json.loads(userdata_json_str))
username = userdata["displayName"]
print(username)
sys.stderr.write('Garmin Connect User Name: ' + username + '\n')
except Exception as e:
print(e)
sys.stderr.write('Unable to retrieve Garmin username! Most likely: incorrect Garmin login or password!\n')
return (session)
def upload_file(self, f, session):
files = {"data": ("withings.fit", f)}
res = session.post(self.UPLOAD_URL,
files=files,
headers={"nk": "NT"})
try:
resp = res.json()["detailedImportResult"]
except (ValueError, KeyError):
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))
return (res.status_code == 200 or res.status_code == 201 or res.status_code == 204)