require 'mechanize' require 'moving_average' mechanize = Mechanize.new login_page = mechanize.get 'https://www.myfitnesspal.com/account/login' form = login_page.forms.first # noinspection RubyResolve form.field_with(id: 'username').value = "username" form.field_with(id: 'password').value = "pass" form.submit days_to_query = 27 weight_report = mechanize.get("http://www.myfitnesspal.com/reports/results/progress/1/#{days_to_query}.json") calories_report = mechanize.get("http://www.myfitnesspal.com/reports/results/nutrition/Calories/#{days_to_query}.json") weights = JSON.parse(weight_report.body)["data"] calories = JSON.parse(calories_report.body)["data"] avg_calories = calories.map { |hash| hash['total'] }.reject { |x| x == 0 }.instance_eval { reduce(:+) / size.to_f } puts "Calories: #{ avg_calories }" weight_values = weights.map { |hash| hash['total'] } smoothed_weights = [] weight_values.count.times do |idx| smoothed_weights.push( if idx > 1 weight_values[0, idx + 1].smma.round(1) else weight_values[idx] end ) end weight_change = weight_values.first - weight_values.last weight_in_calories = weight_change * 7700 weight = weight_values.last observed_tdee = avg_calories + weight_in_calories / weights.count protein = weight * 2 fats = weight * 0.8 carbs = (observed_tdee - (protein*4 + fats * 9))/4 puts "Observed TDEE is #{observed_tdee}" puts "protein = #{protein} g" puts "fat = #{fats} g" puts "carbs = #{carbs} g"