Skip to content

Instantly share code, notes, and snippets.

@christoomey
Last active December 18, 2015 01:48
Show Gist options
  • Save christoomey/5706178 to your computer and use it in GitHub Desktop.
Save christoomey/5706178 to your computer and use it in GitHub Desktop.
Fitbit & My Fitness Pal interactions
#!/usr/bin/env ruby
require "fitgem"
require "pp"
require "yaml"
config_file = begin
File.open(".fitgem.yml")
rescue Object => e
puts 'No .fitgem.yml config file found'
exit 1
end
# Load the existing yml config
config = begin
Fitgem::Client.symbolize_keys(YAML.load(config_file))
rescue ArgumentError => e
puts "Could not parse YAML: #{e.message}"
exit 1
end
client = Fitgem::Client.new(config[:oauth])
# With the token and secret, we will try to use them
# to reconstitute a usable Fitgem::Client
if config[:oauth][:token] && config[:oauth][:secret]
begin
access_token = client.reconnect(config[:oauth][:token], config[:oauth][:secret])
rescue Exception => e
puts "Error: Could not reconnect Fitgem::Client due to invalid keys in .fitgem.yml"
exit
end
# Without the secret and token, initialize the Fitgem::Client
# and send the user to login and get a verifier token
else
request_token = client.request_token
token = request_token.token
secret = request_token.secret
puts "Go to http://www.fitbit.com/oauth/authorize?oauth_token=#{token} and then enter the verifier code below"
verifier = gets.chomp
begin
access_token = client.authorize(token, secret, { :oauth_verifier => verifier })
rescue Exception => e
puts "Error: Could not authorize Fitgem::Client with supplied oauth verifier"
exit
end
puts 'Verifier is: '+verifier
puts "Token is: "+access_token.token
puts "Secret is: "+access_token.secret
user_id = client.user_info['user']['encodedId']
puts "Current User is: "+user_id
config[:oauth].merge!(:token => access_token.token, :secret => access_token.secret, :user_id => user_id)
# Write the whole oauth token set back to the config file
File.open(".fitgem.yml", "w") {|f| f.write(config.to_yaml) }
end
# ============================================================
# Add Fitgem API calls on the client object below this line
today = DateTime.now
15.times do |i|
date = today - i
activities = client.activities_on_date date
steps = activities['summary']['steps']
puts "#{date.strftime '%a, %Y-%m-%d'} -- #{steps} steps"
end
require 'open-uri'
require 'nokogiri'
require 'date'
def get_calories user, date
doc = get_page user, date, 'food'
totals = doc.css("tr.total")[0]
calories_str = totals.children.text.split[1]
calories = Integer calories_str.delete(",")
end
def get_exercise user, date
doc = get_page user, date, 'exercise'
exercise = Integer doc.css('span.soFar')[2].text
end
def get_water user, date
doc = get_page user, date, 'food'
raw_text = doc.css(".water-counter p").children.text
water_count = Integer raw_text.gsub(/[up|down]/i, "").strip
end
def get_page user, date, page
str_format = "%Y-%m-%d"
date_str = date.strftime(str_format)
url = "http://www.myfitnesspal.com/#{page}/diary/#{user}?date=#{date_str}"
doc = Nokogiri::HTML(open(url))
end
def main
user = "christoomey"
today = Date.today
puts sprintf "%10s%10s%10s%10s", "Date", "Calories", "Exercise", "Water"
puts "-"*45
(0..10).each do |i|
date = (today - i)
calories = get_calories user, date
exercise = get_exercise user, date
water = get_water user, date
date_str = date.strftime("%Y-%m-%d")
row_str = sprintf "%10s%10s%10s%10s", date_str, calories, exercise, water
puts row_str
end
end
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment