def buildRecipeDatabase(recipeFilename, totalResults): print "... Creating Recipe Database with %d recipes ..." % totalResults numSteps = getNumSteps(totalResults) recipeDatabase = {} count = 0 for i in range(numSteps): brokeRequest = True start, maxResults = getStartAndMaxResults(i, numSteps, totalResults) print "... Processing recipes: %d to %d ..." % (start + 1, start + maxResults) # print "... start: %d, maxResults: %d ..." % (start, maxResults) # allRecipesFile = open(recipeFilename, 'a') while brokeRequest: apiSearchString = "http://api.yummly.com/v1/api/recipes?_app_id=%s&_app_key=%s&q=&allowedCourse[]=%s&maxResult=%d&start=%d" % (YUM_APP_ID, YUM_APP_KEY, c.YUM_ALLOWED_COURSE, maxResults, start) searchRequest = util.safeConnect(requests.get, tuple([apiSearchString])) brokeRequest = not (searchRequest.status_code == 200) # check out BROKEREQUEST!!! allRecipes = json.loads(searchRequest.content) matches = allRecipes["matches"] for recipe in matches: recipeName, recipeObj = buildRecipeEntry(recipe) recipeDatabase[recipeName] = recipeObj count += 1 if c.PRINT_RECIPE_IN_DATABASE: print "--> recipe %d: %s" % (count, recipeName) print "--> len of recipeDatabase = %d" % len(recipeDatabase) jsonRecipeDatabase = json.dumps(recipeDatabase, sort_keys=True, indent=4) print "--> len of recipeDatabase = %d" % len(recipeDatabase) allRecipesFile = open(recipeFilename + ".json", 'a') allRecipesFile.write(jsonRecipeDatabase) allRecipesFile.close() print "... Done creating Recipe Database with %d recipes ..." % totalResults
def buildRecipeEntry(recipe): recipeName = recipe['recipeName'] recipeId= recipe['id'] ingredients = recipe['ingredients'] #Commenting out to test recipe dump. #addIngredientToNutritionalList(ingredients) brokeRequest = True while brokeRequest: apiGetString = "http://api.yummly.com/v1/api/recipe/%s?_app_id=4d1d7424&_app_key=419a5ef2649eb3b6e359b7a9de93e905" % recipeId getRequest = util.safeConnect(requests.get, tuple([apiGetString])) if getRequest == None: return None brokeRequest = not (getRequest.status_code == 200) getRecipe = json.loads(getRequest.content) ingredientLines = getRecipe["ingredientLines"] recipeObj = {'recipeName': recipeName, 'recipeId': recipeId, 'ingredients': ingredients, 'ingredientLines': ingredientLines, 'cuisine': recipe['attributes'].get('cuisine'), 'course': recipe['attributes'].get('course'), 'totalTimeInSeconds': recipe.get('totalTimeInSeconds'), 'flavors': recipe.get('flavors')} return recipeName, recipeObj
def buildOnlyRecipeDatabase(recipeFilename, totalResults, startIndex): # Set up loop parameters. numSteps = getNumSteps(totalResults) recipeDatabase = {} # Loops once for every YUM_STEPS recipes. for i in range(numSteps): brokeRequest = True start, maxResults = indexedGetStartAndMaxResults(i, numSteps, totalResults, startIndex) processUpdateStatement = "... Processing recipes: %d to %d ..." % (start + 1, start + maxResults) sys.stdout.write(processUpdateStatement + '\n') while brokeRequest: apiSearchString = "http://api.yummly.com/v1/api/recipes?_app_id=%s&_app_key=%s&q=&allowedCourse[]=%s&maxResult=%d&start=%d" % (YUM_APP_ID, YUM_APP_KEY, c.YUM_ALLOWED_COURSE, maxResults, start) searchRequest = util.safeConnect(requests.get, tuple([apiSearchString])) if searchRequest == None: print "\n\nTHE FOLLOWING RANGE IS BAD: %d to %d" % (start + 1, start + maxResults) print "\n\n" #EXIT brokeRequest = not (searchRequest.status_code == 200) # check out BROKEREQUEST!!! # Creates recipes dict from the loaded json file allRecipes = json.loads(searchRequest.content) matches = allRecipes["matches"] for recipe in matches: recipeName, recipeObj = buildRecipeEntry(recipe) if recipeObj == None: print "\n\nTHE FOLLOWING RANGE IS BAD: %d to %d" % (start + 1, start + maxResults) print "\n\n" #EXIT recipeDatabase[recipeName] = recipeObj # Dumps and prints out a .JSON file for every YUM_STEP recipes retrieved. jsonRecipeDatabase = json.dumps(recipeDatabase, sort_keys=True, indent=4) #Filename is /res/jsonrecipes/jsonrecipe_index, where index is startIndex/100. targetFileName = recipeFilename + "_" + str(startIndex/c.YUM_STEP + i) + ".json" allRecipesFile = open(targetFileName, 'a') allRecipesFile.write(jsonRecipeDatabase) allRecipesFile.close() #Adding to clears dict so that next iteration will print clean. recipeDatabase.clear()