palltaruo commited on
Commit
e23e27b
·
verified ·
1 Parent(s): d449ca4

Update tools/recipe_tools.py

Browse files
Files changed (1) hide show
  1. tools/recipe_tools.py +44 -15
tools/recipe_tools.py CHANGED
@@ -49,29 +49,58 @@ def parse_recipe(url: str) -> Dict[str, Any]:
49
  'servings': None
50
  }
51
 
52
- # Get the webpage content
53
- content = '' # You'll get this from the webpage
54
 
55
- if content:
56
- soup = BeautifulSoup(content, 'html.parser')
 
57
 
58
- # Look for common ingredient list patterns
59
- ingredient_elements = soup.find_all(['li', 'div'], class_=re.compile(r'ingredient|ingredients', re.I))
60
- for element in ingredient_elements:
61
- text = element.get_text().strip()
62
- if text and len(text) < 200: # Basic validation
63
- recipe_data['ingredients'].append(text)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64
 
65
- # Look for cooking instructions
66
- instruction_elements = soup.find_all(['li', 'div'], class_=re.compile(r'instruction|directions|steps', re.I))
67
- for element in instruction_elements:
 
68
  text = element.get_text().strip()
69
- if text and len(text) < 500: # Basic validation
70
- recipe_data['instructions'].append(text)
 
71
 
72
  return recipe_data
73
 
74
  except Exception as e:
 
75
  return {'error': f"Error parsing recipe: {str(e)}"}
76
 
77
  @tool
 
49
  'servings': None
50
  }
51
 
52
+ # Get the webpage content using visit_webpage tool
53
+ webpage_content = visit_webpage.forward(url)
54
 
55
+ if webpage_content:
56
+ # Parse with BeautifulSoup
57
+ soup = BeautifulSoup(webpage_content, 'html.parser')
58
 
59
+ # Common recipe website classes/IDs to look for
60
+ ingredient_patterns = [
61
+ 'ingredient', 'ingredients',
62
+ 'recipe-ingredients', 'recipe__ingredient',
63
+ 'ingredients-list', 'recipe-ingreds'
64
+ ]
65
+
66
+ instruction_patterns = [
67
+ 'instruction', 'instructions',
68
+ 'recipe-instructions', 'recipe__instruction',
69
+ 'preparation', 'prep-steps',
70
+ 'recipe-steps', 'recipe-directions'
71
+ ]
72
+
73
+ # Look for ingredients
74
+ for pattern in ingredient_patterns:
75
+ elements = soup.find_all(['li', 'div', 'span', 'p'],
76
+ class_=re.compile(pattern, re.I))
77
+ for element in elements:
78
+ text = element.get_text().strip()
79
+ if text and len(text) < 200 and text not in recipe_data['ingredients']:
80
+ recipe_data['ingredients'].append(text)
81
+
82
+ # Look for instructions
83
+ for pattern in instruction_patterns:
84
+ elements = soup.find_all(['li', 'div', 'span', 'p'],
85
+ class_=re.compile(pattern, re.I))
86
+ for element in elements:
87
+ text = element.get_text().strip()
88
+ if text and len(text) < 500 and text not in recipe_data['instructions']:
89
+ recipe_data['instructions'].append(text)
90
 
91
+ # Look for servings
92
+ servings_elements = soup.find_all(['div', 'span', 'p'],
93
+ class_=re.compile('yield|servings|serves', re.I))
94
+ for element in servings_elements:
95
  text = element.get_text().strip()
96
+ if 'serv' in text.lower():
97
+ recipe_data['servings'] = text
98
+ break
99
 
100
  return recipe_data
101
 
102
  except Exception as e:
103
+ print(f"Error parsing recipe: {str(e)}")
104
  return {'error': f"Error parsing recipe: {str(e)}"}
105
 
106
  @tool