Skip to content

Commit

Permalink
Myplate update (#1468)
Browse files Browse the repository at this point in the history
* new html

* update and groupings
  • Loading branch information
jknndy authored Feb 2, 2025
1 parent 9f9c61c commit dac2517
Show file tree
Hide file tree
Showing 5 changed files with 4,776 additions and 2,362 deletions.
90 changes: 32 additions & 58 deletions recipe_scrapers/usdamyplate.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from ._abstract import AbstractScraper
from ._exceptions import StaticValueException
from ._utils import get_minutes, get_yields, normalize_string
from ._utils import get_minutes, normalize_string
from ._grouping_utils import group_ingredients


class USDAMyPlate(AbstractScraper):
Expand All @@ -11,49 +12,32 @@ def host(cls):
def site_name(self):
raise StaticValueException(return_value="MyPlate")

def title(self):
return self.soup.h1.get_text().strip()
def cook_time(self):
cook_time_span = self.soup.find(
"div", {"class": "mp-recipe-full__detail--cook-time"}
).find("span", {"class": "mp-recipe-full__detail--data"})

def total_time(self):
# not in every recipe has time given
full_detail = self.soup.find(
"div", {"class": "mp-recipe-full__overview desktop:grid-col-5 grid-row"}
)
if cook_time_span:
return get_minutes(cook_time_span)

minutes = 0
for span in full_detail.findAll(
"span", {"class": "mp-recipe-full__detail--data"}
):
if "minute" in span.get_text().lower() or "hour" in span.get_text().lower():
minutes += get_minutes(span)
return 0

if minutes == 0:
return None
def prep_time(self):
prep_time_span = self.soup.find(
"div", {"class": "mp-recipe-full__detail--prep-time"}
).find("span", {"class": "mp-recipe-full__detail--data"})

return minutes
if prep_time_span:
return get_minutes(prep_time_span)

def yields(self):
full_detail = self.soup.find(
"div", {"class": "mp-recipe-full__overview desktop:grid-col-5 grid-row"}
)
return 0

spans = full_detail.findAll("span")
i = 0
for span in spans:
if "Makes:" in span:
return get_yields(spans[i + 1])
i += 1
def total_time(self):
cook_time = self.cook_time()
prep_time = self.prep_time()

def image(self):
div = self.soup.find(
"div",
{
"class": "field field--name-field-recipe-image field--type-image field--label-visually_hidden"
},
)
url = div.find("img")["src"]
# return only the portion before the question mark
return url.split("?")[0]
total_minutes = cook_time + prep_time
return total_minutes

def ingredients(self):
ingredients = self.soup.find(
Expand All @@ -62,6 +46,17 @@ def ingredients(self):

return [normalize_string(paragraph.get_text()) for paragraph in ingredients]

def ingredient_groups(self):
ingredients_section = self.soup.find(
"div", {"class": "field--name-field-ingredients"}
)
return group_ingredients(
self.ingredients(),
ingredients_section,
"b",
"li.field__item",
)

def instructions(self):
div = self.soup.find(
"div",
Expand Down Expand Up @@ -92,24 +87,3 @@ def nutrients(self):
nutrition[el[0]] = el[1]

return nutrition

def serving_size(self):
return normalize_string(
self.soup.find("div", {"class": "field--name-field-recipe-serving-size"})
.find("span", {"class": "field__item"})
.get_text()
)

def description(self):
return normalize_string(
self.soup.find("div", {"class": "mp-recipe-full__description"})
.find("p")
.get_text()
)

def recipe_source(self):
return normalize_string(
self.soup.find("span", {"class": "field--name-field-source"})
.find("p")
.get_text()
)
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"author": "MyPlate",
"canonical_url": "https://www.myplate.gov/recipes/supplemental-nutrition-assistance-program-snap/fabulous-fig-bars",
"canonical_url": "https://www.myplate.gov/recipes/fabulous-fig-bars",
"site_name": "MyPlate",
"host": "myplate.gov",
"language": "en",
Expand Down Expand Up @@ -28,12 +28,11 @@
"Spread fig mixture evenly over the dough. Crumble reserved dough over top, allowing fig mixture to show.",
"Bake 30 minutes or until golden brown. Cool completely in baking pan. Cut into 24 bars (about 2 1/2 x 2 inches)."
],
"category": null,
"yields": "24 bars",
"yields": "24 servings",
"description": "Fig bars are a great on-the-go snack. The sweet, nutty flavors in these bars are sure to be a hit with all ages.",
"total_time": 75,
"cook_time": 1,
"prep_time": 1,
"cook_time": 30,
"prep_time": 45,
"ratings": 3.44,
"ratings_count": 9,
"nutrients": {
Expand All @@ -52,5 +51,5 @@
"Iron": "1 mg",
"Potassium": "169 mg"
},
"image": "https://myplate-prod.azureedge.us/sites/default/files/styles/recipe_525_x_350_/public/2021-03/FigBars.jpg"
"image": "https://myplate-prod.azureedge.us/sites/default/files/styles/medium/public/2021-03/FigBars.jpg?itok=3ZW7jyGj"
}
Loading

0 comments on commit dac2517

Please sign in to comment.