-
Notifications
You must be signed in to change notification settings - Fork 31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
added support for optional [date] parameter for MFP plugin #174
base: gonzobot
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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): | ||
"""<user> - returns macros from the MyFitnessPal food diary of <user>""" | ||
request = requests.get(scrape_url.format(text)) | ||
""" | ||
<user> [date]- returns macros from the MyFitnessPal food diary of <user> | ||
optionally, specify [date] to retrieve that day's diary | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The core will only grab the first line of the docstring for command documentation, you'll want to combine these lines. |
||
""" | ||
date = 'today' | ||
|
||
args = text.split() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd recommend doing |
||
user = args[0] | ||
if(len(args) > 1): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Redundant parentheses in |
||
dt = parser.parse(' '.join(args[1:])) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To go along with my comment about the |
||
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)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should probably use |
||
|
||
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 = [] | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,3 +17,4 @@ pyenchant | |
pythonwhois | ||
imgurpython | ||
isodate | ||
python-dateutil |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would probably be a good idea to use the
parameters
keyword torequests.get()
to pass query parameters rather than formatting them in to the URL. For example:which ensures that the parameter is properly escaped.