// See https://aka.ms/new-console-template for more information using OpenQA.Selenium.Chrome; using SeleniumUndetectedChromeDriver; using OpenQA.Selenium; using System.Net; using System.Diagnostics; using ConsoleApp3.Contracts; using static System.Collections.Specialized.BitVector32; namespace CardmarketBot { class CardMarketParser { private readonly string username; private readonly string password; Dictionary portoberechnung = new Dictionary() { {"1,15 €", Helper.Porto.BRIEF085 }, {"1,30 €", Helper.Porto.BRIEF100 }, {"2,10 €", Helper.Porto.BRIEF160 }, {"3,25 €", Helper.Porto.BRIEF275 }, {"2,60 €", Helper.Porto.PRIO210 }, {"3,20 €", Helper.Porto.PRIO270 }, }; public CardMarketParser(string username, string password) { this.username = username; this.password = password; } public List ParseCardMarket(string forceid = "") { //var driver = UndetectedChromeDriver.Create(driverExecutablePath: await new ChromeDriverInstaller().Auto()); ChromeOptions options = new ChromeOptions(); options.AddArgument("start-maximized"); //options.AddArgument("incognito"); //options.AddArgument("disable-popup-blocking"); options.AddArgument("force-device-scale-factor=0.8"); IWebDriver cd = new ChromeDriver(options); //driver.GoToUrl("https://www.cardmarket.com/de/OnePiece"); cd.Url = @"https://www.cardmarket.com/de/OnePiece"; cd.Navigate(); Thread.Sleep(2000); IWebElement? element; /* bool cloudfire = true; if (cloudfire) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Scheint als würde Cloudfire aktiviert sein."); Console.WriteLine("Bitte bestätige und komme zurück und klicke die buchstabe c und Enter"); Console.ReadLine(); } */ cd.FindElement(By.CssSelector("#CookiesConsent > div > div > form > div > button")).Click(); Thread.Sleep(2000); cd.FindElement(By.Name("username")).Click(); cd.FindElement(By.Name("username")).SendKeys(username); cd.FindElement(By.Name("userPassword")).Click(); cd.FindElement(By.Name("userPassword")).SendKeys(password); cd.FindElement(By.CssSelector("#header-login > input.btn.btn-outline-primary.btn-sm")).Click(); Thread.Sleep(6000); CookieContainer cookieContainer = new CookieContainer(); foreach (var c in cd.Manage().Cookies.AllCookies) { string name = c.Name; string value = c.Value; cookieContainer.Add(new System.Net.Cookie(name, value, c.Path, c.Domain)); } List ids = new List(); List kunden = new List(); //IWebElement? element; /*cloudfire = true; try { element = cd.FindElement(By.XPath("/html/body/div[1]/div/div[1]/div")); // //[@id="challenge-body-text"] } catch (NoSuchElementException) { cloudfire = false; } Console.WriteLine(cloudfire); if(cloudfire ) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Scheint als würde Cloudfire aktiviert sein."); Console.WriteLine("Bitte bestätige und komme zurück und klicke die buchstabe c und Enter"); Console.ReadLine(); } */ if (forceid != "") { string[] mid = forceid.Split(','); if (mid.Length < 0) { ids.Add(forceid); } else { foreach(string kid in mid) { ids.Add(kid); } } } else { cd.Navigate().GoToUrl(@"https://www.cardmarket.com/de/OnePiece/Orders/Sales/Paid"); try { var verkaufstable = cd.FindElements(By.XPath("/html/body/main/section/div[3]/div[4]/div[2]/*")); foreach(var karte in verkaufstable) { var content = karte.Text.Split("\r\n"); // 10 Stellen ist die ID ids.Add(content[1]); } } catch (OpenQA.Selenium.NotFoundException) { Console.WriteLine("Keine Verkäufe aktuell"); return kunden; } } foreach (string id in ids) { cd.Navigate().GoToUrl(string.Format(@"https://www.cardmarket.com/de/OnePiece/Orders/{0}", id)); element = cd.FindElement(By.XPath("/html/body/main/section/div/div[1]/div/div[3]/div[2]/div[2]/div/div")); Kunde kunde = Helper.ConvertToKunde(element.Text); // Bezahldatum element = cd.FindElement(By.XPath("/html/body/main/section/div/div[1]/div/div[2]/div/div[2]/div[2]")); kunde.Bezahldatum = Helper.ConvertBezahlDatum(element.Text); // Versandkosten element = cd.FindElement(By.XPath("/html/body/main/section/div/div[1]/div/div[3]/div[1]/div[2]/div/div[3]/span[2]")); Helper.Porto resul; if(portoberechnung.TryGetValue(element.Text, out resul)) { kunde.Versandskosten = resul; } else { kunde.OverrideVersandskosten = element.Text; Console.WriteLine($"Achtung bei ID {id} wurde kein Richtige Porto berechnet. Liegt es im Ausland?"); } //kunde.Versandskosten = portoberechnung[element.Text]; kunde.BestellungID = id; // Artikeln element = cd.FindElement(By.XPath("/html/body/main/section/div/div[1]/div/div[5]/table/tbody")); string artikeln = element.Text; kunde.Artikels = Helper.ParseArtikeln(element.Text); kunden.Add(kunde); Thread.Sleep(TimeSpan.FromSeconds(3)); } cd.Quit(); return kunden; } } }