/// <summary> /// Remove duplicate tags added from unknown bug. My guess is that running 3 threads at once causes some products to be added twice somehow /// </summary> public static void RemoveDuplicates() { //Remove duplicate tags from unknown bug List <String> duplicates = Wordpress.tags.GroupBy(x => x).Where(g => g.Count() > 1).Select(y => y.Key).ToList(); foreach (String str in duplicates) { //remove tag Wordpress.tags.Remove(str); //remove product for (int i = 0; i < Product.products.Count; i++) { if (Product.products[i].ID == str) { Product.products.RemoveAt(i); break; } } } //remove the picture on hard drive Wordpress.CleanImagesFolder(duplicates).Wait(); if (Wordpress.tags.Count != Wordpress.tags.Distinct().Count()) { MessageBox.Show("Error: There are still duplicates"); } }
/// <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(element => element.FindElement(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; // } // } //} /// <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(element => element.FindElement(By.XPath("//td [@id = 'cb']"))); } } MessageBox.Show("Link not found"); return("Not Found"); }
private void AddBtn_Click(object sender, EventArgs e) { TurnOffButtons(); Product.products.Clear(); //Grabs New products from excel, adds to SQL, and populates products list 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(); TurnOnButtons(); }
private void UpdatePostsBtn_Click(object sender, EventArgs e) { TurnOffButtons(); //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 List <List <Product> > DividedUpdates = Product.SplitList(Product.updates, 3); Thread thread1 = new Thread(() => Selenium.GetPostData(Selenium.drivers[0], Selenium.waits[0], DividedUpdates[0])); Thread thread2 = new Thread(() => Selenium.GetPostData(Selenium.drivers[1], Selenium.waits[1], DividedUpdates[1])); Thread thread3 = new Thread(() => Selenium.GetPostData(Selenium.drivers[2], Selenium.waits[2], DividedUpdates[2])); thread1.Start(); thread2.Start(); thread3.Start(); thread1.Join(); thread2.Join(); thread3.Join(); Product.updates = DividedUpdates[0].Concat(DividedUpdates[1]).Concat(DividedUpdates[2]).ToList(); //Selenium.GetOldPostData(); //Updates the posts on the website with the updates sale information for each product Wordpress.UpdatePosts().Wait(); TurnOnButtons(); }
private void RemoveDuplicatesButton_Click(object sender, EventArgs e) { TurnOffButtons(); Wordpress.RemoveDuplicates(); TurnOnButtons(); }
private void CleanLibraryBtn_Click(object sender, EventArgs e) { TurnOffButtons(); Wordpress.CleanMedia().Wait(); TurnOnButtons(); }
private void CleanPicsBtn_Click(object sender, EventArgs e) { TurnOffButtons(); Wordpress.CleanImagesFolder().Wait(); TurnOnButtons(); }
/// <summary> /// Takes a list of posts and returns a list of products /// </summary> /// <param name="posts"></param> /// <returns></returns> public static List <Product> PostToProduct(List <Post> posts) { List <Product> siteProduct = new List <Product>(); foreach (Post post in posts) { siteProduct.Add(new Product( post.Title.Rendered, post.Categories[0], post.Link, post.Tags[0] )); } siteProduct = Wordpress.FixTags(siteProduct).Result; return(siteProduct); }
/// <summary> /// Creates record of all products from website /// </summary> public static void WriteProducts() { //TODO grab all the tags at once instead of getting each one every time. too long SqlCommand cmd = new SqlCommand("TRUNCATE TABLE Products", con); cmd.ExecuteNonQuery(); List <Post> WP = Wordpress.SendGetPosts(Wordpress.GetPosts()); Dictionary <int, Tag> tags = Wordpress.SendGetTags(Wordpress.GetTags()).ToDictionary(p => p.Id); foreach (Post post in WP) { //String id = Wordpress.SendGetTag(Wordpress.GetTag(post)); String id = tags[post.Tags[0]].Name; 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(); } }
private void FullUpdateBtn_Click(object sender, EventArgs e) { TurnOffButtons(); //Reads website and populates site list with existing posts //List<Product> site = Wordpress.ReadWebsite().Result; //Cleanup becuase my laptop has limited storage Wordpress.CleanImagesFolder().Wait(); Wordpress.CleanMedia().Wait(); Wordpress.RemoveEmptyTags(); //Wordpress.RemoveNoURLPostsAsync().Wait(); //Begin the 3 browser threads lists String[] list1 = new String[] { "Laptops", "Desktops", "PC Gaming", "Monitors", "Computer Accessories" }; String[] list2 = new String[] { "Networking", "Computer Components", "Storage", "TV & Video", "Cell Phones & Accessories" }; String[] list3 = new String[] { "Speakers", "Headphones", "Bluetooth Earbuds", "Phones" }; //Initialize browser threads //Grabs new info and stores it as Product in Product.products list //Products WRITE Thread thread1 = new Thread(() => Selenium.GetInfoByCategories(Selenium.drivers[0], Selenium.waits[0], list1)); Thread thread2 = new Thread(() => Selenium.GetInfoByCategories(Selenium.drivers[1], Selenium.waits[1], list2)); Thread thread3 = new Thread(() => Selenium.GetInfoByCategories(Selenium.drivers[2], Selenium.waits[2], list3)); thread1.Start(); thread2.Start(); thread3.Start(); thread1.Join(); thread2.Join(); thread3.Join(); //Remove duplicates from unknown bug Formatting.RemoveDuplicates(); //Correct the categories of Products list so they fit the website Formatting.CorrectCategories(); //Goes to the amazon page of each existing product (in used list) and updates the sale information List <List <Product> > DividedUpdates = (List <List <Product> >)Product.SplitList(Product.posts.Values.ToList(), (Product.posts.Count / 3) + 1); if (DividedUpdates.Count > 0) { thread1 = new Thread(() => Selenium.GetPostData(Selenium.drivers[0], Selenium.waits[0], DividedUpdates[0])); thread2 = new Thread(() => Selenium.GetPostData(Selenium.drivers[1], Selenium.waits[1], DividedUpdates[1])); thread3 = new Thread(() => Selenium.GetPostData(Selenium.drivers[2], Selenium.waits[2], DividedUpdates[2])); thread1.Start(); thread2.Start(); thread3.Start(); thread1.Join(); thread2.Join(); thread3.Join(); Product.updates = DividedUpdates[0].Concat(DividedUpdates[1]).Concat(DividedUpdates[2]).ToList(); //Updates the posts on the website with the updated sale information for each product Wordpress.UpdatePosts().Wait(); } Selenium.drivers[0].Close(); Selenium.drivers[1].Close(); Selenium.drivers[2].Close(); //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 pictures 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().Wait(); //Removes images that are not in use on the hard drive Wordpress.CleanImagesFolder().Wait(); Wordpress.CleanMedia().Wait(); Wordpress.RemoveEmptyTags(); SQL.WriteProducts(); Selenium.driver.Close(); Selenium.driver.Quit(); TurnOnButtons(); }