Forked from krzysztofzablocki/gist:f5f597f04b2efcb711c7
Created
November 20, 2019 12:31
-
-
Save Siyanda/9332c8f6aa7d3f1b7093b9132e057f5d to your computer and use it in GitHub Desktop.
mfp.rb
This file contains hidden or 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
| 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" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment