Alan helps you even in the kitchen
private async Task SearchRecipe()
{
await GUIOutput("What do you want to cook?", true);
SpeechRecognitionResult food = await task_recognizer.RecognizeAsync();
await GUIOutput("Searching a recipe for " + food.Text, false);
try
{
var http = new HttpClient();
var response = await http.GetAsync("https://api.edamam.com/search?q=" + food.Text + "&app_id=b4a3fd65&app_key=64c81e2e1ae114c948f814fc9c31041f&from=0&to=1");
string recipe = await response.Content.ReadAsStringAsync();
int idx1 = recipe.IndexOf("\"ingredientLines\" : [ \"") + "\"ingredientLines\" : [ \"".Length;
int idx2 = recipe.IndexOf("\"ingredients\"") - 11;
string ingredients = recipe.Substring(idx1, idx2 - idx1);
ingredients = ingredients.Replace("\", \"", ";");
var ingredients_list = ingredients.Split(';');
idx1 = recipe.IndexOf("\"label\" : \"") + "\"label\" : \"".Length;
idx2 = recipe.IndexOf("\"image\"") - 9;
string recipe_name = recipe.Substring(idx1, idx2 - idx1);
idx1 = recipe.IndexOf("\"image\" : \"") + "\"image\" : \"".Length;
idx2 = recipe.IndexOf("\"source\"") - 9;
string recipe_photo = recipe.Substring(idx1, idx2 - idx1);
await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
() =>
{
main_page.SwitchToRecipeGrid();
main_page.SetRecipeName(recipe_name);
main_page.SetRecipePhoto(recipe_photo);
main_page.SetRecipeIngredients(ingredients_list);
});
// Stay in the recipe grid until the user clicks the exit button
in_recipe_view = true;
while(in_recipe_view);
await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
() =>
{
main_page.SwitchToDefaultGrid();
});
}
catch
{
await GUIOutput("I'm sorry, I couldn't retrieve the informations from the web.", true);
}
state = AssistantState.BACKGROUND_DEFAULT;
}
// Handles return to default grid from recipe grid
public void RecipeGridToDefaultGrid() { in_recipe_view = false; }
This functionality is also very similar to the "Inspire me" one. In this case, however,
we don't obtain a JSON object but rather a string with all the informations we need: in
order to extract and isolate the various parts that compose this string, the function
SearchRecipe
makes a "manual" parsing and then prints on the screen all the
information. Since the user may want to stay on this screen for an undefined amount of
time, the application stays in a void loop until the X button is pressed.