public async Task <bool> GetShops(String locality, AppModel model)
        {
            string postData = "quoiqui=supermarché&ou=" + locality;

            byte[]     byteArray = Encoding.UTF8.GetBytes(postData);
            WebRequest request   = WebRequest.Create(this.URL);

            request.Method      = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            Stream dataStream = await request.GetRequestStreamAsync();

            dataStream.Write(byteArray, 0, byteArray.Length);
            WebResponse response2 = await request.GetResponseAsync();

            dataStream = response2.GetResponseStream();
            StreamReader reader             = new StreamReader(dataStream);
            string       responseFromServer = reader.ReadToEnd();

            HtmlDocument document = new HtmlDocument();

            document.LoadHtml(responseFromServer);
            IEnumerable <HtmlNode> shopsNodes = document.DocumentNode.Descendants("div").Where(d => d.Attributes.Contains("class") && d.Attributes["class"].Value.Contains("visitCardContent"));

            foreach (HtmlNode shopNode in shopsNodes)
            {
                HtmlNode titleNode = shopNode.Descendants("h2").ElementAt(0);
                String   title     = titleNode.Descendants("span").Where(d => !d.Attributes.Contains("class")).ElementAt(0).InnerText.Replace("&#039;", "'");

                HtmlNode localisationBlock = shopNode.Descendants("div").Where(d => d.Attributes.Contains("class") && d.Attributes["class"].Value.Contains("localisationBlock")).ElementAt(0);
                String   address           = localisationBlock.Descendants("p").ElementAt(0).InnerHtml.Replace("<br>", " ").Replace("&nbsp;", " ").Replace("<strong>", "").Replace("</strong>", "");

                model.AddShop(new Shop(title, address));
            }

            return(true);
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 3
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);
        }