コード例 #1
0
        /// <summary>
        /// Gets the data.
        /// </summary>
        /// <param name="keyWord">The key word.</param>
        /// <param name="nbItemsPerPage">The nb items per page.</param>
        /// <param name="startIndex">The start index.</param>
        /// <param name="model">The model.</param>
        /// <returns></returns>
        public async Task <bool> GetData(String keyWord, int nbItemsPerPage, int startIndex, AppModel model)
        {
            HttpClient http  = new System.Net.Http.HttpClient();
            bool       error = false;

            model.ClearReceipes();
            try
            {
                foreach (var jsonItem in model.LocalReceipes)
                {
                    Receipe localToAdd = new Receipe(jsonItem.Stringify());
                    if (checkInstructions(localToAdd, keyWord) || checkTitle(localToAdd, keyWord))
                    {
                        addReceipe(localToAdd, model);
                    }
                }
            }catch (Exception ex) {
            }

            try
            {
                HttpResponseMessage response = await http.GetAsync(String.Format(this.URL, keyWord, nbItemsPerPage, startIndex));

                string jsonString = await response.Content.ReadAsStringAsync();

                JsonObject jsonObject = JsonObject.Parse(jsonString);
                JsonArray  jsonArray  = getItemsArrayFromJSONObject(jsonObject);



                foreach (var item in jsonArray)
                {
                    Receipe receipe = getReceipeFromJSONItem(item.GetObject());
                    addReceipe(receipe, model);
                }
            }
            catch (Exception ex)
            {
                error = true;
            }

            return(error);
        }
コード例 #2
0
        /// <summary>
        /// Gets the data by ingredients.
        /// </summary>
        /// <param name="keyWords">The key words.</param>
        /// <param name="nbItemsPerPage">The nb items per page.</param>
        /// <param name="startIndex">The start index.</param>
        /// <param name="model">The model.</param>
        /// <returns></returns>
        public async Task <bool> GetDataByIngredients(String[] keyWords, int nbItemsPerPage, int startIndex, AppModel model)
        {
            bool error = false;

            model.ClearReceipes();
            try
            {
                //we add all receipes created by the user
                foreach (var jsonItem in model.LocalReceipes)
                {
                    Receipe localToAdd = new Receipe(jsonItem.Stringify());
                    addReceipe(localToAdd, model);
                }
            }
            catch (Exception ex)
            {
            }
            try
            {
                List <Receipe>[] results = new List <Receipe> [keyWords.Length];
                for (int i = 0; i < keyWords.Length; i++)
                {
                    results[i]  = new List <Receipe>();
                    keyWords[i] = keyWords[i].ToUpper();
                }
                //we add all basic research associated to each keyword to the model
                foreach (var keyWord in keyWords)
                {
                    await this.GetData(keyWord, nbItemsPerPage, startIndex, model);
                }

                int bestResults = 1;// confidence level

                foreach (var receipe in model.Receipes)
                {
                    //we check the research parameters to go faster
                    if (checkType(receipe))
                    {
                        if (checkDifficulty(receipe))
                        {
                            if (checkOptions(receipe))
                            {
                                //used to compute the confidence level
                                int count = 0;

                                //case of receipes from marmiton
                                if (receipe.Id != -1)
                                {
                                    //retrieves ingredients
                                    ReceipeRetriever rr = new ReceipeRetriever();
                                    var task            = rr.extractReceipeFromMarmiton(receipe);


                                    if ((await task) == true)
                                    {
                                        var task2 = rr.cleanHtmlEntities(receipe.HtmlReceipe, receipe);
                                        rr.handleIngredients(rr.ingredientPart, receipe);
                                    }
                                }

                                //we compute the confidence level
                                foreach (var keyWord in keyWords)
                                {
                                    //each key word in the receipe informations add 1 confidence level
                                    if (checkIngredients(receipe, keyWord) || checkInstructions(receipe, keyWord) || checkTitle(receipe, keyWord))
                                    {
                                        count++;
                                    }
                                }
                                results[count].Add(receipe);
                                if (count > bestResults)
                                {
                                    bestResults = count;
                                }
                            }
                        }
                    }
                }

                model.ClearReceipes();


                //only best results are displayed
                foreach (var receipe in results[bestResults])
                {
                    model.AddReceipe(receipe);
                }
            }
            catch (Exception ex)
            {
                error = true;
            }

            return(error);
        }