Created
February 12, 2013 12:33
-
-
Save michael-simons/4762022 to your computer and use it in GitHub Desktop.
This script fetches stuff from the fitbit api. You need to register an app (for consumer key and secret) and authorize this script on behalf of your account. One way to do this is with the out of band authentication. Feel free to expand this script to automate this.
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
# encoding: UTF-8 | |
require 'rubygems' | |
require 'date' | |
require 'optparse' | |
require 'ostruct' | |
require 'oauth' | |
require 'json' | |
class FitbitArchiver | |
attr_accessor :oauth, :day, :archive | |
def initialize(options) | |
consumerKey = "<<YOUR_API_KEY>>" | |
consumerSecret = "<<YOUR_API_SECRET>>" | |
michaelKey = "<<YOUR_KEY_AFTER_AUTHORIZING_THIS_SCRIPT>>" | |
michaelSecret = "<<YOUR_SECRET_AFTER_AUTHORIZING_THIS_SCRIPT>>" | |
@oauth = OAuth::AccessToken.new OAuth::Consumer.new(consumerKey, consumerSecret), michaelKey, michaelSecret | |
@day = (options.day ? options.day : Date.today - 1).strftime '%Y-%m-%d' | |
@archive = "#{File.expand_path(File.dirname(__FILE__))}/archive" | |
end | |
def archive_body | |
data = retrieve_json format("http://api.fitbit.com/%s/user/-/body/date/%s.json", 1, day) | |
write_to_file __method__.to_s.gsub('archive_', ''), data | |
end | |
def archive_body_weight | |
data = retrieve_json format("http://api.fitbit.com/%s/user/-/body/log/weight/date/%s.json", 1, day) | |
write_to_file __method__.to_s.gsub('archive_', ''), data | |
end | |
def archive_body_fat | |
data = retrieve_json format("http://api.fitbit.com/%s/user/-/body/log/fat/date/%s.json", 1, day) | |
write_to_file __method__.to_s.gsub('archive_', ''), data | |
end | |
def archive_activities | |
data = retrieve_json format("http://api.fitbit.com/%s/user/-/activities/date/%s.json", 1, day) | |
write_to_file __method__.to_s.gsub('archive_', ''), data | |
end | |
def archive_sleep | |
data = retrieve_json format("http://api.fitbit.com/%s/user/-/sleep/date/%s.json", 1, day) | |
write_to_file __method__.to_s.gsub('archive_', ''), data | |
end | |
private | |
def retrieve_json(url) | |
response = oauth.get url | |
case response | |
when Net::HTTPSuccess then JSON.parse(response.body) | |
else nil | |
end | |
end | |
def write_to_file(type, data) | |
File.open("#{archive}/#{type}.#{day}.json", 'w') {|f| f.write(JSON.pretty_generate(data)) } if data | |
end | |
end | |
begin | |
options = OpenStruct.new | |
ARGV.options do |opts| | |
opts.on("-dDAY", "--day DAY", "Use the given date instead of the day before today") do |day| | |
options.day = Date.parse day | |
end | |
opts.on_tail("-h", "--help", "Show this message") do | |
puts opts | |
exit | |
end | |
end | |
ARGV.parse! | |
fitbitArchiver = FitbitArchiver.new options | |
fitbitArchiver.methods.grep(/archive_.*/).each{|method| fitbitArchiver.send method} | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment