Skip to content

Instantly share code, notes, and snippets.

@katrielalex
Created March 14, 2015 17:35
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save katrielalex/6302010eaf3fb5d25359 to your computer and use it in GitHub Desktop.
Save katrielalex/6302010eaf3fb5d25359 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
"""
Quick script to beemind entering at least some meals into MyFitnessPal for the day.
Setup instructions:
- install myfitnesspal, requests and keyring (if you don't have them)
- call keyring.set_password("myfitnesspal", <your_username>, <your_password>)
- call keyring.set_password("beeminder", <your_username>, <your_api_key>)
PROFIT!
"""
import datetime
import json
import keyring
import myfitnesspal
import requests
import urlparse
username = "katrielalex"
mfp_goal_name = "myfitnesspal"
BEEMINDER_URL = "https://www.beeminder.com/api/v1"
GET_URL = BEEMINDER_URL + "/users/{username}/goals/{mfp_goal_name}.json".format(**locals())
POST_URL = BEEMINDER_URL + "/users/{username}/goals/{mfp_goal_name}/datapoints.json".format(**locals())
# Set up a MFP API client
password = keyring.get_password("myfitnesspal", username)
client = myfitnesspal.Client(username, password)
# Look up the meals you entered today
today = datetime.date.today()
day = client.get_date(today.year, today.month, today.day)
entered = max(len(meal.entries) for meal in day.meals)
# Look up data points for today
api_key = keyring.get_password("beeminder", username)
result = json.loads(requests.get(GET_URL, data={"auth_token": api_key}).content)
updated_at = datetime.datetime.fromtimestamp(result["updated_at"])
# Do we need to log new data?
now = datetime.datetime.now()
today8am = now.replace(hour=8, minute=0, second=0)
if entered and updated_at < today8am:
requests.post(POST_URL, data={
"auth_token": api_key,
"value": 1,
"comment": "via myfitnesspal scripting on {}".format(now.isoformat())})
@josephholsten
Copy link

I've made a couple changes at https://github.com/josephholsten/myfitnesspal-minder, would love some help getting this able to run under lambda or another serverless system.

@cmeury
Copy link

cmeury commented Apr 30, 2018

Made a couple more changes and changed the tracking quite a bit (checks whether anything has been entered for breakfast, lunch and dinner -- optimal value for every day is "3"). Added a Dockerfile for easy local development and instructions on how to run it on Heroku. https://github.com/cmeury/myfitnesspal-minder

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment