Skip to content

Instantly share code, notes, and snippets.

@retospect
Created September 27, 2012 17:24
Show Gist options
  • Save retospect/3795252 to your computer and use it in GitHub Desktop.
Save retospect/3795252 to your computer and use it in GitHub Desktop.
Hack to pull fitbit data to Beeminder
require 'rubygems'
gem "beeminder", "= 0.2.2"
require 'fitgem'
require 'json'
require 'beeminder'
consumer_key = 'FITBIT_KEY'
consumer_secret = 'FITBIT_SECRET'
token = 'FITBIT_TOKEN'
secret = ''
user_id = '2285JS' # may be similar to '12345N'
$datafile = '/home/beemind/history.json'
$history = {}
if File.exists?($datafile)
json = File.read($datafile) || "{}"
$history = JSON.parse(json)
end
def save
if File.exists?($datafile)
File.delete($datafile)
end
File.open($datafile,"w") do |f|
puts "Keys of json : #{$history.keys}"
f.write($history.to_json)
end
end
def get_value(date, key)
if not $history.has_key?(date.to_s)
return 0
end
if not $history[date.to_s].has_key?(key)
return 0
end
return $history[date.to_s][key]
end
def store_in_file(date, key, value)
if not $history.has_key?(date.to_s)
$history[date.to_s] = {}
end
$history[date.to_s][key] = value
save
end
def updateBKValue(bee, date, varname, newValue)
oldval = get_value(date, varname)
#p "Old value is #{oldval}, new value is #{newValue}"
stepsMod = 1.0*newValue - get_value(date, varname)
stepsMod = ((stepsMod*100).round*1.0)/100
if stepsMod < 0.01 && stepsMod > -0.01
stepsMod = 0
end
m = "Fitbit: on #{Time.new}, for #{date} adds #{stepsMod} to #{varname} for a daily total of #{newValue}"
p m
if not stepsMod == 0
g = bee.goal "fitbit_#{varname}"
nts = DateTime.parse(date.to_s) + 0.5
p nts.to_s
dp = Beeminder::Datapoint.new("comment" => m, "timestamp" => nts, "value" => stepsMod)
g.add dp
end
store_in_file date, varname, newValue
end
client = Fitgem::Client.new({:consumer_key => consumer_key, :consumer_secret => consumer_secret, :token => token, :secret => secret, :user_id => user_id})
access_token = client.reconnect(token, secret)
[Date.today, Date.today-1, Date.today-2].each do |date|
#[Date.today].each do |date|
sleep(1)
act_raw = client.activities_on_date(date)
steps = act_raw['summary']['steps']
score = act_raw['summary']['activeScore']
calories_out = act_raw['summary']['caloriesOut']
sleep(1)
sleep_raw = client.sleep_on_date(date)
sleep = 1.0/60*sleep_raw['summary']['totalMinutesAsleep']
sleep(1)
food_raw = client.foods_on_date(date)
calories_in = food_raw['summary']['calories']
protein = food_raw['summary']['protein']
p "Protein: #{protein}"
p "Steps: #{steps}, ActiveScore: #{score}"
p "Sleep is #{sleep}"
p "Calories are #{calories_in} in, and #{calories_out} out"
bee = Beeminder::User.new 'BEEMINDER_TOKEN', {}
updateBKValue(bee, date, 'steps', steps)
updateBKValue(bee, date, 'sleep', sleep)
updateBKValue(bee, date, 'score', score)
if (calories_in > 0) # Only do this if foods were logged
caloriedeficit = calories_out - calories_in
updateBKValue(bee, date, 'caloriedeficit', caloriedeficit)
end
updateBKValue(bee, date, 'protein', protein)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment