Esempio n. 1
0
        /// <summary>
        /// gets the amazon link of a post from the website
        /// </summary>
        /// <param name="driver"></param>
        /// <param name="wait"></param>
        /// <param name="post"></param>
        /// <returns></returns>
        public static String getLink(IWebDriver driver, WebDriverWait wait, Post post)
        {
            int    totalPages;
            String tag = Wordpress.SendGetTag(Wordpress.GetTag(post));

            driver.FindElement(By.XPath("//div [@class = 'wp-menu-image dashicons-before dashicons-admin-post']")).Click();
            if (driver.FindElements(By.XPath("//span [@class = 'total-pages']")).Count > 0)
            {
                totalPages = int.Parse(driver.FindElement(By.XPath("//span [@class = 'total-pages']")).Text);
            }
            else
            {
                totalPages = 1;
            }

            for (int x = 1; x <= totalPages; x++)
            {
                List <IWebElement> elements = driver.FindElements(By.XPath("//td [@class = 'tags column-tags']")).ToList();
                foreach (IWebElement element in elements)
                {
                    if (element.Text == tag)
                    {
                        return(driver.FindElement(By.XPath("(//span [@class = 'url'])[" + (elements.IndexOf(element) + 1).ToString() + "]")).GetAttribute("innerHTML"));
                    }
                }
                if (x != totalPages)
                {
                    driver.FindElement(By.XPath("//a [@class = 'next-page button']")).Click();
                    wait.Until(ExpectedConditions.PresenceOfAllElementsLocatedBy(By.XPath("//td [@id = 'cb']")));
                }
            }
            MessageBox.Show("Link not found");
            return("Not Found");
        }
Esempio n. 2
0
        public static void WriteProducts()
        {
            SqlCommand cmd = new SqlCommand("TRUNCATE TABLE Products", con);

            cmd.ExecuteNonQuery();
            List <Post> WP = Wordpress.SendGetPosts(Wordpress.GetPosts());

            foreach (Post post in WP)
            {
                String id       = Wordpress.SendGetTag(Wordpress.GetTag(post));
                String name     = post.Title.Rendered;
                int    price    = int.Parse(Formatting.GetPrice(post.Content.Rendered));
                int    xprice   = int.Parse(Formatting.GetXprice(post.Content.Rendered));
                String category = Formatting.CatIDtoCategory(post.Categories[0]);
                String url      = post.Link;

                cmd.CommandText = $"INSERT INTO Products VALUES ('{id}', '{name}', {price}, {xprice}, '{category}', '{url}');";
                cmd.ExecuteNonQuery();
            }
        }
Esempio n. 3
0
        public static void WriteHHPosts()
        {
            IWorkbook wb = new XSSFWorkbook();
            ISheet    ws = wb.CreateSheet();

            IRow header = ws.CreateRow(0);

            header.CreateCell(0).SetCellValue("Name");
            header.CreateCell(1).SetCellValue("Price");
            header.CreateCell(2).SetCellValue("Old Price");
            header.CreateCell(3).SetCellValue("Difference");
            header.CreateCell(4).SetCellValue("Category");
            header.CreateCell(5).SetCellValue("ID");
            header.CreateCell(6).SetCellValue("URL");

            int         rowcount = 1;
            List <Post> WP       = Wordpress.SendGetPosts(Wordpress.GetPosts());

            foreach (Post post in WP)
            {
                IRow row = ws.CreateRow(rowcount);

                row.CreateCell(0).SetCellValue(post.Title.Rendered);
                row.CreateCell(1).SetCellValue(int.Parse(Formatting.GetPrice(post.Content.Rendered)));
                row.CreateCell(2).SetCellValue(int.Parse(Formatting.GetXprice(post.Content.Rendered)));
                row.CreateCell(3).SetCellValue(int.Parse(Formatting.GetXprice(post.Content.Rendered)) - int.Parse(Formatting.GetPrice(post.Content.Rendered)));
                row.CreateCell(4).SetCellValue(Formatting.CatIDtoCategory(post.Categories[0]));
                row.CreateCell(5).SetCellValue(Wordpress.SendGetTag(Wordpress.GetTag(post)));
                row.CreateCell(6).SetCellValue(post.Link);

                rowcount++;
            }

            ws.CreateRow(rowcount).CreateCell(0).SetCellValue("Stop");
            rowcount++;
            ws.CreateRow(rowcount).CreateCell(0).SetCellValue(DateTime.Now.ToString("D"));

            wb.Write(new FileStream(@"C:\Users\email\Desktop\Hardware Hub\HHPosts.xlsx", FileMode.Create, FileAccess.Write, FileShare.ReadWrite));
            wb.Close();
        }
Esempio n. 4
0
        public static void Main(string[] args)
        {
            SQL.con.Open();

            #region populate Products Only
            //SQL.WriteProducts();
            #endregion

            //Grabs last week's posts straight from website
            Product.updates.Clear();

            //Reads website and populates updates list with existing posts
            Wordpress.ReadWebsite().Wait();

            //Goes to the amazon page of each existing product (in used list) and updates the sale information
            Selenium.GetOldPostData();

            //Updates the posts on the website with the updates sale information for each product
            Wordpress.UpdatePosts().Wait();

            Product.products.Clear();

            //Grabs New products from excel, adds to SQL, and populates products list
            Excel.ReadProducts();
            SQL.WriteNewProducts();

            //populate products list with new products to be added
            //SQL.ReadNewProducts();
            //excel.ReadProducts();

            //Correct the categories so they fit the website
            Formatting.CorrectCategories();

            //// Remove any duplicate products in the new Products list that are already on the website
            //Product.RemoveUpdates();

            //Send selenium to the posts page on wordpress and await orders to manually add links cuz wordpresspcl is dumb and cant do it by itself
            Selenium.GoToPosts(Selenium.driver);

            //Adds picture of the the new products to the media library
            Wordpress.AddPics(Selenium.driver).Wait();
            foreach (Product product in Product.products)
            {
                //Posts the new products to the site
                Wordpress.CreatePost(Selenium.driver, product).Wait();
            }

            //Another check to make sure none of the posts are duplicates
            Wordpress.RemoveDuplicates();

            //Removes images that are not in use on the hard drive
            Wordpress.CleanImagesFolder().Wait();
            Wordpress.CleanMedia().Wait();

            SQL.WriteProducts();

            //excel.WriteHHPosts();

            Selenium.driver.Close();
            Selenium.driver.Quit();

            SQL.con.Close();
        }
Esempio n. 5
0
        /// <summary>
        /// removes duplicate elements from products list
        /// </summary>
        /// <param name="driver"></param>
        /// <param name="wait"></param>
        public static void removeDuplicates(IWebDriver driver, WebDriverWait wait)
        {
            foreach (Product product in Product.products)
            {
                product.change = false;
            }

            foreach (Product product1 in Product.products)
            {
                String name1;

                driver.Navigate().GoToUrl(product1.URL);

                try
                {
                    wait.Until(ExpectedConditions.PresenceOfAllElementsLocatedBy(By.XPath("//span [@id = 'productTitle']")));
                    name1 = driver.FindElement(By.XPath("//span [@id = 'productTitle']")).Text;
                }
                catch (Exception e)
                {
                    if (e is NoSuchElementException)
                    {
                        name1           = "";
                        product1.change = true;
                    }
                    else if (e is WebDriverTimeoutException)
                    {
                        name1           = "";
                        product1.change = true;
                    }
                    else
                    {
                        throw;
                    }
                }

                foreach (Product product2 in Product.products)
                {
                    String name2;

                    driver.Navigate().GoToUrl(product2.URL);

                    try
                    {
                        name2 = driver.FindElement(By.XPath("//span [@id = 'productTitle']")).Text;
                    }
                    catch (Exception e)
                    {
                        if (e is NoSuchElementException)
                        {
                            name2           = "";
                            product2.change = true;
                        }
                        else if (e is WebDriverTimeoutException)
                        {
                            name2           = "";
                            product2.change = true;
                        }
                        else
                        {
                            throw;
                        }
                    }

                    if (name1 == name2)
                    {
                        product2.change = true;
                    }
                }
            }

restart:
            foreach (Product product in Product.products)
            {
                if (product.change)
                {
                    Product.products.Remove(product);
                    Wordpress.RemoveProduct(product).Wait();
                    goto restart;
                }
            }
        }