-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRecipeParser.py
223 lines (195 loc) · 7.04 KB
/
RecipeParser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import sys
sys.path.insert(0, 'lib')
#Not inluded in Google App Engine
from bs4 import BeautifulSoup
from bs4.dammit import EntitySubstitution
#Included in Google App Engine
import urllib2
import json
import logging
import re
from utils import trim, find_image, find_title
def parseRecipeUrlToJsonOld(url):
try:
response = urllib2.urlopen(url)
html = response.read()
soup = BeautifulSoup(html)
return SchemaOrgParser(soup).parseRecipe()
# if (isSchemaOrgStandard(soup)):
# logging.info("html conforms to schema.org standard")
# parser = SchemaOrgParser(soup)
# return parser.parseRecipe()
# elif (isHRecipeStandard(soup)):
# logging.info("html conforms to hrecipe standard")
# parser = HRecipeParser(soup)
# return parser.parseRecipe()
# else:
# logging.info("html does not conform to a recipe standard")
# return json.dumps({"error" : "We had trouble finding a recipe on this page. Try another URL."})
except Exception, e:
logging.debug(e)
return json.dumps({"error" : "We had trouble connecting to that site. Try another URL."})
def parseRecipeUrlToJson(url):
try:
response = urllib2.urlopen(url)
html = response.read()
soup = BeautifulSoup(html)
if (isSchemaOrgStandard(soup)):
logging.info("html conforms to schema.org standard")
parser = SchemaOrgParser(soup)
return parser.parseRecipe()
elif (isHRecipeStandard(soup)):
logging.info("html conforms to hrecipe standard")
parser = HRecipeParser(soup)
return parser.parseRecipe()
else:
logging.info("html does not conform to a recipe standard. parsing what we can")
parser = RecipeParser(soup)
return parser.parseRecipe()
except Exception, e:
logging.debug(e)
return json.dumps({"error" : "We had trouble connecting to that site. Try again later or try another URL."})
def getRecipeIngredientsFromSoup(soup):
if (isSchemaOrgStandard(soup)):
logging.info("html conforms to schema.org standard")
return [{"ingredient" : ingredient} for ingredient in SchemaOrgParser(soup).parseRecipeIngredients()]
elif (isHRecipeStandard(soup)):
logging.info("html conforms to hrecipe standard")
return [{"ingredient" : ingredient} for ingredient in HRecipeParser(soup).parseRecipeIngredients()]
else:
logging.info("html does not conform to a recipe standard")
return []
def getRecipeInstructionsFromSoup(soup):
if (isSchemaOrgStandard(soup)):
logging.info("html conforms to schema.org standard")
return [{"instruction" : instruction} for instruction in SchemaOrgParser(soup).parseRecipeInstructions()]
elif (isHRecipeStandard(soup)):
logging.info("html conforms to hrecipe standard")
return [{"instruction" : instruction} for instruction in HRecipeParser(soup).parseRecipeInstructions()]
else:
logging.info("html does not conform to a recipe standard")
return []
def isSchemaOrgStandard(soup):
try:
return soup.findAll(True, {"itemtype" : re.compile("http://schema.org/Recipe", re.IGNORECASE)}) != []
except Exception, e:
logging.debug(e)
return False
def isHRecipeStandard(soup):
try:
return soup.findAll(True, {"class" : re.compile("hrecipe", re.IGNORECASE)}) != []
except Exception, e:
logging.debug(e)
return False
# Parent class of recipe parsers. Only used if we can't match a more specific schema
class RecipeParser(object):
def __init__(self, soup):
self.soup = soup
def parseRecipe(self):
self.site_feed_url = ""
self.site_name = self.parseRecipeSiteName()
self.page_url = self.parseRecipeUrl()
self.page_title = self.parseRecipeName()
self.page_description = self.parseRecipeDescription()
self.page_image_url = self.parseRecipeImage()
self.page_date_published = ""
self.page_recipe_ingredients = self.parseRecipeIngredients()
self.page_recipe_instructions = self.parseRecipeInstructions()
result = json.dumps({"recipe" : {
"site_feed_url" : self.site_feed_url,
"site_name" : self.site_name,
"page_url" : self.page_url,
"page_title" : self.page_title,
"page_description" : self.page_description,
"page_image_url" : self.page_image_url,
"page_date_published" : self.page_date_published,
"page_recipe_ingredients" : [{"ingredient" : ingredient} for ingredient in self.page_recipe_ingredients],
"page_recipe_instructions" : [{"instruction" : instruction} for instruction in self.page_recipe_instructions]
}})
logging.info(result)
return result
def parseRecipeSiteName(self):
try:
return trim(self.soup.findAll("meta", {"property" : re.compile("og:site_name", re.IGNORECASE)})[0].get("content"))
except:
return ""
def parseRecipeUrl(self):
try:
url = trim(self.soup.findAll("link", {"rel" : re.compile("canonical", re.IGNORECASE)})[0].get("href"))
logging.info("found canonical url: %s", url)
return url
except:
logging.info("couldn't find canonical url")
return ""
def parseRecipeName(self):
try:
title = find_title(self.page_url, self.soup)
if not title:
return ""
else:
return title
except:
return ""
def parseRecipeImage(self):
try:
image_url = find_image(self.page_url, self.soup)
if not image_url:
return ""
else:
return image_url
except:
return ""
def parseRecipeDescription(self):
try:
return trim(self.soup.findAll("meta", {"property" : re.compile("og:description", re.IGNORECASE)})[0].get("content"))
except:
return ""
def parseRecipeIngredients(self):
return []
def parseRecipeInstructions(self):
return []
class SchemaOrgParser(RecipeParser):
def parseRecipeDescription(self):
try:
description = super(SchemaOrgParser, self).parseRecipeDescription()
if not description or description == "":
return trim(self.soup.findAll(True, {"itemprop" : re.compile("description", re.IGNORECASE)})[0].get_text())
else:
return description
except Exception, e:
logging.info(e)
return ""
def parseRecipeIngredients(self):
try:
return [trim(ingredient.get_text()) for ingredient in self.soup.findAll(True, {"itemprop" : re.compile("ingredients", re.IGNORECASE)})]
except:
return []
def parseRecipeInstructions(self):
try:
instructions = self.soup.findAll(True, {"itemprop" : re.compile("recipeInstructions", re.IGNORECASE)})
if (len(instructions) > 1):
return [trim(instruction.get_text()) for instruction in instructions]
else:
return [trim(instruction) for instruction in instructions[0].stripped_strings]
except:
return []
class HRecipeParser(RecipeParser):
def parseRecipeDescription(self):
try:
description = super(HRecipeParser, self).parseRecipeDescription()
if not description or description == "":
return trim(self.soup.findAll(True, {"class" : re.compile("summary", re.IGNORECASE)})[0].get_text())
else:
return description
except:
return ""
def parseRecipeIngredients(self):
try:
return [trim(ingredient.get_text()) for ingredient in self.soup.findAll(True, {"class" : re.compile("ingredient", re.IGNORECASE)})]
except:
return []
def parseRecipeInstructions(self):
try:
return [trim(instruction) for instruction in self.soup.findAll(True, {"class" : re.compile("instructions", re.IGNORECASE)})[0].stripped_strings]
except:
return []