forked from samjavner/recipeformats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfdx.py
357 lines (305 loc) · 12.9 KB
/
fdx.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
"""Recipe parsing for Food Data Exchange (Living Cookbook) format (.fdx) files.
Use parse_file to parse a file.
"""
import xml.etree.ElementTree as ET
__all__ = ['parse_file', 'Recipe', 'RecipeIngredient', 'RecipeProcedure',
'RecipeAuthorNote', 'RecipeTip', 'RecipeReview', 'RecipeMeasure',
'RecipeImage']
def parse_file(filename):
"""Parses a .fdx file.
Args:
filename: File name of the .fdx file to parse.
Returns:
A list of Recipe objects.
"""
fdx = ET.parse(filename).getroot()
return [Recipe.parse(e) for e in fdx.findall('./Recipes/Recipe')]
class Recipe:
"""Represents a recipe in a Food Data Exchange .fdx file.
Attributes:
name: Name string.
id: Id string - can be reference from ingredients of another recipe.
cookbook_id: Id string of cookbook.
cookbook_chapter_id: Id string of cookbook chapter.
servings: Number of servings string (perhaps empty).
yield_: Yield string (e.g. 24 cupcakes) (perhaps empty).
oven_temperature_f: Oven temperature in degrees F (perhaps empty).
oven_temperature_c: Oven temperature in degrees C (perhaps empty).
preparation_time: Preparation time in minutes as a string (perhaps empty).
cooking_time: Cooking time in minutes as a string (perhaps empty).
inactive_time: Inactive time in minutes as a string (perhaps empty).
ready_in_time: Ready in time in minutes as a string (perhaps empty).
degree_of_difficulty: Difficulty (1-5) as a string (perhaps empty).
recipe_types: Recipe types as a comma separated string (perhaps empty).
author: Author string (perhaps empty).
source: Source string (perhaps empty).
source_page_number: Source page number string (perhaps empty).
web_page: Web page string (perhaps empty).
copyright: Copyright string (perhaps empty).
comments: Comments string (perhaps empty).
color_flag: Color flag string (one of '<None>', 'Red', 'Blue',
'Yellow', 'Green', 'Orange', or 'Purple').
create_date: Creation date string ('YYYY-MM-DD' format).
recipe_image: RecipeImage object for main recipe image or None.
source_image: RecipeImage object for recipe source or None.
ingredients: List of RecipeIngredient objects.
procedures: List of RecipeProcedure objects.
author_notes: List of RecipeAuthorNote objects.
tips: List of RecipeTip objects.
reviews: List of RecipeReview objects.
measures: List of RecipeMeasure objects.
images: List of RecipeImage objects.
user_data: List of custom user data with length 15 (items may be empty).
nutrition: Dictionary of nutrition values.
Keys are nutrient strings (e.g. 'VitaminA').
Values are decimal string in the nutrient's standard units (e.g. '1.25')
"""
def __init__(self):
self.name = ''
self.id = ''
self.cookbook_id = ''
self.cookbook_chapter_id = ''
self.servings = ''
self.yield_ = ''
self.oven_temperature_f = ''
self.oven_temperature_c = ''
self.preparation_time = ''
self.cooking_time = ''
self.inactive_time = ''
self.ready_in_time = ''
self.degree_of_difficulty = ''
self.recipe_types = ''
self.author = ''
self.source = ''
self.source_page_number = ''
self.web_page = ''
self.copyright = ''
self.comments = ''
self.color_flag = ''
self.create_date = ''
self.recipe_image = None
self.source_image = None
self.ingredients = []
self.procedures = []
self.author_notes = []
self.tips = []
self.reviews = []
self.measures = []
self.images = []
self.user_data = [''] * 15
self.nutrition = {}
@staticmethod
def parse(r):
recipe = Recipe()
recipe.name = r.get('Name', '')
recipe.id = r.get('ID', '')
recipe.cookbook_id = r.get('CookbookID', '')
recipe.cookbook_chapter_id = r.get('CookbookChapterID', '')
recipe.servings = r.get('Servings', '')
recipe.yield_ = r.get('Yield', '')
recipe.oven_temperature_f = r.get('OvenTemperatureF', '')
recipe.oven_temperature_c = r.get('OvenTemperatureC', '')
recipe.preparation_time = r.get('PreparationTime', '')
recipe.cooking_time = r.get('CookingTime', '')
recipe.inactive_time = r.get('InactiveTime', '')
recipe.ready_in_time = r.get('ReadyInTime', '')
recipe.degree_of_difficulty = r.get('DegreeOfDifficulty', '')
recipe.recipe_types = r.get('RecipeTypes', '')
recipe.author = r.get('Author', '')
recipe.source = r.get('Source', '')
recipe.source_page_number = r.get('SourcePageNumber', '')
recipe.web_page = r.get('WebPage', '')
recipe.copyright = r.get('Copyright', '')
recipe.comments = r.get('Comments', '')
recipe.color_flag = r.get('ColorFlag', '')
recipe.create_date = r.get('CreateDate', '')
recipe.recipe_image = RecipeImage.find_and_parse(r, 'RecipeImage')
recipe.source_image = RecipeImage.find_and_parse(r, 'SourceImage')
recipe.ingredients = [RecipeIngredient.parse(e) for e in r.findall('./RecipeIngredients/RecipeIngredient')]
recipe.procedure = [RecipeProcedure.parse(e) for e in r.findall('./RecipeProcedures/RecipeProcedure')]
recipe.author_notes = [RecipeAuthorNote.parse(e) for e in r.findall('./RecipeAuthorNotes/RecipeAuthorNote')]
recipe.tips = [RecipeTip.parse(e) for e in r.findall('./RecipeTips/RecipeTip')]
recipe.reviews = [RecipeReview.parse(e) for e in r.findall('./RecipeReviews/RecipeReview')]
recipe.measures = [RecipeMeasure.parse(e) for e in r.findall('./RecipeMeasures/RecipeMeasure')]
recipe.images = [RecipeImage.parse(e) for e in r.findall('./RecipeImages/RecipeImages')]
recipe.nutrition = {name: value for name, value in _find(r, 'RecipeNutrition').items()}
recipe.user_data = [r.get('UserData' + str(i + 1), '') for i in range(15)]
return recipe
class RecipeIngredient:
"""Represents an ingredient in a Food Data Exchange .fdx file.
Attributes:
quantity: Amount string (perhaps empty) (e.g. '2').
unit: Unit of measure string (perhaps empty) (e.g. 'cups').
ingredient: Ingredient part string (e.g. 'milk').
heading: Heading string ('Y' or 'N').
link_type: Link type string:
'', 'Recipe', 'NoLink', 'Ingredient', 'Unlinked', others?
ingredient_id: ID string of linked ingredient when link_type == 'Ingredient'.
ingredient_name: Name string of linked ingredient when link_type == 'Ingredient'.
measure_id: ID string of measure when link_type == 'Ingredient'.
measure: Quantity string of measure when link_type == 'Ingredient'.
measure_gram_weight: Equivalent number of grams as a string when link_type == 'Ingredient'.
Equivalent to the measure given in measure.
measure_quantity: Number (as a string) to multiply by measure or
measure_gram_weight to get the total quantity when link_type == 'Ingredient'.
Number (as a string) of recipe servings when link_type == 'Recipe'.
recipe_id: ID string of linekd recipe when link_type == 'Recipe'.
recipe_name: Name string of linked recipe when link_type == 'Recipe'.
"""
def __init__(self):
self.quantity = ''
self.unit = ''
self.ingredient = ''
self.heading = ''
self.link_type = ''
self.ingredient_id = ''
self.ingredient_name = ''
self.measure_id = ''
self.measure = ''
self.measure_gram_weight = ''
self.measure_quantity = ''
self.recipe_id = ''
self.recipe_name = ''
@staticmethod
def parse(e):
ingredient = RecipeIngredient()
ingredient.quantity = e.get('Quantity', '')
ingredient.unit = e.get('Unit', '')
ingredient.ingredient = e.get('Ingredient', '')
ingredient.heading = e.get('Heading', '')
ingredient.link_type = e.get('LinkType', '')
ingredient.ingredient_id = e.get('IngredientID', '')
ingredient.ingredient_name = e.get('IngredientName', '')
ingredient.measure_id = e.get('MeasureID', '')
ingredient.measure = e.get('Measure', '')
ingredient.measure_quantity = e.get('MeasureQuantity', '')
ingredient.measure_gram_weight = e.get('MeasureGramWeight', '')
ingredient.recipe_id = e.get('RecipeID', '')
ingredient.recipe_name = e.get('RecipeName', '')
return ingredient
class RecipeProcedure:
"""Represents a procedure in a Food Data Exchange .fdx file.
Attributes:
procedure_text: Text string.
heading: Heading string ('Y' or 'N').
image: Image object or None.
"""
def __init__(self):
self.procedure_text = ''
self.heading = ''
self.procedure_image = None
@staticmethod
def parse(e):
procedure = RecipeProcedure()
procedure.procedure_text = e.findtext('ProcedureText', '').strip()
procedure.heading = e.get('Heading', '')
procedure.procedure_image = RecipeImage.find_and_parse(e, 'ProcedureImage')
return procedure
class RecipeAuthorNote:
"""Represents an author note in a Food Data Exchange .fdx file.
Attributes:
text: Text string.
heading: Heading string ('True' or '')
"""
def __init__(self):
self.text = ''
self.heading = ''
@staticmethod
def parse(e):
author_note = RecipeAuthorNote()
author_note.text = e.text.strip()
author_note.heading = e.get('Heading', '')
return author_note
class RecipeTip:
"""Represents a tip in a Food Data Exchange .fdx file.
Attributes:
text: Text string.
heading: Heading string ('True' or '')
"""
def __init__(self):
self.text = ''
self.heading = ''
@staticmethod
def parse(e):
tip = RecipeTip()
tip.text = e.text.strip()
tip.heading = e.get('Heading', '')
return tip
class RecipeReview:
"""Represents a review in a Food Data Exchange .fdx file.
Attributes:
review_date: Date string ('YYYY-MM-DD' format).
rating: Rating (0-5) as a string.
reviewer: Name of reviewer string.
"""
def __init__(self):
self.review_date = ''
self.rating = ''
self.reviewer = ''
@staticmethod
def parse(e):
review = RecipeReview()
review.review_date = e.get('ReviewDate', '')
review.rating = e.get('Rating', '')
review.reviewer = e.get('Reviewer', '')
return review
class RecipeMeasure:
"""Represents a recipe measurement in a Food Data Exchange .fdx file.
Attributes:
measure_id: ID string.
description: Description string (e.g. '1 cup').
gram_weight: Gram weight string (e.g. '300').
measure_type: Measure type string:
'Volume', 'Mass', 'Weight (Mass)', 'Unit', others?
"""
def __init__(self):
self.measure_id = ''
self.description = ''
self.gram_weight = ''
self.measure_type = ''
@staticmethod
def parse(e):
measure = RecipeMeasure()
measure.measure_id = e.get('MeasureID', '')
measure.description = e.get('Description', '')
measure.gram_weight = e.get('GramWeight', '')
measure.measure_type = e.get('MeasureType', '')
return measure
class RecipeImage:
"""Represents an image in a Food Data Exchange .fdx file.
Attributes:
value: Bytes of image file encoded as a base 64 string.
file_type: Extension of image file name (e.g. 'JPG', 'GIF').
description: Description string for image.
(only used for Recipe.images; not recipe_image, source_image, or procedure_image)
"""
def __init(self):
self.value = ''
self.file_type = ''
self.description = ''
def __repr__(self):
return "{%s} {%s} {%s}" % (self.file_type, self.description, self.value[:50] + '...')
@staticmethod
def find_and_parse(element, tag):
image = element.find(tag)
if image is None:
return None
else:
return RecipeImage.parse(image)
@staticmethod
def parse(e):
image = RecipeImage()
image.value = e.text.strip()
image.file_type = e.get('FileType', '')
image.description = e.get('Description', '')
return image
def _find(element, tag):
"""Finds the first subelement with the given tag name.
Returns the subelement, or an empty element with no attributes or
subelements if no subelement with the given tag name was found.
"""
subelement = element.find(tag)
if subelement is None:
return ET.Element(tag)
else:
return subelement