예제 #1
0
        public void PopulateMobileSpecifications(bool evenId)
        {
            Settings.AttachToBrowserTimeOut = 240;
            Settings.WaitUntilExistsTimeOut = 240;
            Settings.WaitForCompleteTimeOut = 240;
            Database.SetInitializer(new CreateDatabaseIfNotExists<MobilesDbContext>());

            using (var dbContext = new MobilesDbContext())
            {
                try
                {
                    List<Product> dbProducts = dbContext.Products.Where(p => p.IsRead == false && ((p.Id%2)==0)==evenId).ToList();
                    foreach (var dbProduct in dbProducts)
                    {
                       // KillIeProcesses();
                        string url = dbProduct.Url;

                        using (var browser = new IE(url, true))
                        {
                            //browser.ShowWindow(NativeMethods.WindowShowStyle.ForceMinimized);
                            //browser.WaitForComplete();

                            Div specificationsDiv = browser.Div(Find.ById("specs-list"));
                            var productDetails = new ProductDetails();

                            foreach (Table table in specificationsDiv.Tables)
                            {
                                var specification = new Specification();
                                Element specificationTypeCell =
                                    table.TableRows.First().Children().Where(c => c.TagName.ToUpper() == "TH").First();
                                if (specificationTypeCell != null && specificationTypeCell.Exists)
                                {
                                    specification.Type = specificationTypeCell.Text;
                                }

                                foreach (TableRow tableRow in table.TableRows)
                                {
                                    if (tableRow.TableCells.Count > 1)
                                    {
                                        TableCell keyCell = tableRow.TableCells.FirstOrDefault();
                                        TableCell valueCell = tableRow.TableCells.LastOrDefault();

                                        if (keyCell != null && keyCell.Exists && valueCell != null && valueCell.Exists)
                                        {
                                            if (keyCell.Links.Count > 0)
                                            {
                                                var specDetail = new SpecificationDetail();
                                                specDetail.Key = keyCell.Links.First().Text;
                                                specDetail.Value = valueCell.Text;
                                                specification.Details.Add(specDetail);
                                            }
                                            else if (specification.Details.Count > 0)
                                            {
                                                specification.Details.Last().Value = string.Format("{0}{1}",
                                                                                                   specification.Details
                                                                                                       .Last().
                                                                                                       Value,
                                                                                                   valueCell.Text);
                                            }
                                        }
                                    }
                                }

                                productDetails.Specifications.Add(specification);
                                dbProduct.IsRead = true;
                            }

                            GenerateProductDetails(dbProduct,productDetails, dbContext);
                            dbContext.SaveChanges();

                        }
                    }

                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                    Console.ReadLine();
                    //throw ex;
                }
            }
        }
예제 #2
0
        public Product GenerateProductDetails(Product product, ProductDetails specdetails, MobilesDbContext dbContext)
        {
            int index = 1;
            List<SpecifictionType> specTypeList = dbContext.SpecifictionTypes.ToList();
            List<SpecifictionHeading> specHeadingList = dbContext.SpecifictionHeadings.ToList();

            foreach (Specification specification in specdetails.Specifications)
            {
                SpecifictionType specType = specTypeList.Where(s => s.Name == specification.Type).FirstOrDefault();
                if (specType == null)
                {
                    specType = new SpecifictionType {Name = specification.Type};
                    dbContext.SpecifictionTypes.Add(specType);
                }

                var dbSpecifiction = new Specifiction { Type = specType, Product = product };

                foreach (SpecificationDetail specDetail in specification.Details)
                {
                    SpecifictionHeading specHeading =
                        specHeadingList.Where(s => s.Name == specDetail.Key).FirstOrDefault();
                    if (specHeading == null)
                    {
                        specHeading = new SpecifictionHeading {Name = specDetail.Key};
                        dbContext.SpecifictionHeadings.Add(specHeading);
                    }

                    var dbSpecDetail = new SpecificatonDetail
                                           {
                                               Heading = specHeading,
                                               Specifiction = dbSpecifiction,
                                               Value = specDetail.Value
                                           };
                    dbSpecifiction.SpecificatonDetails.Add(dbSpecDetail);
                }
                product.Specifictions.Add(dbSpecifiction);
            }
            return product;
        }