예제 #1
0
        // GET api/products/5
        public async Task<Product> Get(int id)
        {
            Product result = new Product();
            HtmlDocument myPageSource = null;
            bool tryWithEmptyString = false;
            try
            {
                myPageSource = await this.pageRetrieverFactory.ProductDetailsPageRetriever.RetrievePageHtml(id.ToString());
            }
            catch
            {
                tryWithEmptyString = true;
            }

            if (tryWithEmptyString)
            {
                myPageSource = await this.pageRetrieverFactory.ProductDetailsPageRetriever.RetrievePageHtml(String.Empty);
            }

            string textWithProductDescription = String.Empty;
            foreach (HtmlNode link in myPageSource.DocumentNode.SelectNodes("//td"))
            {
                if (link.InnerText.Contains(id.ToString()))
                {
                    textWithProductDescription = link.InnerText;
                }
            }

            List<string> linkText = new List<string>();
            string[] splitInnerText = textWithProductDescription.Split(new char[] { '\t', '\r', '\n', '|' }, StringSplitOptions.RemoveEmptyEntries);
            if (textWithProductDescription.Length > 0)
            {
                for (int i = 0; i < splitInnerText.Length - 1; i++)
                {
                    splitInnerText[i] = splitInnerText[i].Trim();
                    if (splitInnerText[i] != String.Empty)
                    {
                        linkText.Add(splitInnerText[i]);
                    }
                }

                int productNameIndex = 0;
                int containerFormatIndex = 2;
                TextInfo textInfo = CultureInfo.CurrentCulture.TextInfo;
                result.Name = textInfo.ToTitleCase(linkText[productNameIndex].ToLower(CultureInfo.CurrentCulture));
                result.Id = id;
                PopulateContainer(result, linkText[containerFormatIndex]);
            }

            return result;
        }
예제 #2
0
        // GET api/products
        public async Task<IEnumerable<Product>> Get()
        {
            Dictionary<string, Product> resultDictionary = new Dictionary<string, Product>();

            var doc = await this.pageRetrieverFactory.ProductListPageRetriever.RetrievePageHtml(String.Empty);

            var productNodes = doc.DocumentNode.SelectNodes(String.Format("//a[@id[starts-with(.,'{0}')]]", "col"));
            if (productNodes != null)
            {
                foreach (HtmlNode link in productNodes)
                {
                    HtmlAttribute linkId = link.Attributes["id"];
                    string idName = linkId.Value;
                    string idNumber = idName.Substring("col".Length, idName.Length - "col".Length - 1);
                    Product currentProduct;
                    if (!resultDictionary.ContainsKey(idNumber))
                    {
                        currentProduct = new Product();
                        resultDictionary.Add(idNumber, currentProduct);
                    }
                    else
                    {
                        currentProduct = resultDictionary[idNumber];
                    }

                    char idNameLastChar = idName[idName.Length - 1];
                    switch (idNameLastChar)
                    {
                        case '1':
                            TextInfo textInfo = CultureInfo.CurrentCulture.TextInfo;
                            currentProduct.Name = textInfo.ToTitleCase((link.InnerText.ToLower(CultureInfo.CurrentCulture)));
                            break;
                        case '2':
                            ProductsController.GetDetails(link.InnerText, currentProduct);
                            break;
                        default:
                            break;
                    }
                }
            }

            // Filter out products with ID=0, which are non-alcoholic products, like gift bags
            var resultsWithNoId0Products = from p in resultDictionary.Values where p.Id != 0 select p;
            return resultsWithNoId0Products.ToList();
        }
예제 #3
0
        /// <summary>
        /// Gets the details.
        /// </summary>
        /// <param name="productText">The product text.</param>
        /// <param name="product">The product.</param>
        private static void GetDetails(string productText, Product product)
        {
            string[] splitInnerText = productText.Split(new char[] { '\t', '\r', '\n', '|' }, StringSplitOptions.RemoveEmptyEntries);
            List<string> linkText = new List<string>();
            if (splitInnerText.Length > 0)
            {
                for (int i = 0; i < splitInnerText.Length - 1; i++)
                {
                    splitInnerText[i] = splitInnerText[i].Trim();
                    if (splitInnerText[i] != String.Empty)
                    {
                        linkText.Add(splitInnerText[i]);
                    }
                }
            }

            // Container
            product.Container = new Container();
            if (linkText.Count >= 6)
            {
                PopulateContainer(product, linkText[4]);

                // Id
                product.Id = Convert.ToInt32(linkText[3]);

                // Origin
                product.CountryOfOrigin = linkText[0];

                // Manufacturer
                product.Manufacturer = linkText[1];

                // Price
                string[] priceSplit = linkText[5].Split();
                if (priceSplit.Length >= 2)
                {
                    product.Price = Convert.ToDecimal(priceSplit[1]);
                }
            }
            //TODO: Case when items<5 i.e. it's not a drink
        }
예제 #4
0
 private static void PopulateContainer(Product product, string linkText)
 {
     string[] containerSizeSplit;
     if (!linkText.Contains("x"))
     {
         containerSizeSplit = linkText.Split();
         product.Container.NumberOfContainers = 1;
         product.Container.ContainerCapacity = Convert.ToDecimal(containerSizeSplit[0]);
     }
     else
     {
         containerSizeSplit = linkText.Split(new char[] { ' ', 'x' });
         product.Container.NumberOfContainers = Convert.ToInt32(containerSizeSplit[0]);
         product.Container.ContainerCapacity = Convert.ToDecimal(containerSizeSplit[1]);
     }
 }