/* ------------------------- CONTROL FUNCTIONS ------------------------- */ /* Functions are primarily modified control functions for a cross threaded application */ /* * General purpose function for setting the text of a control in a threadsafe manner * @param text: The desired text for the control * @param control: The control being modified */ private void setControlText(String text, Control control) { if (control.InvokeRequired) { SetLabelTextDelegate d = new SetLabelTextDelegate(setControlText); this.Invoke(d, new object[] { text, control }); } else { control.Text = text; } }
//MAIN CODE TO LOAD HANDBAGS private void WorkerMine_DoWork() { //Set Progress Text to "0/0" SetLabelTextDelegate setLabelTextDelegate = new SetLabelTextDelegate(SetLabelText); String progressTextString = "0/0"; this.Invoke(setLabelTextDelegate, new object[] { "progressText", progressTextString }); productsLoaded = false; //Create a box with 4 default spots + more depending on how many bags are present WebBrowser webBrowser = new WebBrowser(); int defaultWidth = 550; int defaultHeight = 550; Panel leftPanel = new Panel { Name = "leftPanel", Location = new Point(30, 30), Size = new Size(defaultWidth, defaultHeight), AutoScroll = false, BackColor = Color.Gray, }; leftPanel.VerticalScroll.Visible = false; //Click Event this.Click += clickEvent; leftPanel.Click += clickEvent; AddPanelDelegate addPanel = new AddPanelDelegate(AddPanel); this.Invoke(addPanel, new object[] { leftPanel }); //Variables for item boxes int xCoord = 0; int yCoord = 0; //Check if the user wants to get stock data (slow as it uses a web browser) bool getStock = false; if (stockCheckBox.Checked) { getStock = true; } //Load an instance of Internet Explorer 11 for Web Browsing if (getStock) { webBrowser.ScriptErrorsSuppressed = true; if (!WBEmulator.IsBrowserEmulationSet()) { WBEmulator.SetBrowserEmulationVersion(); } //Web Browser Event webBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(JavaScriptLoaded); } //Using the HTML Agility NuGet HtmlAgilityPack.HtmlWeb htmlWeb = new HtmlWeb(); HtmlAgilityPack.HtmlDocument webPage = new HtmlAgilityPack.HtmlDocument(); HtmlAgilityPack.HtmlDocument webPageDesc = new HtmlAgilityPack.HtmlDocument(); webPage = htmlWeb.Load("https://uk.louisvuitton.com/eng-gb/women/handbags/_/N-2keomb.html"); //Collect HTML Data HtmlAgilityPack.HtmlNodeCollection productNames = webPage.DocumentNode.SelectNodes("//div[@class='productName toMinimize']"); HtmlAgilityPack.HtmlNodeCollection productPrices = webPage.DocumentNode.SelectNodes("//div[@class='from-price']//span"); HtmlAgilityPack.HtmlNodeCollection productPageLinks = webPage.DocumentNode.SelectNodes("//a[@class='product-item productItem tagClick tagClick']"); //HtmlAgilityPack.HtmlNodeCollection productPageLinks = webPage.DocumentNode.SelectNodes("//a[@class='product-item tagClick tagClick']"); HtmlAgilityPack.HtmlNodeCollection productImageLinks = webPage.DocumentNode.SelectNodes("//a[@class='product-item productItem tagClick tagClick']//div[@class='imageWrapper']//img"); //Progress Bar InitialseProgressBar(productNames.Count); //Collect JavaScript Data + Add Item to Form handbags.Clear(); for (int i = 0; i < productNames.Count; i++) { //Create Handbag Object handbags.Add(new Handbag()); //Product Name handbags.ElementAt(i).ProductName = productNames.ElementAt(i).InnerText; //Product Price handbags.ElementAt(i).ProductPrice = productPrices.ElementAt(i).Attributes["data-htmlContent"].Value; //Product Code handbags.ElementAt(i).ProductCode = productPageLinks.ElementAt(i).Attributes["data-sku"].Value; //Image Link handbags.ElementAt(i).ProductImage = productImageLinks.ElementAt(i).Attributes["data-src"].Value; handbags.ElementAt(i).ProductImage = handbags.ElementAt(i).ProductImage.Replace("{IMG_WIDTH}", "360"); handbags.ElementAt(i).ProductImage = handbags.ElementAt(i).ProductImage.Replace("{IMG_HEIGHT}", "360"); handbags.ElementAt(i).ProductImage = handbags.ElementAt(i).ProductImage.Replace(" ", "%20"); //Page Link handbags.ElementAt(i).PageLink = "https://uk.louisvuitton.com/" + productPageLinks.ElementAt(i).Attributes["href"].Value; //Navigate Browser for JavaScript (The only way to get stock messages is using JavaScript, so the program opens an internet browser) if (getStock) { LoadWebPageWithBrowser(webPageDesc, handbags.ElementAt(i).PageLink, webBrowser); } else { webPageDesc = htmlWeb.Load(handbags.ElementAt(i).PageLink); } HtmlAgilityPack.HtmlNode description = webPageDesc.DocumentNode.SelectSingleNode("//div[@class='productDescription description-push-text onlyML ppTextDescription ']"); if (description != null) { handbags.ElementAt(i).ProductDescription = description.InnerText; handbags.ElementAt(i).ProductDescription = handbags.ElementAt(i).ProductDescription.Remove(0, 15); } //Handbag Availability if (getStock) { HtmlAgilityPack.HtmlNode availability = webPageDesc.DocumentNode.SelectSingleNode("//div[@id='notInStock']"); if (availability != null) { if (availability.Attributes["class"].Value == "getIndexClass") { handbags.ElementAt(i).Availability = "Currently out of stock online"; } else { handbags.ElementAt(i).Availability = "In Stock"; } } } //Add Item to Form int rowNumber = Convert.ToInt32(Math.Floor(Convert.ToDouble(i) / 2)); //Get X & Y Coordinates yCoord = yMargin + rowNumber * boxSize + yMargin * rowNumber; if (i % 2 == 0) { xCoord = xMargin; } else { xCoord = columnDistance; } //Add Box To Panel UpdatePanelDelegate updatePanelDelegate = new UpdatePanelDelegate(UpdatePanel); this.Invoke(updatePanelDelegate, new object[] { "leftPanel", handbags.ElementAt(i), xCoord, yCoord, boxSize, i }); //Progress Text progressTextString = (i + 1) + "/" + productNames.Count; VoidDelegate progressStep = new VoidDelegate(ProgressStep); this.Invoke(setLabelTextDelegate, new object[] { "progressText", progressTextString }); this.Invoke(progressStep); } //DATA COLLECTED! VoidDelegate endProgressBar = new VoidDelegate(EndProgressBar); this.Invoke(endProgressBar); webBrowser.Dispose(); webSiteLoaded = false; midwork = false; productsLoaded = true; }