Skip to content
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

Open
wants to merge 3 commits into
base: gonzobot
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions plugins/myfitnesspal.py
Original file line number Diff line number Diff line change
Expand Up @@ -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={}"

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 to requests.get() to pass query parameters rather than formatting them in to the URL. For example:

request = requests.get(scrape_url.format(user), parameters={'date': date})

which ensures that the parameter is properly escaped.



@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

Choose a reason for hiding this comment

The 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()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd recommend doing text.split(None, 1) to limit the split to only splitting once, since you only want to retrieve 2 parts from text.

user = args[0]
if(len(args) > 1):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Redundant parentheses in if statement. Format should be if len(args) > 1:

dt = parser.parse(' '.join(args[1:]))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To go along with my comment about the .split(), you wouldn't need to use a join if the text is only split on the first space.

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')
Expand Down Expand Up @@ -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))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably use request.url instead of duplicating the URL generation code.


except Exception as e:
print(e)
Expand All @@ -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 = []

Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ pyenchant
pythonwhois
imgurpython
isodate
python-dateutil