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

Add support for fetching individual food items #39

Open
wants to merge 1 commit into
base: master
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
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,30 @@ Asynchronously scrapes nutrient data from a user's food diary on a given date.
- 'sodium'
- 'fiber'
- 'sugar'
- 'entries' (individual food items)
- callback `Function`
- the callback is passed a single argument `data`, which will be an `Object`
whose keys are the nutrient field names and values are each a `Number`, as well as the date.
Eg. `{ 'date': '2014-10-05', 'calories': 2078, 'carbs': 98, 'fat': 119, 'saturated fat': 35, 'protein': 153 }`

The food entries contain the following information

```javascript
entries: [
{
name: "Stiegl - Beer",
amount: "330 ml"
},
{
name: "Vodka shot",
amount: "1 shot"
},
{
name: "Club Mate",
amount: "500 ml"
}
]
```

Example 1:

Expand Down
24 changes: 21 additions & 3 deletions mfp_functions/fetchSingleDate.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,27 @@ var fetchSingleDate = function(username, date, fields, callback){

//add date to results object
results.date = date;

if (fields == 'all' || fields.includes('entries')) {
// show the actual food items the user ate that day
results["entries"] = []

var $foodEntries = $('td[class=first]')
$foodEntries.each(function(entryIndex, entryElement) {
if (entryIndex == 0 || entryIndex == $foodEntries.length - 1) {
// The first and last items are headers that we don't need
return
}

// The code below won't fully work if there is a `, ` in the name
// of the food. For now, this will do the job though
var currentEntry = $(entryElement).text().split(", ")
results["entries"].push({
"name": currentEntry[0],
"amount": currentEntry[currentEntry.length - 1]
})
})
}

//check to see if water is included
if (fields == 'all' || fields.includes('water')) {
Expand All @@ -82,9 +103,6 @@ var fetchSingleDate = function(username, date, fields, callback){
callback(results);
});
} else {



callback(results);
}
});
Expand Down