MyFitnessPal Data Export.ipynb
Export your data from MyFitnessPal into a CSV file.
This notebook scrapes the MyFitnessPal website to get your food logs. It's a hack, but...
This runs in your personal, ephemeral virtual machine.
MyFitnessPal charges a monthly subscription for data export, but you can use this instead. ;)
This is another hack, but this installs the library you need if it's not already here.
After you enter it, your password is deliberately hidden below. We don't want it stored in the notebook!
This notebook scrapes the MyFitnessPal website to get your food logs. It's a hack, but...
This runs in your personal, ephemeral virtual machine.
MyFitnessPal charges a monthly subscription for data export, but you can use this instead. ;)
This is another hack, but this installs the library you need if it's not already here.
def install_and_import(package):
import importlib
try:
importlib.import_module(package)
except ImportError:
import subprocess
import sys
subprocess.call([sys.executable, "-m", "pip", "install", package])
finally:
globals()[package] = importlib.import_module(package)
install_and_import('myfitnesspal')
After you enter it, your password is deliberately hidden below. We don't want it stored in the notebook!
mfp_username = input("What is your username? ")
mfp_password = input("What is your password? ")
numdays = int(input("How many days do you want to collect? "))
from IPython.display import clear_output
clear_output()
print("Username: {}".format(mfp_username))
print("Days to collect: {}".format(numdays))
import csv
import os
import arrow
import requests
print("Getting your Open Humans username, which we need for the filepath at the end...")
response = requests.get(
"https://www.openhumans.org/api/direct-sharing/project/exchange-member/"
"?access_token={}".format(os.environ.get('OH_ACCESS_TOKEN')))
oh_username = response.json()['username']
print("Initiating a login to MyFitnessPal...")
client = myfitnesspal.Client(mfp_username, password=mfp_password)
row_items = ['date', 'meal', 'name', 'calories', 'carbohydrates', 'fat', 'protein', 'sodium', 'sugar']
def get_day(year, month, day):
data = []
day = client.get_date(year, month, day)
for meal in day.keys():
for entry in day[meal].get_as_list():
row_data = [meal] + [entry['name']] + [
entry['nutrition_information'][item] for item in
row_items[3:]
]
data.append(row_data)
return data
today = arrow.get()
filename = '{}-myfitnesspal-data.csv'.format(today.format('YYYYMMDD'))
out = open(filename, 'w')
csv_out = csv.writer(out)
csv_out.writerow(row_items)
for i in range(0, -1 * numdays, -1):
target_day = today.shift(days=i)
print("Retrieving {}-{}-{}...".format(target_day.year, target_day.month, target_day.day))
data = get_day(target_day.year, target_day.month, target_day.day)
for row in data:
csv_out.writerow([target_day.format('YYYY-MM-DD')] + row)
print("\nDone! Click this link to download your data:")
print("https://notebooks.openhumans.org/user/{}/tree/{}".format(oh_username, filename))