Skip to main content

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.

Comments

Popular posts from this blog

Will Chromecast become our primary home media device?

Chromecast is totally rock'n! I immediately ordered two Chromecast units last week while watching the livecast during a flight from Detroit to NYC. They were unexpectedly delivered before I made it back home. It's simply incredible how fast the world is changing driven by emerging solutions in mobile, social and cloud computing. Just two years ago, I was thrilled to get wifi on a plane and now its simply amazing to watch and order a newly announced product from a tablet while flying across the country!! 

After an easy setup, the interesting features announced at the product launch last week such as Chrome tab sharing and multi-device support are quickly living up to the hype. Tab sharing is my hands down favorite feature right now which gets around the many blocking issues of other streaming media solutions. For example, I can now stream Amazon video to the living room TV via Chromecast while editing photos on another virtual desktop via Mission Control on the family iMac. <…

Bowling Ball Beach | Mendocino California

This was another great adventure on the golden coast to find the Bowling Ball Beach in Mendocino, California which proved a tad difficult to find. I plotted the location in Google Maps but when I was alerted that I arrived at my location, I only saw woods on both sides and I could tell I was at an elevated location far from the beach so I continued on down the road thinking Google Maps must have got it wrong... more on the Unified Photography photo blog. Bowling Ball Beach Sunset | Mendocino, California