-
Notifications
You must be signed in to change notification settings - Fork 10
/
inventory_grabcronometer.py
executable file
·90 lines (79 loc) · 3.3 KB
/
inventory_grabcronometer.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
#!/usr/bin/python
import sys
import os
from xml.etree import ElementTree
import glob
import re
import datetime
CRONOMETER_FOLDER = "../CRONOMETER-data/foods/"
def cronometer_recipes(inventoryarr):
#execute concatenate_cronometer.sh
foodlist = {}
food = ''
foodroot = ElementTree.parse('complete_cronometer_data.xml').getroot()
foods = foodroot.findall('food')
recipes = foodroot.findall('recipe')
validrecipelist = []
#the CRONOMETER item is either a recipe or an item with a UPC
#check to make sure it's a recipe; should start with <recipe uid="####" ...
#from ####.xml
print "Collecting foods in inventory..."
for inventoryarritems in inventoryarr:
expiryindex = 0
#print inventoryarr[inventoryarritems]
expirylength = inventoryarr[inventoryarritems][0][0]
#print expirylength
for inventoryarritem in inventoryarr[inventoryarritems][1:]:
#print inventoryarritem
daydiff = datetime.date.today() - inventoryarritem
expiryindextmp = float((daydiff.days))/expirylength #numdays in cupboard
#print daydiff
#print float(daydiff.days)
if expiryindextmp > 1:
expiryindextmp = 1
if expiryindextmp > expiryindex:
expiryindex = expiryindextmp
#print expiryindex
foodlist[inventoryarr[inventoryarritems][0][2]] = expiryindex
print "Gathering and sorting matching recipes..."
for recipe in recipes:
ingredients = recipe.iter('serving')
ingmatch = 0
expirymetric = 0
ingcounter = 0
for ingredient in ingredients:
#print ingredient.attrib
ingcounter = ingcounter + 1
if ingredient.attrib['food'] in foodlist.keys():
ingmatch = ingmatch + 1
expirymetric = expirymetric + foodlist[ingredient.attrib['food']]
#print(foodlist[ingredient.attrib['food']])
if ingmatch > 0:
#add the recipe to our list, and determine whether any of the <serving ... food="######" source="My Foods"
#exist in the recipe, if so, add them as an element of the recipe-indexed list.
expirymetric = float(expirymetric)/ingmatch
validrecipelist.append((recipe, float(float(1-float(1/float(ingmatch+0.3))+float(1/float(ingcounter+0.3)))*(float(expirymetric+2)/3.0))))
#print('Recipe found, with metric ' + str(expirymetric) + '; ingmatch is ' + str(ingmatch))
return(sorted(validrecipelist, key = lambda recipedata: recipedata[1], reverse=True))
def cronometer(upc):
foodxml = ''
food = ''
foodroot = ''
for filename in glob.glob(CRONOMETER_FOLDER + '/*.xml'):
foodroot = ElementTree.parse(filename).getroot()
food = foodroot.findall('comments')[0].text
if food is not None:
#print "Found a UPC in the comment."
foodarr = re.split('\r|\t|\n',food)
if upc in foodarr:
print foodroot.attrib['name'] + ", UID = " + foodroot.attrib['uid']
return([foodarr[foodarr.index(upc)+1],foodroot.attrib['name'],foodroot.attrib['uid']])
return(0)
def main():
x = cronometer("0066800000152")
if x == 0:
print "No result."
else:
print "Match found!"
if __name__ == "__main__":
main()