Created
April 8, 2015 18:43
-
-
Save beaugunderson/a5f315e2c8e5883900a3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var crypto = require('crypto'); | |
var debug = require('debug')('withings'); | |
var request = require('request'); | |
var _ = require('lodash'); | |
var EMAIL = process.env.EMAIL; | |
var PASSWORD = process.env.PASSWORD; | |
var BASE_URL = 'http://scalews.withings.net'; | |
function md5(string) { | |
var md5sum = crypto.createHash('md5'); | |
md5sum.update(string); | |
return md5sum.digest('hex'); | |
} | |
var DEFAULTS = { | |
appliver: '20409', | |
appname: 'wiscaleNG', | |
apppfm: 'ios', | |
callctx: 'foreground' | |
}; | |
function Withings(email, password) { | |
this.email = email; | |
this.password = password; | |
} | |
Withings.prototype.post = function post(url, form, cb) { | |
form = _.defaults(form, DEFAULTS); | |
if (this.sessionId) { | |
form.sessionid = this.sessionId; | |
} | |
if (this.userId) { | |
form.userid = this.userId; | |
} | |
request.post({ | |
url: BASE_URL + url, | |
form: _.defaults(form, DEFAULTS), | |
headers: { | |
'User-Agent': 'Withings/20411 CFNetwork/711.1.16 Darwin/14.0.0', | |
'Accept-Language': 'en-us' | |
}, | |
gzip: true, | |
json: true | |
}, cb); | |
}; | |
Withings.prototype.once = function once(cb) { | |
debug('POST once'); | |
this.post('/cgi-bin/once', {action: 'get'}, function (err, res, body) { | |
if (err || body.status !== 0) { | |
console.log('error getting random one-time code'); | |
process.exit(1); | |
} | |
cb(body.body.once); | |
}); | |
}; | |
Withings.prototype.session = function session(once, cb) { | |
debug('POST session'); | |
var hash = md5(this.email + ':' + md5(this.password) + ':' + once); | |
this.post('/cgi-bin/session', { | |
action: 'new', | |
auth: this.email, | |
duration: 600, | |
hash: hash | |
}, function (err, res, body) { | |
if (err || body.status !== 0) { | |
console.log('error getting session'); | |
console.log(err, body); | |
process.exit(1); | |
} | |
cb(body.body); | |
}); | |
}; | |
Withings.prototype.user = function user(cb) { | |
debug('POST user'); | |
this.post('/cgi-bin/account', { | |
action: 'getuserslist', | |
recurse_devtype: '1', | |
enrich: 't', | |
recurse_use: '1', | |
listmask: '7', | |
accountid: this.accountId | |
}, function (err, res, body) { | |
if (err || body.status !== 0) { | |
console.log('error getting user'); | |
console.log(err, body); | |
process.exit(1); | |
} | |
cb(body.body.users[0]); | |
}); | |
}; | |
Withings.prototype.login = function (cb) { | |
debug('login'); | |
var self = this; | |
self.once(function (once) { | |
self.session(once, function (session) { | |
self.sessionId = session.sessionid; | |
self.accountId = session.accountid; | |
self.user(function (user) { | |
self.userId = user.id; | |
self.userInfo = user; | |
cb(); | |
}); | |
}); | |
}); | |
}; | |
var withings = new Withings(EMAIL, PASSWORD); | |
withings.login(function () { | |
withings.post('/cgi-bin/v2/measure', { | |
action: 'getvasistas', | |
devicetype: '32', | |
meastype: '36,37,38,39,40,41,42,43,44', | |
startdate: '1422432000', | |
enddate: '1422518399' | |
}, function (err, res, body) { | |
if (err) { | |
throw err; | |
} | |
var sleep = _.flatten(body.body.series[0].vasistas); | |
var timestamps = body.body.series[0].dates; | |
var rows = _.zip(timestamps, sleep); | |
_.each(rows, function (row) { | |
console.log(row.join(',')); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment