Code cleanup
This commit is contained in:
@@ -1,496 +1,37 @@
|
||||
// See https://aka.ms/new-console-template for more information
|
||||
|
||||
using OpenQA.Selenium.Chrome;
|
||||
using OpenQA.Selenium;
|
||||
using System.Diagnostics;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using static System.Collections.Specialized.BitVector32;
|
||||
using ConsoleApp3.DataContracts;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
/*
|
||||
*
|
||||
* referalPage: /de/OnePiece
|
||||
username: Skywalkerex
|
||||
userPassword: Magnatpower310!!
|
||||
/de/OnePiece/PostGetAction/User_Login
|
||||
|
||||
curbpJUJmtup1t.Tq0awbHIhIRwhzMW7vrsWxLAJu.pI9X4r
|
||||
*/
|
||||
|
||||
namespace CardmarketBot
|
||||
{
|
||||
|
||||
static class LexofficeApiAddressesBuilder
|
||||
{
|
||||
private static string BaseAddress = @"https://api.lexoffice.io";
|
||||
private static string Invoice = "v1/invoices";
|
||||
|
||||
public static string InvoiceUri(string id)
|
||||
{
|
||||
return $"{BaseAddress}/{Invoice}/{id}";
|
||||
}
|
||||
|
||||
public static string CreateInvoice()
|
||||
{
|
||||
return $"{BaseAddress}/{Invoice}";
|
||||
}
|
||||
}
|
||||
class InvoiceService
|
||||
{
|
||||
private readonly WebRequest request;
|
||||
|
||||
public InvoiceService()
|
||||
{
|
||||
request = WebRequest.Create("https://api.lexoffice.io/v1/invoices");
|
||||
request.Method = "POST";
|
||||
request.Headers.Add("Authorization", "Bearer curbpJUJmtup1t.Tq0awbHIhIRwhzMW7vrsWxLAJu.pI9X4r");
|
||||
request.Headers.Add("Accept", "application/json");
|
||||
}
|
||||
|
||||
public void InsertInvoice(Invoice invoice)
|
||||
{
|
||||
var json = JsonConvert.SerializeObject(invoice);
|
||||
byte[] bytearray = Encoding.UTF8.GetBytes(json);
|
||||
request.ContentType = "application/json";
|
||||
using var reqStream = request.GetRequestStream();
|
||||
reqStream.Write(bytearray, 0, bytearray.Length);
|
||||
using var response = request.GetResponse();
|
||||
Console.WriteLine(((HttpWebResponse)response).StatusDescription);
|
||||
reqStream.Close();
|
||||
}
|
||||
}
|
||||
|
||||
class LexofficeService
|
||||
{
|
||||
private readonly HttpClient _client;
|
||||
private readonly Random _random = new();
|
||||
|
||||
public LexofficeService()
|
||||
{
|
||||
_client = new HttpClient
|
||||
{
|
||||
DefaultRequestHeaders =
|
||||
{
|
||||
{ "Authorization", "Bearer curbpJUJmtup1t.Tq0awbHIhIRwhzMW7vrsWxLAJu.pI9X4r" },
|
||||
{ "Accept", "application/json" }
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public void test()
|
||||
{
|
||||
var d = GetInvoiceAsync("d47936c1-71c6-4e22-b394-3630d7354ebf");
|
||||
var m = d.Result;
|
||||
Console.WriteLine(m);
|
||||
}
|
||||
|
||||
public void m(Invoice invoice)
|
||||
{
|
||||
|
||||
|
||||
}
|
||||
|
||||
public async void WriteInvoice(Invoice invoice)
|
||||
{
|
||||
|
||||
|
||||
|
||||
var jsonSerializerSettings = new JsonSerializerSettings
|
||||
{
|
||||
MissingMemberHandling = MissingMemberHandling.Ignore
|
||||
};
|
||||
|
||||
var s = JsonConvert.SerializeObject(invoice, jsonSerializerSettings);
|
||||
|
||||
var content = new StringContent(invoice.ToString(), Encoding.UTF8, "application/json");
|
||||
|
||||
var uri = LexofficeApiAddressesBuilder.CreateInvoice();
|
||||
var result = await _client.PostAsync(uri, content);
|
||||
Console.WriteLine(result.Content);
|
||||
}
|
||||
|
||||
private async Task<Invoice> GetInvoiceAsync(string id)
|
||||
{
|
||||
var uri = LexofficeApiAddressesBuilder.InvoiceUri(id);
|
||||
var response = await _client.GetAsync(uri).ConfigureAwait(false);
|
||||
|
||||
response.EnsureSuccessStatusCode();
|
||||
|
||||
var contents = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
|
||||
|
||||
var jsonSerializerSettings = new JsonSerializerSettings
|
||||
{
|
||||
MissingMemberHandling = MissingMemberHandling.Ignore
|
||||
};
|
||||
return JsonConvert.DeserializeObject<Invoice>(contents, jsonSerializerSettings);
|
||||
}
|
||||
}
|
||||
internal class Programm
|
||||
{
|
||||
static string DateTimeConverter(string input)
|
||||
{
|
||||
//2023-06-20T11:30:00"
|
||||
//2023-02-22T00:00:00.000+01:0
|
||||
string datum = input.Substring(0, 10);
|
||||
string uhrzeit = input.Substring(10, input.Length - 10);
|
||||
input = string.Format("{0} {1}", datum, uhrzeit);
|
||||
DateTime converted = Convert.ToDateTime(input);
|
||||
var format = "yyyy-MM-ddTHH:mm:ssK";
|
||||
string res = converted.ToString(format);
|
||||
return string.Format("{0}.000+01:00", res);
|
||||
|
||||
}
|
||||
|
||||
static void Main(string[] args)
|
||||
{
|
||||
// Result 2023-02-22T00:00:00.000+01:00
|
||||
/*string input = "20.06.202311:30";
|
||||
|
||||
|
||||
var localTime = new DateTime(2022, 1, 13, 16, 25, 35, 125, DateTimeKind.Local);
|
||||
|
||||
Console.WriteLine(converted.ToString(format));
|
||||
|
||||
Debugger.Break();
|
||||
return;
|
||||
*/
|
||||
|
||||
Dictionary<string, Helper.Porto> portoberechnung = new Dictionary<string, Helper.Porto>()
|
||||
{
|
||||
{"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 },
|
||||
};
|
||||
|
||||
Dictionary<Helper.Porto, decimal> PortoPreis = new Dictionary<Helper.Porto, decimal>()
|
||||
{
|
||||
{Helper.Porto.BRIEF085, 1.15m },
|
||||
{Helper.Porto.BRIEF100, 1.30m },
|
||||
{Helper.Porto.BRIEF160, 2.10m },
|
||||
{Helper.Porto.BRIEF275, 3.25m },
|
||||
{Helper.Porto.PRIO210, 2.60m },
|
||||
{Helper.Porto.PRIO270, 3.20m }
|
||||
};
|
||||
ChromeOptions options = new ChromeOptions();
|
||||
options.AddArgument("start-maximized");
|
||||
IWebDriver cd = new ChromeDriver(options);
|
||||
cd.Url = @"https://www.cardmarket.com/de/OnePiece";
|
||||
|
||||
cd.Navigate();
|
||||
cd.FindElement(By.CssSelector("#CookiesConsent > div > div > form > button")).Click();
|
||||
cd.FindElement(By.Name("username")).Click();
|
||||
cd.FindElement(By.Name("username")).SendKeys("Skywalkerex");
|
||||
cd.FindElement(By.Name("userPassword")).Click();
|
||||
|
||||
|
||||
cd.FindElement(By.Name("userPassword")).SendKeys("Magnatpower310!!");
|
||||
cd.FindElement(By.CssSelector("#header-login > input.btn.AB-login-btn.btn-outline-primary.btn-sm")).Click();
|
||||
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
//cd.Navigate().GoToUrl(@"https://www.cardmarket.com/de/OnePiece/Orders/Sales/Paid");
|
||||
cd.Navigate().GoToUrl(@"https://www.cardmarket.com/de/OnePiece/Orders/Sales/Sent");
|
||||
|
||||
IWebElement element = cd.FindElement(By.XPath("/html/body/main/section/div[3]/div[3]/div[2]"));
|
||||
string content = element.Text;
|
||||
|
||||
string[] datas = content.Split("\r\n");
|
||||
|
||||
List<string> ids = new List<string>();
|
||||
for (int i = 0; i < datas.Length; i += 7)
|
||||
{
|
||||
ids.Add(datas[i]);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// Kunden aus Cardmarket erstellen
|
||||
List<Kunde> kunden = new List<Kunde>();
|
||||
CardMarketParser cardMarketParser = new CardMarketParser();
|
||||
kunden = cardMarketParser.ParseCardMarket();
|
||||
|
||||
int maxCounter = 2;
|
||||
int counter = 0;
|
||||
foreach (string id in ids)
|
||||
{
|
||||
if (counter > maxCounter) break;
|
||||
Thread.Sleep(TimeSpan.FromSeconds(5));
|
||||
Console.WriteLine(id);
|
||||
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 = element.Text;
|
||||
//Debugger.Break();
|
||||
|
||||
// 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]"));
|
||||
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);
|
||||
counter++;
|
||||
|
||||
}
|
||||
|
||||
// Rechnungen generieren
|
||||
List<Invoice> rechnungen = new List<Invoice>();
|
||||
InvoiceParser invoiceParser = new InvoiceParser(kunden);
|
||||
rechnungen = invoiceParser.GetInvoices();
|
||||
|
||||
foreach(Kunde kunde in kunden)
|
||||
{
|
||||
Invoice invoice = new Invoice();
|
||||
invoice.Language = "de";
|
||||
invoice.VoucherDate = DateTimeConverter(kunde.Bezahldatum); // "2023-02-22T00:00:00.000+01:00"; //
|
||||
invoice.Address = new InvoiceAddress()
|
||||
{
|
||||
Name = kunde.Name,
|
||||
Street = kunde.Strasse + " " + kunde.Hausnummer,
|
||||
City = kunde.Ort,
|
||||
Zip = kunde.Plz,
|
||||
CountryCode = "DE"
|
||||
};
|
||||
invoice.LineItems = new List<InvoiceLineItem>();
|
||||
foreach(var artikel in kunde.Artikels)
|
||||
{
|
||||
invoice.LineItems.Add(new InvoiceLineItem()
|
||||
{
|
||||
Type = "custom",
|
||||
Name = artikel.ENGName + "(" + artikel.Source + ")",
|
||||
Quantity = artikel.Amount,
|
||||
UnitName = "Stück",
|
||||
UnitPrice = new InvoiceLineUnitPrice()
|
||||
{
|
||||
Currency = "EUR",
|
||||
GrossAmount = Convert.ToDecimal(artikel.Preis),
|
||||
TaxRatePercentage = 19
|
||||
}
|
||||
});
|
||||
}
|
||||
invoice.LineItems.Add(new InvoiceLineItem()
|
||||
{
|
||||
Type = "custom",
|
||||
Name = "Versandkosten",
|
||||
Quantity = 1,
|
||||
UnitName = "Stück",
|
||||
UnitPrice = new InvoiceLineUnitPrice()
|
||||
{
|
||||
Currency = "EUR",
|
||||
GrossAmount = PortoPreis[kunde.Versandskosten],
|
||||
TaxRatePercentage = 19
|
||||
}
|
||||
});
|
||||
|
||||
invoice.TotalPrice = new TotalPrice() { Currency = "EUR" };
|
||||
invoice.TaxConditions = new TaxConditions() { TaxType = "gross" };
|
||||
invoice.ShippingConditions = new ShippingConditions()
|
||||
{
|
||||
shippingDate = "2023-04-22T00:00:00.000+02:00",
|
||||
shippingType = "delivery"
|
||||
};
|
||||
rechnungen.Add(invoice);
|
||||
}
|
||||
|
||||
|
||||
foreach(var item in rechnungen)
|
||||
foreach (var item in rechnungen)
|
||||
{
|
||||
InvoiceService invoiceService = new InvoiceService();
|
||||
invoiceService.InsertInvoice(item);
|
||||
}
|
||||
Debugger.Break();
|
||||
|
||||
|
||||
// Post CSV Erstellen
|
||||
IEnumerable<IGrouping<Helper.Porto, Kunde>> groupedPorto = kunden.GroupBy(x => x.Versandskosten).Distinct();
|
||||
|
||||
foreach (IGrouping<Helper.Porto, Kunde> gkunde in groupedPorto)
|
||||
{
|
||||
//FileStream stream = File.Create(string.Format("test_{0}.csv", gkunde.Key.ToString()));
|
||||
Helper.Porto porto = gkunde.Key;
|
||||
using (StreamWriter writer = new StreamWriter(string.Format("./test_{0}.csv", gkunde.Key.ToString()), false, Encoding.Latin1))
|
||||
{
|
||||
|
||||
string header = "NAME;ZUSATZ;STRASSE;NUMMER;PLZ;STADT;LAND;ADRESS_TYP;REFERENZ";
|
||||
writer.WriteLine(header);
|
||||
string absender = "W&W TECH UG;Lukas Winkelmann;Schwedenschanze;70;49808;LINGEN;DEU;HOUSE";
|
||||
writer.WriteLine(absender);
|
||||
foreach (Kunde n in gkunde)
|
||||
{
|
||||
string empfanger = string.Format("{0};{1};{2};{3};{4};{5};{6};{7};{8}",
|
||||
n.Name,
|
||||
"",
|
||||
n.Strasse,
|
||||
n.Hausnummer,
|
||||
n.Plz,
|
||||
n.Ort,
|
||||
"DEU",
|
||||
//n.Land,
|
||||
"HOUSE",
|
||||
n.BestellungID
|
||||
);
|
||||
writer.WriteLine(empfanger);
|
||||
}
|
||||
writer.Close();
|
||||
}
|
||||
}
|
||||
DeutschePost deutschePost = new DeutschePost(kunden);
|
||||
deutschePost.GenerateCSV();
|
||||
|
||||
|
||||
Console.WriteLine("Fertig");
|
||||
Console.ReadLine();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static class Helper
|
||||
{
|
||||
public enum Porto
|
||||
{
|
||||
BRIEF085,
|
||||
BRIEF100,
|
||||
BRIEF160,
|
||||
BRIEF275,
|
||||
PRIO210,
|
||||
PRIO270
|
||||
}
|
||||
internal static Kunde ConvertToKunde(string text)
|
||||
{
|
||||
|
||||
string[] datas = text.Split("\r\n");
|
||||
string name = datas[0];
|
||||
string adresseUNDHausnummer = datas[1];
|
||||
string plzUNDOrt = datas[2];
|
||||
string land = datas[3];
|
||||
|
||||
datas = plzUNDOrt.Split(" ");
|
||||
string plz = datas[0];
|
||||
string ort = datas[1];
|
||||
|
||||
datas = adresseUNDHausnummer.Split(" ");
|
||||
string strasse = "";
|
||||
for (int i = 0; i < datas.Length - 1; i++)
|
||||
{
|
||||
strasse += datas[i] + " ";
|
||||
}
|
||||
strasse = strasse.Trim();
|
||||
string hausnummer = datas[datas.Length - 1];
|
||||
|
||||
return new Kunde(name, strasse, hausnummer, plz, ort, land);
|
||||
|
||||
}
|
||||
|
||||
internal static List<Artikel> ParseArtikeln(string text)
|
||||
{
|
||||
List<Artikel> result = new List<Artikel>();
|
||||
string[] datas = text.Split("\r\n");
|
||||
|
||||
|
||||
int anzahl = datas.Length / 6;
|
||||
|
||||
for (int i = 0; i < datas.Length; i += 6)
|
||||
{
|
||||
Artikel temp = new Artikel();
|
||||
var xFound = datas[i].IndexOf("x");
|
||||
var am = datas[i].Substring(0, xFound);
|
||||
temp.Amount = Convert.ToInt32(am);
|
||||
temp.GERName = datas[i].Substring(xFound + 1);
|
||||
temp.ENGName = datas[i + 1];
|
||||
temp.CardNumber = datas[i + 2];
|
||||
temp.Source = datas[i + 3];
|
||||
temp.Raritaet = datas[i + 4];
|
||||
temp.Preis = datas[i + 5].Split(' ')[0];
|
||||
result.Add(temp);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
internal class Artikel
|
||||
{
|
||||
public int Amount { get; set; }
|
||||
public string GERName { get; set; } = "";
|
||||
public string ENGName { get; set; } = "";
|
||||
public string CardNumber { get; set; } = "";
|
||||
public string Source { get; set; } = "";
|
||||
public string Raritaet { get; set; } = "";
|
||||
public string Preis { get; set; } = "";
|
||||
}
|
||||
|
||||
class Kunde
|
||||
{
|
||||
string bestellungID = "";
|
||||
Helper.Porto versandskosten;
|
||||
string name;
|
||||
string strasse;
|
||||
string hausnummer;
|
||||
string plz;
|
||||
string ort;
|
||||
string land;
|
||||
List<Artikel> artikels = new List<Artikel>();
|
||||
public string Bezahldatum { get; set; } = "";
|
||||
|
||||
public string Name { get => name; set => name = value; }
|
||||
public string Strasse { get => strasse; set => strasse = value; }
|
||||
public string Plz { get => plz; set => plz = value; }
|
||||
public string Ort { get => ort; set => ort = value; }
|
||||
public string Land { get => land; set => land = value; }
|
||||
public string BestellungID { get => bestellungID; set => bestellungID = value; }
|
||||
public Helper.Porto Versandskosten { get => versandskosten; set => versandskosten = value; }
|
||||
public string Hausnummer { get => hausnummer; set => hausnummer = value; }
|
||||
internal List<Artikel> Artikels { get => artikels; set => artikels = value; }
|
||||
|
||||
public Kunde(string Name, string Strasse, string Hausnummer, string PLZ, string Ort, string Land)
|
||||
{
|
||||
name = Name;
|
||||
hausnummer = Hausnummer;
|
||||
strasse = Strasse;
|
||||
plz = PLZ;
|
||||
ort = Ort;
|
||||
land = Land;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
HttpWebRequest hwr = (HttpWebRequest)HttpWebRequest.Create(@"https://www.cardmarket.com/de/OnePiece/Orders/Sales/Paid");
|
||||
hwr.CookieContainer = cookieContainer;
|
||||
hwr.Method = "GET";
|
||||
hwr.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36";
|
||||
WebResponse wr = hwr.GetResponse();
|
||||
string s = new StreamReader(wr.GetResponseStream()).ReadToEnd();
|
||||
Console.WriteLine(s);
|
||||
|
||||
string GetHashKey(string line)
|
||||
{
|
||||
var x = line.IndexOf("__cmtkn");
|
||||
var d = line.Substring(x + 16);
|
||||
var m = d.IndexOf("\"");
|
||||
var a = d.Substring(0, m);
|
||||
return a;
|
||||
}
|
||||
|
||||
*/
|
||||
}
|
||||
Reference in New Issue
Block a user