MyFitnessPal Custom Macros Script for Intermittent Fasting Diet Plan

After recently switching to an intermittent fasting plan, I've been seeking a solution for MyFitnessPal to support different macros on different days. A fellow coworker who's already lost 50lb on an IF plan pointed me to a reddit thread with a MFP custom macro script. It's an excellent little script and macro but I ran into some issues getting it to work.

I started by attempting to run the script via OSX on my Macbook but found I had to install the Mechanize extension for Python. No big deal but when I ran the script for the first time, I got a what looked to be an Mechanize error which kind of threw me off. After digging around, I found the fix on another related reddit thread. That fixed the authentication issue but then the script failed again with a similar error related to the 'totalCallories' variable.

macbook:site-packages user$ python macros.py
Schedule section exists
Sunday is: R
All configuration options present
Logging in
Login sent
Login response received
Login successful
Opening custom goals page
Custom goals page loaded
Traceback (most recent call last):
  File "macros.py", line 92, in <module>
    br.form["goals_nutrient_calories"]=targetCalories
  File "build/bdist.macosx-10.9-intel/egg/mechanize/_form.py", line 2780, in __setitem__
  File "build/bdist.macosx-10.9-intel/egg/mechanize/_form.py", line 3101, in find_control
  File "build/bdist.macosx-10.9-intel/egg/mechanize/_form.py", line 3185, in _find_control
mechanize._form.ControlNotFoundError: no control matching name 'goals_nutrient_calories'
Turns out the error maps to the script side here:
br.select_form(nr=0)
br.form["nutrient_goal[calories]"]=targetCalories
br.form["nutrient_goal[carb_ratio]"]=[targetCarb]
br.form["nutrient_goal[protein_ratio]"]=[targetProtein]
br.form["nutrient_goal[fat_ratio]"]=[targetFat]
It appears MFP made some additional changes on their side since the script was posted in 2012 and the prior fix was posted to reddit a year ago. After a little digging in the web source code, I found the MFP website was using new variable names on the custom calorie goals web form. Making the following changes resolved the issue:

br.select_form(nr=0)
br.form["goals[nutrient_calories]"]=targetCalories
br.form["goals[carb_ratio]"]=[targetCarb]
br.form["goals[protein_ratio]"]=[targetProtein]
br.form["goals[fat_ratio]"]=[targetFat]
That was the last hurdle for me. The script is now working great! Next up, I'll be exploring some ways to automate the script with into Google Sheets.

Popular posts from this blog

Will Chromecast become our primary home media device?

Bowling Ball Beach | Mendocino California