Created
February 1, 2011 04:52
-
-
Save harperreed/805429 to your computer and use it in GitHub Desktop.
Withings API php shell script for syncing to DB or whatever
This file contains 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
<? | |
define ('pound',0.453592); | |
define ('inch', 0.0254); | |
function CurlCall ( $service , &$result=null ) | |
{ | |
try { | |
$s = curl_init(); | |
curl_setopt($s,CURLOPT_URL,MYWBSAPIURL.$service); | |
curl_setopt($s,CURLOPT_POST,false); | |
curl_setopt($s, CURLOPT_RETURNTRANSFER, 1); | |
$output = curl_exec($s); | |
curl_close($s); | |
$result = json_decode ( $output , TRUE ); | |
if (!is_array($result)) return (false); | |
if (!key_exists('status',$result)) return (false); | |
if ($result['status']!=0) return (false); | |
return ( true ); | |
} catch ( Exception $e ) { | |
return ( false ); | |
} | |
} | |
function WBSAPI_OnceProbe () | |
{ | |
return ( CurlCall ( "once?action=probe" , &$result) ); | |
} | |
function WBSAPI_AccountGetuserslist ( $email, $password , &$userslist ) | |
{ | |
$userslist = Array (); | |
if (CurlCall ( "once?action=get", &$result)===false) return (false); | |
$once = $result['body']['once']; | |
$hash = md5 ( $email.":".md5($password).":".$once); | |
if (CurlCall ( "account?action=getuserslist&email=".$email."&hash=".$hash, &$result)===false) return (false); | |
$userslist = $result['body']['users']; | |
return (true); | |
} | |
function WBSAPI_UserGetbyuserid ( $userid, $publickey , &$user ) | |
{ | |
if (CurlCall ( "user?action=getbyuserid&userid=".$userid."&publickey=".$publickey,&$result)===false) return ( false );; | |
$user = $result['body']['users']['0']; | |
return (true); | |
} | |
function WBSAPI_UserUpdate ( $userid, $publickey , $ispublic ) | |
{ | |
return(CurlCall ( "user?action=update&userid=".$userid."&publickey=".$publickey."&ispublic=".$ispublic)); | |
} | |
function WBSAPI_MeasureGetmeas ( $userid, $publickey , &$measuregrps, $startdate=0, $enddate=0, $meastype=0, $category=1, $limit=1 ) | |
{ | |
$string="measure?action=getmeas&userid=".$userid."&publickey=".$publickey."&category=".$category."&limit=".$limit; | |
if ($startdate!=0) $string.="&startdate=".$startdate; | |
if ($enddate !=0) $string.="&enddate=".$enddate; | |
if ($meastype !=0) $string.="&meastype=".$enddate; | |
if (CurlCall ( $string,&$result)===false) return ( false ); | |
$measuregrps = $result['body']['measuregrps']; | |
return (true); | |
} | |
function WBSAPI_Helper_MeasureGrp ( $measuregrp , $system='SI') | |
{ | |
$string = date('r',$measuregrp['date'])."\n"; | |
foreach ( $measuregrp['measures'] as $measure ) { | |
$string .=" "; | |
$string .= WBSAPI_Helper_Measure ( $measure, $system ); | |
$string .="\n"; | |
} | |
return($string); | |
} | |
function WBSAPI_Helper_Measure ( $measure , $system='SI') | |
{ | |
$val = floatval ( $measure['value'] ) * floatval ( "1e".$measure['unit'] ); | |
if ( ($measure['type'] == 1) || ($measure['type'] == 5) || ($measure['type'] == 8) ) { | |
if ($system=='US') { | |
$val = $val / pound; | |
$val = round ( $val , 2); | |
$string = $val." lb"; | |
} else { | |
$val = round ( $val , 2); | |
$string = $val." Kg"; | |
} | |
if ($measure['type'] == 1) $string = "Weight ".$string; | |
if ($measure['type'] == 5) $string = "FatFree ".$string; | |
if ($measure['type'] == 8) $string = "FAT ".$string; | |
} elseif ($measure['type'] == 4) { | |
if ($system=='US') { | |
$inchs = round($val/inch); | |
$foot = floor($inchs/12); | |
$inch = $inchs-12*$foot; | |
$string = $foot." ft ".$inch." in"; | |
} else { | |
$val = round ( $val , 2); | |
$string = $val." m"; | |
} | |
$string = "Size ".$string; | |
} elseif ($measure['type'] == 6) { | |
$string = "FAT % ".$val." %"; | |
} else { | |
$string = "Unknown type"; | |
} | |
return ($string); | |
} | |
function WBSAPI_NotifySubscribe ( $userid, $publickey , $callbackurl ) | |
{ | |
$string="notify?action=subscribe&userid=".$userid."&publickey=".$publickey."&callbackurl=".urlencode($callbackurl); | |
if (CurlCall ( $string,&$result)===false) return ( false ); | |
return (true); | |
} | |
function WBSAPI_NotifyRevoke ( $userid, $publickey , $callbackurl ) | |
{ | |
$string="notify?action=revoke&userid=".$userid."&publickey=".$publickey."&callbackurl=".$callbackurl; | |
if (CurlCall ( $string,&$result)===false) return ( false ); | |
return (true); | |
} | |
function WBSAPI_NotifyGet ( $userid, $publickey , $callbackurl , &$expires , &$comment ) | |
{ | |
$string="notify?action=get&userid=".$userid."&publickey=".$publickey."&callbackurl=".$callbackurl; | |
if (CurlCall ( $string,&$result)===false) return ( false ); | |
$expires = $result['body']['expires']; | |
$comment = $result['body']['comment']; | |
return (true); | |
} | |
?> |
This file contains 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
#!/usr/bin/php | |
<? | |
include ("WBSAPI.php"); | |
ini_set("memory_limit","1024M"); | |
define ('MYMAIL','youremail@email.com'); | |
define ('MYPASS','asdkjh78asdjkgkjqwhg321978asydljgqwehglqwe'); | |
define ('MYURL',''); | |
define ('MYWBSAPIURL','wbsapi.withings.net/'); | |
define ('MYAPIURL','scalews.withings.net/cgi-bin/'); | |
echo "Probing API : "; | |
if (WBSAPI_OnceProbe ()) { | |
echo "OK\n"; } else { echo "KO\n"; exit(-1); | |
} | |
echo "Getting UserList : "; | |
if (WBSAPI_AccountGetuserslist ( MYMAIL, MYPASS, $userslist)) { | |
echo "OK\n"; } else { echo "KO\n"; exit(-1); | |
} | |
$listtoreload = false; | |
echo "Ensuring all users are public : "; | |
foreach ($userslist as $user) { | |
$name = $user['firstname']." ".$user['lastname']; | |
if ($user['ispublic']!='1') { | |
$listtoreload = true; | |
WBSAPI_UserUpdate ( $user['id'], $user['publickey'], 1 ); | |
} | |
} | |
echo "OK\n"; | |
// When a user has been set or reset to "public", its public key changes... We need to reload the list in that case . | |
if ($listtoreload) { | |
echo "Getting UserList : "; | |
if (WBSAPI_AccountGetuserslist ( MYMAIL, MYPASS, $userslist)) { | |
echo "OK\n"; } else { echo "KO\n"; exit(-1); | |
} | |
} | |
foreach ($userslist as $user) { | |
$name = $user['firstname']." ".$user['lastname']; | |
echo "[".$name."] Loading measures : "; | |
#$startdate = strtotime("16 December 2009"); | |
#$enddate = strtotime("now"); | |
$meastype=0; | |
$category=1; | |
$limit=5; | |
#$limit = round(($enddate-$startdate)/(24*60*60),0);; | |
if (WBSAPI_MeasureGetmeas ( $user['id'], $user['publickey'] , &$measuregrps, $startdate, $enddate,$meastype, $category, $limit)) { | |
echo "OK\n"; } else { echo "KO\n"; exit(-1); | |
} | |
echo count($measuregrps). " weights to sync\n"; | |
foreach ( $measuregrps as $measuregrp ) { | |
$w['date'] = $measuregrp['date']; | |
foreach ($measuregrp['measures'] as $m){ | |
if ($m['type']==1) | |
$w['weight'] = $m['value']; | |
if ($m['type']==4) | |
$w['size'] = $m['value']; | |
if ($m['type']==5) | |
$w['fat_free_mass'] = $m['value']; | |
if ($m['type']==6) | |
$w['fat_ratio'] = $m['value']; | |
if ($m['type']==5) | |
$w['fat_mass_weight'] = $m['value']; | |
} | |
// Do some shit with the data | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment