Пример #1
0
        // Element         Spalte
        // Datum            1
        // Uhrzeit          2
        // Shop             4
        // Preis            8,9
        // Transaktionscode 16

        public static void ReadFromFile()
        {
            List <Payment> newPayments = new List <Payment>();
            OpenFileDialog ofd         = new OpenFileDialog();

            ofd.Filter = "CSV|*.csv";
            if (ofd.ShowDialog() == true)
            {
                var lines = File.ReadAllLines(ofd.FileName);
                lines[0] = "";

                foreach (var line in lines)
                {
                    if (string.IsNullOrEmpty(line))
                    {
                        continue;
                    }

                    try
                    {
                        var lineSplits = line.Split(new string[] { "\",\"" }, StringSplitOptions.None);
                        if (lineSplits[4].StartsWith("Allgemeine"))
                        {
                            continue;
                        }
                        var dateSplits = lineSplits[0].Replace("\"", "").Split('.');
                        var timeSplits = lineSplits[1].Split(':');
                        var year       = int.Parse(dateSplits[2]);
                        var month      = int.Parse(dateSplits[1]);
                        var day        = int.Parse(dateSplits[0]);
                        var hour       = int.Parse(timeSplits[0]);
                        var minute     = int.Parse(timeSplits[1]);
                        var second     = int.Parse(timeSplits[2]);

                        var dt = new DateTimeOffset(
                            year, month, day, hour, minute, second,
                            new TimeSpan(0)
                            );

                        var price = double.Parse(lineSplits[7]);
                        if (price > 0)
                        {
                            continue;
                        }

                        var payment = new Payment(-price, lineSplits[3], dt, lineSplits[12]);
                        newPayments.Add(payment);
                    }
                    catch (Exception ex)
                    {
                        Logging.Log.Error($"Could not Parse {line} to payment. {ex}");
                        continue;
                    }
                }
            }
            else
            {
                return;
            }

            var currentPayments = DataBaseHelper.GetPaymentsFromDB();

            Logging.Log.Info($"found {newPayments.Count} payments in {ofd.FileName}");
            int i = 0;

            foreach (var payment in newPayments)
            {
                if (currentPayments.Any(curPay => curPay.Equals(payment)))
                {
                    Logging.Log.Info($"payment is already in database: {payment.Price}€, {payment.Shop}, {payment.DateOfPayment.ToString("yyyy-MM-dd HH:mm:ss")}");
                    continue;
                }
                while (currentPayments.Any(curPay => curPay.DateOfPayment == payment.DateOfPayment))
                {
                    payment.DateOfPayment = payment.DateOfPayment.AddMinutes(1);
                }
                currentPayments.Add(payment);
                DataBaseHelper.AddPaymentToDB(payment);
                i++;
            }
            Logging.Log.Info($"added {i} new payments to database");

            RaiseCsvFinished();
        }
Пример #2
0
        public void ReadImap()
        {
            var            currentPayments = DataBaseHelper.GetPaymentsFromDB();
            DateTimeOffset date            = (currentPayments?.Count > 0)
                ? currentPayments.Last().DateOfPayment.AddDays(-1)
                : new DateTimeOffset(DateTime.Today.AddYears(-10));

            GetCredentials(out string user, out string pw);

            var mailRepository = new MailRepository(
                "imap.gmail.com",
                993,
                true,
                user,
                pw
                );

            var payPalFolder = Globals.Settings.FirstOrDefault(s => s.Name == Globals.PAYPAL_FOLDER_SETTING).Value;

            payPalFolder = payPalFolder == string.Empty ? DEFAULT_FOLDER : payPalFolder;
            // Get the mails from the Inbox and paypalfolder
            var emailList  = mailRepository.GetMailsSince(payPalFolder, new DateTime(date.Year, date.Month, date.Day));
            var emailList2 = mailRepository.GetMailsSince("INBOX", new DateTime(date.Year, date.Month, date.Day));

            // put the ID of the Inobx into the collection
            emailList.ForEach(mail =>
            {
                if (emailList2.Any(m2 => m2.TimeReceived == mail.TimeReceived))
                {
                    mail.InBoxID = emailList2.First(m2 => m2.TimeReceived == mail.TimeReceived).InBoxID;
                }
            });

            var newPayments = new List <Payment>();

            var regexStrings = new List <string>();

            regexStrings.Add("Sie\\shaben\\s((eine\\s(Zahlung|Bestellung)\\süber\\s)|)(\\W{0,1}|.*;)(\\d+[,.]\\d{2})[\\s\\S]EUR\\san (.*) (genehmigt|gesendet|autorisiert)");

            List <EMail> messagesToDelete = new List <EMail>();

            foreach (var email in emailList)
            {
                Match match   = null;
                var   success = false;
                foreach (var reg in regexStrings)
                {
                    // create regex for relevant text - group[0]=complete text, group[2]=amount, group[3]=shop
                    Regex paymentRegex = new Regex(reg);

                    // search for the match
                    match = paymentRegex.Match(email.Body);
                    // if there is no match.. continue
                    if (!match.Success)
                    {
                        //Debug.WriteLine(email.Subject);
                        continue;
                    }
                    success = true;
                    break;
                }

                // continue to next email if the text didnt match any of the regular expressions
                if (!success)
                {
                    continue;
                }

                // otherwise fetch data
                var priceText = match.Groups[5].Value;
                var shop      = match.Groups[6].Value;
                double.TryParse(priceText, out double price);

                // get transaction and authorization
                Regex  transaktion   = new Regex("Transaktionscode:{0,1}\\s*(\\w{17})");
                Regex  autorisierung = new Regex("Autorisierungscode:{0,1}\\s*(\\w{6})");
                var    tmatch        = transaktion.Match(email.Body);
                string trans         = "";
                if (tmatch.Success)
                {
                    trans = tmatch.Groups[1].Value;
                }
                else
                {
                    continue;
                }
                var    amatch = autorisierung.Match(email.Body);
                string auth   = "";
                if (amatch.Success)
                {
                    auth = amatch.Groups[1].Value;
                }

                // put data into a class
                newPayments.Add(new Payment(price, shop, new DateTimeOffset(email.TimeReceived.Ticks, new TimeSpan(0)), trans, auth));
                messagesToDelete.Add(email);
            }

            Logging.Log.Info($"found {newPayments.Count} new payments via PayPal");
            foreach (var payment in newPayments)
            {
                if (currentPayments.Any(curPay => curPay.Equals(payment)))
                {
                    Logging.Log.Info($"payment is already in database: {payment.Price}€, {payment.Shop}, {payment.DateOfPayment.ToString("yyyy-MM-dd HH:mm:ss")}");
                    continue;
                }
                while (currentPayments.Any(curPay => curPay.DateOfPayment == payment.DateOfPayment))
                {
                    payment.DateOfPayment = payment.DateOfPayment.AddMinutes(1);
                }
                currentPayments.Add(payment);
                DataBaseHelper.AddPaymentToDB(payment);
            }

            RaiseMailFinished();

            // delete the messages from the gmail folders
            mailRepository.DeleteMails(messagesToDelete, "INBOX");
            if (payPalFolder.ToUpper() != "INBOX")
            {
                mailRepository.DeleteMails(messagesToDelete, payPalFolder);
            }
        }