This repository has been archived by the owner on Nov 8, 2022. It is now read-only.
forked from ikasamah/withings-garmin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
garmin.py
81 lines (64 loc) · 2.67 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
# -*- coding: utf-8 -*-
import urllib2
import urllib
import datetime
class LoginSucceeded(Exception):
pass
class LoginFailed(Exception):
pass
class GarminConnect(object):
LOGIN_URL = 'https://connect.garmin.com/signin'
UPLOAD_URL = 'http://connect.garmin.com/proxy/upload-service-1.1/json/upload/.fit'
WEIGHT_INPUT_URL = 'http://connect.garmin.com/proxy/user-service-1.0/json/weight'
def __init__(self):
self.opener = self.create_opener()
def create_opener(self):
this = self
class _HTTPRedirectHandler(urllib2.HTTPRedirectHandler):
def http_error_302(self, req, fp, code, msg, headers):
if req.get_full_url() == this.LOGIN_URL:
raise LoginSucceeded
return urllib2.HTTPRedirectHandler.http_error_302(self, req, fp, code, msg, headers)
return urllib2.build_opener(_HTTPRedirectHandler, urllib2.HTTPCookieProcessor())
def login(self, username, password):
# prepare session cookies or so.
# you cannot login without access any Garmin Connect page.
self.opener.open(self.LOGIN_URL)
params = {'login:loginUsernameField': username,
'login:password': password,
'login': 'login',
'login:signInButton': 'Sign In',
'javax.faces.ViewState': 'j_id1'}
try:
self.opener.open(self.LOGIN_URL, urllib.urlencode(params))
except LoginSucceeded:
return True
raise LoginFailed('invalid username or password')
def upload_file(self, f):
# accept file object or string
if isinstance(f, file):
f.seek(0)
fbody = f.read()
else:
fbody = f
boundary = '----withingsgarmin'
req = urllib2.Request(self.UPLOAD_URL)
req.add_header('Content-Type', 'multipart/form-data; boundary=%s' % boundary)
# file
lines = []
lines.append('--%s' % boundary)
lines.append('Content-Disposition: form-data; name="data"; filename="weight.fit"')
lines.append('Content-Type: application/octet-stream')
lines.append('')
lines.append(fbody)
lines.append('--%s--' % boundary)
lines.append('')
r = self.opener.open(req, '\r\n'.join(lines))
return r.code == 200
def post_weight(self, date, value):
"""deprecated"""
if isinstance(date, (datetime.datetime, datetime.date)):
date = date.strftime('%Y-%m-%d')
params = {'date': date, 'value': value, 'returnProvidedValues': 'true'}
r = self.opener.open(self.WEIGHT_INPUT_URL, urllib.urlencode(params))
return r.code == 200