예제 #1
0
        public InProduct GetProductFromSite(String ProductCode, Boolean Incongito = false)
        {
            if (!Incongito && !LoggedIn)
            {
                Login();
            }

            String Url = "http://www.seclock.com/products/ajax/details.asp?i=" + ProductCode;

            HtmlAgilityPack.HtmlNode.ElementsFlags.Remove("form");
            HtmlAgilityPack.HtmlDocument doc = browser.GetWebRequest(Url);
            var productsSeriesUl             = doc.DocumentNode.SelectNodes("//ul[@class='products']");



            InProduct p = new InProduct();

            p.Code = ProductCode;
            var dbProducts = new InProductManager(Constants.ConnectionString).GetData(p);

            if (dbProducts.Count == 1)
            {
                p = dbProducts[0];
            }

            p.Code = doc.DocumentNode.SelectNodes("h2").First().InnerHtml;
            p.Name = doc.DocumentNode.SelectNodes("h2").First().InnerHtml;

            var img = doc.DocumentNode.SelectNodes("//img[@id='itemPic']");

            p.ImageUrl1 = img == null ? String.Empty : img[0].Attributes["src"].Value;

            Decimal price;

            var listPriceHtml = doc.DocumentNode.SelectNodes("//div[@class='clearfix big-tight']").FirstOrDefault();

            if (!ReferenceEquals(listPriceHtml, null))
            {
                String ListPrice = listPriceHtml.SelectNodes("//div[@class='input list']").First().InnerHtml.Trim().Replace("$", "");
                p.ListPrice = Decimal.TryParse(ListPrice, out price) ? price : 0m;
            }

            var yourPriceHtml = doc.DocumentNode.SelectNodes("//div[@class='clearfix big-tight your']").FirstOrDefault();

            if (!ReferenceEquals(yourPriceHtml, null))
            {
                String YourPrice = yourPriceHtml.SelectNodes("//div[@class='input']").First().InnerHtml.Trim().Replace("$", "");
                p.YourPrice = Decimal.TryParse(YourPrice, out price) ? price : 0m;
            }

            var qty = doc.DocumentNode.SelectNodes("//div[@class='input tight']/h3");
            int stock;

            if (Int32.TryParse(qty[0].InnerHtml.ToLower().Replace("available", "").Trim(), out stock))
            {
                p.Stock = stock.ToString();
            }

            //var productForm = doc.DocumentNode.SelectNodes("div[@class='input tight']/h3");
            //if (!ReferenceEquals(productForm, null))
            //{
            //    var qty = productForm[0].SelectNodes("h3").FirstOrDefault();
            //    if (!ReferenceEquals(qty, null))
            //    {
            //        p.Stock = Convert.ToInt32(qty.InnerHtml.ToLower().Replace("available", "").Trim()).ToString();
            //    }
            //}


            HtmlNode modalBody = doc.DocumentNode.SelectNodes("//div[@id='product-modal-body']").FirstOrDefault();

            if (!ReferenceEquals(modalBody, null))
            {
                var modalBodyP = modalBody.SelectNodes("p");

                int descIndex = 1;
                if (modalBodyP[0].Descendants("img").Count() > 0)
                {
                    descIndex = 2;
                }

                if (!ReferenceEquals(modalBodyP[descIndex], null))
                {
                    var description = modalBodyP[descIndex].InnerHtml;
                    p.Description = description.Trim().Replace('\'', '\"');
                }

                //if (!ReferenceEquals(modalBodyTechDocs, null))
                //{
                //    var techDocs = modalBodyTechDocs.FirstOrDefault().InnerHtml;
                //    p.TechDoc = techDocs.Trim();
                //}

                #region [ Tech Docs Download ]
                var modalBodyTechDocs = modalBody.SelectNodes("//ul[@class='techdocs']");
                if (!ReferenceEquals(modalBodyTechDocs, null))
                {
                    if (modalBodyTechDocs.Count == 2)
                    {
                        List <String> TechDocs = new List <string>();
                        var           hrefs    = modalBodyTechDocs[0].Descendants("a");
                        foreach (var href in hrefs)
                        {
                            String docUrl = href.Attributes["href"].Value;
                            try
                            {
                                if (docUrl.StartsWith("/"))
                                {
                                    docUrl = "http://www.seclock.com/" + docUrl;
                                }

                                String DocFolder = Settings.GetValue("DocFolder");
                                if (!Directory.Exists(DocFolder))
                                {
                                    Directory.CreateDirectory(DocFolder);
                                }

                                String FileName = String.Format("{0}_{1}_{2}", Settings.GetValue("SecLockImagePrefix"), p.Code, docUrl.Substring(docUrl.LastIndexOf("/") + 1));
                                String FilePath = String.Format("{0}\\{1}", DocFolder, FileName);

                                if (File.Exists(FilePath))
                                {
                                    File.Delete(FilePath);
                                }

                                browser.DownloadFile(docUrl, FilePath);
                                TechDocs.Add(FileName);
                            }
                            catch (Exception ex)
                            {
                                Utility.ErrorLog(ex, null);
                                if (Settings.GetValue("MailErrors") == true)
                                {
                                    Utility.ApplicationLog(String.Format("{0}", ex.Message), Constants.EmailErrorFile);
                                }
                            }
                            finally{
                                p.TechDoc = String.Join(";", TechDocs);
                            }
                        }
                    }
                }
                #endregion

                //var manLinksNode =modalBody.SelectNodes("//ul[@class='techdocs']").FirstOrDefault();
                //if (!ReferenceEquals(manLinksNode, null))
                //{
                //    var manuFacturerLinks = manLinksNode.InnerHtml;
                //}
            }
            return(p);
        }