diff --git a/plugins/myfitnesspal.py b/plugins/myfitnesspal.py index 2e1e0d47c..0cc7820c7 100644 --- a/plugins/myfitnesspal.py +++ b/plugins/myfitnesspal.py @@ -2,21 +2,33 @@ import math import requests from bs4 import BeautifulSoup +from dateutil import parser from cloudbot import hook -scrape_url = "http://www.myfitnesspal.com/food/diary/{}" +scrape_url = "http://www.myfitnesspal.com/food/diary/{}?date={}" @hook.command('mfp', 'myfitnesspal') def mfp(text, bot): - """ - returns macros from the MyFitnessPal food diary of """ - request = requests.get(scrape_url.format(text)) + """ + [date]- returns macros from the MyFitnessPal food diary of + optionally, specify [date] to retrieve that day's diary + """ + date = 'today' + + args = text.split() + user = args[0] + if(len(args) > 1): + dt = parser.parse(' '.join(args[1:])) + date = "{}-{}-{}".format(dt.year, dt.month, dt.day) + + request = requests.get(scrape_url.format(user, date)) if request.status_code != requests.codes.ok: return "Failed to fetch info ({})".format(request.status_code) - output = "Diary for {}: ".format(text) + output = "Diary for {}: ".format(user) try: soup = BeautifulSoup(request.text, 'html.parser') @@ -45,7 +57,7 @@ def mfp(text, bot): output += ("{caption}: {total}/{remain}{units} ({pct}%) " .format(**kwargs)) - output += " ({})".format(scrape_url.format(text)) + output += " ({})".format(scrape_url.format(user, date)) except Exception as e: print(e) @@ -69,7 +81,7 @@ def get_headers(soup): def get_values(soup, row_class): """get values from a specific summary row based on the row class""" - locale.setlocale(locale.LC_ALL, 'en_US.UTF-8') # for number parsing + locale.setlocale(locale.LC_ALL, '') # for number parsing values = [] diff --git a/requirements.txt b/requirements.txt index ba58005f9..511fcd822 100644 --- a/requirements.txt +++ b/requirements.txt @@ -17,3 +17,4 @@ pyenchant pythonwhois imgurpython isodate +python-dateutil