Details for MyFitnessPal Data Export.ipynb

Published by madprime

Description

Export your data from MyFitnessPal into a CSV file.

0

Tags & Data Sources

food log myfitnesspal username

Comments

Please log in to comment.

Notebook
Last updated 4 years, 9 months ago

Retrieve MyFitnessPal diet logs

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. ;)

Step 1: Import the library

This is another hack, but this installs the library you need if it's not already here.

Step 2: Get username / password

After you enter it, your password is deliberately hidden below. We don't want it stored in the notebook!

Username: madprime
Days to collect: 14
Getting your Open Humans username, which we need for the filepath at the end...
Initiating a login to MyFitnessPal...
Retrieving 2019-6-14...
Retrieving 2019-6-13...
Retrieving 2019-6-12...
Retrieving 2019-6-11...
Retrieving 2019-6-10...
Retrieving 2019-6-9...
Retrieving 2019-6-8...
Retrieving 2019-6-7...
Retrieving 2019-6-6...
Retrieving 2019-6-5...
Retrieving 2019-6-4...
Retrieving 2019-6-3...
Retrieving 2019-6-2...
Retrieving 2019-6-1...

Done! Click this link to download your data:
https://notebooks.openhumans.org/user/madprime/tree/20190614-myfitnesspal-data.csv

Notebook
Last updated 4 years, 9 months ago

Retrieve MyFitnessPal diet logs

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. ;)

Step 1: Import the library

This is another hack, but this installs the library you need if it's not already here.

In [1]:
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')

Step 2: Get username / password

After you enter it, your password is deliberately hidden below. We don't want it stored in the notebook!

In [2]:
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))
Username: madprime
Days to collect: 14
In [3]:
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))
Getting your Open Humans username, which we need for the filepath at the end...
Initiating a login to MyFitnessPal...
Retrieving 2019-6-14...
Retrieving 2019-6-13...
Retrieving 2019-6-12...
Retrieving 2019-6-11...
Retrieving 2019-6-10...
Retrieving 2019-6-9...
Retrieving 2019-6-8...
Retrieving 2019-6-7...
Retrieving 2019-6-6...
Retrieving 2019-6-5...
Retrieving 2019-6-4...
Retrieving 2019-6-3...
Retrieving 2019-6-2...
Retrieving 2019-6-1...

Done! Click this link to download your data:
https://notebooks.openhumans.org/user/madprime/tree/20190614-myfitnesspal-data.csv