-
Notifications
You must be signed in to change notification settings - Fork 12
/
draw.py
160 lines (120 loc) · 3.32 KB
/
draw.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
import hashlib
import Image
import json
import sys
import time
import urllib
import urllib2
from urlgrabber.keepalive import HTTPHandler
MARGIN = 3000
urlbase = "http://scalews.withings.net/cgi-bin/"
urlonce = urlbase + "once"
urlsess = urlbase + "session"
urlmaint = urlbase + "maint"
urlassoc = urlbase + "association"
urlmeas = urlbase + "measure"
def craft_params(params):
return urllib.unquote(urllib.urlencode(params))
def do_request(opener, url, data):
req = urllib2.Request(url, data)
opener.addheaders = headers
return opener.open(req).read()
if __name__ == "__main__":
if len(sys.argv) != 4:
print "%s <mac_addr> <secret> <image>" % sys.argv[0]
exit()
mac_addr = sys.argv[1]
secret = sys.argv[2]
headers = [
("User-Agent", "Withings UserAgent"),
("Accept", "*/*"),
]
opener = urllib2.build_opener(HTTPHandler()) # Keep-alive
##
# Step 1: /cgi-bin/once
##
params = {
"action": "get"
}
data = craft_params(params)
resp = do_request(opener, urlonce, data)
print resp
once = json.loads(resp)["body"]["once"]
##
# Step 2: /cgi-bin/session
##
md5 = hashlib.md5()
md5.update("%s:%s:%s" % (mac_addr, secret, once))
hash_calc = md5.digest().encode("hex")
params = {
"action": "new",
"auth": mac_addr,
"hash": hash_calc,
"mfgid": "262151",
"currentfw": "200",
"batterylvl": "1337",
"duration": "30",
"zreboot": "1"
}
data = craft_params(params)
resp = do_request(opener, urlsess, data)
print resp
tmp = json.loads(resp)
sessionid = tmp["body"]["sessionid"]
user_id = tmp["body"]["sp"]["users"][0]["id"]
##
# Step 3: /cgi-bin/measure
##
curtime = int(time.time())
print "Current time: %d" % curtime
im = Image.open(sys.argv[3])
width, height = im.size
print "Image width: %d" % width
print "Image height: %d" % height
print "Iterating pixels..."
pix = im.load()
count = 0
for x in xrange(width):
points = {
"measures": [
]
}
for y in xrange(height):
a, b, c, d = pix[x,y]
if a < 128:
point = {
"value": 20000 + ((height - y) * MARGIN),
"type": 1,
"unit": -3
}
points["measures"].append(point)
count += 1
print "total points = %d" % count
if len(points["measures"]) == 0:
print "Blank column, skipping request"
continue
params = {
"action": "store",
"sessionid": sessionid,
"macaddress": mac_addr,
"userid": user_id,
"meastime": curtime - ((width - x) * MARGIN),
"devtype": 1,
"attribstatus": 0,
"measures": urllib.quote_plus(json.dumps(points, separators=(",", ":")))
}
data = craft_params(params)
print data
resp = do_request(opener, urlmeas, data)
print resp
##
# Step 4: /cgi-bin/session
##
params = {
"action": "delete",
"sessionid": sessionid
}
data = craft_params(params)
print data
resp = do_request(opener, urlsess, data)
print resp