Пример #1
0
        public void PopulateBrands()
        {
            Url = "http://www.gsmarena.com/makers.php3";
            using (var browser = new IE(Url,true))
            {
                try
                {
                    Settings.AttachToBrowserTimeOut = 240;
                    Settings.WaitUntilExistsTimeOut = 240;
                    Settings.WaitForCompleteTimeOut = 240;

                    Thread.Sleep(1000);

                    var shop = new OnlineShoppingModel();

                    Div allBrandsDiv = browser.Div(Find.ById("mid-col"));
                    Table allBrandTable = allBrandsDiv.Tables.First();

                    int index = 1;
                    foreach (TableRow brandRow in allBrandTable.TableRows)
                    {
                        IEnumerable<Link> brandLinks = brandRow.Links.Where(l => l.Images.Count > 0);

                        if (brandLinks.Count() > 0)
                        {
                            foreach (Link brandLink in brandLinks)
                            {
                                Image brandImage = brandLink.Images.First();
                                Link textBrandlink =
                                    brandRow.Links.Where(b => b.Url.Contains(brandLink.Url) && b.Images.Count <= 0).
                                        First();
                                string brandText = textBrandlink.Text;

                                string numberOfProducts =
                                    brandText.Substring(brandText.IndexOf("(")).Replace("(", "").Replace(")", "");

                                var brand = new Brand
                                                {
                                                    Id = index,
                                                    Name = brandImage.Alt,
                                                    Url = brandLink.Url,
                                                    NoOfProducts = int.Parse(numberOfProducts),
                                                    ImageUrl = brandImage.Src
                                                };

                                shop.Brands.Add(brand);
                                index++;
                            }
                        }

                        Database.SetInitializer(new CreateDatabaseIfNotExists<MobilesDbContext>());

                        using (var dbContext = new MobilesDbContext())
                        {
                            List<string> brandUrlList = dbContext.Brands.Select(c => c.Url).ToList();
                            foreach (Brand brand in
                                shop.Brands.Where(c => brandUrlList.Contains(c.Url) == false))
                            {
                                dbContext.Brands.Add(new DataModels.Brand
                                                         {
                                                             BrandName = brand.Name,
                                                             IsActive = true,
                                                             AddedDate = DateTime.Now,
                                                             LastModifiedDate = DateTime.Now,
                                                             Url = brand.Url,
                                                             NumberOfProducts = brand.NoOfProducts,
                                                             ImageUrl = brand.ImageUrl
                                                         });
                            }

                            dbContext.SaveChanges();
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                    Console.ReadLine();
                    //throw ex;
                }
            }
        }
Пример #2
0
        public void PopulateBrands()
        {
            Url = "http://www.gsmarena.com/makers.php3";
            using (var browser = new IE(Url, true))
            {
                try
                {
                    Settings.AttachToBrowserTimeOut = 240;
                    Settings.WaitUntilExistsTimeOut = 240;
                    Settings.WaitForCompleteTimeOut = 240;

                    Thread.Sleep(1000);

                    var shop = new OnlineShoppingModel();

                    Div   allBrandsDiv  = browser.Div(Find.ById("mid-col"));
                    Table allBrandTable = allBrandsDiv.Tables.First();

                    int index = 1;
                    foreach (TableRow brandRow in allBrandTable.TableRows)
                    {
                        IEnumerable <Link> brandLinks = brandRow.Links.Where(l => l.Images.Count > 0);

                        if (brandLinks.Count() > 0)
                        {
                            foreach (Link brandLink in brandLinks)
                            {
                                Image brandImage    = brandLink.Images.First();
                                Link  textBrandlink =
                                    brandRow.Links.Where(b => b.Url.Contains(brandLink.Url) && b.Images.Count <= 0).
                                    First();
                                string brandText = textBrandlink.Text;

                                string numberOfProducts =
                                    brandText.Substring(brandText.IndexOf("(")).Replace("(", "").Replace(")", "");


                                var brand = new Brand
                                {
                                    Id           = index,
                                    Name         = brandImage.Alt,
                                    Url          = brandLink.Url,
                                    NoOfProducts = int.Parse(numberOfProducts),
                                    ImageUrl     = brandImage.Src
                                };

                                shop.Brands.Add(brand);
                                index++;
                            }
                        }

                        Database.SetInitializer(new CreateDatabaseIfNotExists <MobilesDbContext>());

                        using (var dbContext = new MobilesDbContext())
                        {
                            List <string> brandUrlList = dbContext.Brands.Select(c => c.Url).ToList();
                            foreach (Brand brand in
                                     shop.Brands.Where(c => brandUrlList.Contains(c.Url) == false))
                            {
                                dbContext.Brands.Add(new DataModels.Brand
                                {
                                    BrandName        = brand.Name,
                                    IsActive         = true,
                                    AddedDate        = DateTime.Now,
                                    LastModifiedDate = DateTime.Now,
                                    Url = brand.Url,
                                    NumberOfProducts = brand.NoOfProducts,
                                    ImageUrl         = brand.ImageUrl
                                });
                            }

                            dbContext.SaveChanges();
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                    Console.ReadLine();
                    //throw ex;
                }
            }
        }