public IEnumerable <PaymentModel> ScrapeStatement(ScraperConfigurationModel configuration, Chrome chromeDriver,
                                                          DateTime startFrom)
        {
            if (startFrom < DateTime.Now.AddYears(-2).AddDays(1))
            {
                startFrom = DateTime.Now.AddYears(-2).AddDays(1);
            }

            var driver = chromeDriver.Driver;

            Login(configuration, driver);

            var existingLinks = driver.FindElementsByPartialLinkText("История операций").ToList();

            var link1 = (RemoteWebElement)driver.FindElementByLinkText("Счета");

            driver.Mouse.MouseMove(link1.Coordinates, 1, 1);

            Thread.Sleep(500);

            existingLinks = driver.FindElementsByPartialLinkText("История операций").Except(existingLinks).ToList();
            existingLinks.Single().Click();

            Thread.Sleep(2000);

            var selectBtn = driver.FindElementById("pt1:soc1::button");

            var accountsChooser = driver.FindElementById("pt1:soc1::pop");

            var accs = accountsChooser.FindElements(By.TagName("tr"));

            var result = new List <PaymentModel>();

            foreach (var acc in accs)
            {
                var tds = acc.FindElements(By.TagName("td"));
                if (tds.Count < 4)
                {
                    continue;
                }

                selectBtn.Click();

                Thread.Sleep(2000);

                tds[0].Click();

                Thread.Sleep(2000);


                var inputDate = driver.FindElementById("pt1:id1::fd");
                inputDate.Click();

                Thread.Sleep(2000);

                driver.Keyboard.SendKeys(Enumerable.Repeat(Keys.Delete, 20).Join(""));
                Thread.Sleep(500);
                driver.Keyboard.SendKeys(Enumerable.Repeat(Keys.Backspace, 20).Join(""));
                Thread.Sleep(500);
                driver.Keyboard.SendKeys(Enumerable.Repeat(Keys.Delete, 20).Join(""));
                Thread.Sleep(500);
                driver.Keyboard.SendKeys(Enumerable.Repeat(Keys.Backspace, 20).Join(""));
                Thread.Sleep(500);
                driver.Keyboard.SendKeys(startFrom.ToString("ddMMyyyy"));

                var submit = driver.FindElementById("pt1:showButton::button");
                submit.Click();

                Thread.Sleep(2000);

                var csv = driver.FindElementById("pt1:downloadCSVLink");
                csv.Click();

                int waited = 0;
                while (chromeDriver.GetDownloads().Count < 1 && waited < 300)
                {
                    Thread.Sleep(1000);
                    waited++;
                }

                var files = chromeDriver.GetDownloads();
                if (files.Count == 1)
                {
                    var csvFile    = files.First();
                    var csvContent = File.ReadAllLines(csvFile.FullName, Encoding.GetEncoding(1251)).Skip(1).Select(v => new AlphaStatement(v)).ToList();
                    var payments   = csvContent.Select(v =>
                                                       Statement(v.Date, v.AccountName, v.What, v.Outcome - v.Income, v.Ccy, v.Reference)).ToList();

                    var holdPayments = payments.Where(v => v.StatementReference == "HOLD").ToList();
                    payments = payments.Except(holdPayments).ToList();

                    result.AddRange(payments);
                    csvFile.Delete();
                }

                chromeDriver.CleanupDownloads();
            }


            return(result);
        }
Example #2
0
        public override IList <PaymentModel> ScrapeStatement(ScraperConfigurationModel configuration, Chrome chrome, DateTime startFrom)
        {
            DoLogin(configuration, chrome);

            var driver = chrome.Driver;

            var btns = GetElements(driver, By.TagName("button"));

            var rightBtn = btns.First(s => s.Text.ToLower().Contains("выписка"));

            rightBtn.Click();

            var form = GetElement(driver, By.ClassName("address_form"));

            var switches = form.FindElements(By.ClassName("switch_item"));

            var htmlSwitch = switches.First(s => s.Text == "1C");

            htmlSwitch.Click();

            var rows = form.FindElements(By.ClassName("form_group"));

            var periodRow    = rows.First(v => v.Text.ToLower().Contains("за период:"));
            var periodButton = periodRow.FindElement(By.TagName("button"));

            periodButton.Click();

            var dateSelector = GetElement(driver, By.ClassName("filter_date_value"));
            var inputs       = dateSelector.FindElements(By.TagName("input"));
            var first        = inputs.First();

            first.Click();

            first.SendKeys(Keys.Control + "a");
            first.SendKeys(Keys.Delete);
            first.SendKeys(startFrom.ToString("dd.MM.yyyy"));
            first.SendKeys(Keys.Enter);

            var dateSelectorApply = dateSelector.FindElement(By.TagName("button"));

            dateSelectorApply.Click();

            var buttons  = form.FindElements(By.TagName("button"));
            var dlButton = buttons.First(s => s.Text.ToLower().Contains("получить"));

            chrome.CleanupDownloads();

            dlButton.Click();

            var waited = 0;

            while (chrome.GetDownloads().Count == 0 && waited < 300)
            {
                WaitForPageLoad(driver);
                waited++;
            }

            var dlItem = chrome.GetDownloads().First().FullName;

            var docs = ParseOdinAssFile(dlItem);

            var config = docs.Single(v => !v.ContainsKey("СекцияДокумент"));

            var account = config["РасчСчет"];

            var goodDocs = docs.Where(v => v.ContainsKey("СекцияДокумент"));

            var statements = goodDocs.Select(v =>
            {
                var whenString = v["Дата"];
                var when       = DateTime.ParseExact(whenString, "dd.MM.yyyy", CultureInfo.InvariantCulture);
                var what       = v["НазначениеПлатежа"];
                var amountText = v["Сумма"];
                var amount     = double.Parse(amountText, new NumberFormatInfo()
                {
                    NumberDecimalSeparator = "."
                });

                var isIncome  = v["ПолучательСчет"] == account;
                var kind      = isIncome ? PaymentKind.Income : PaymentKind.Expense;
                var ccy       = "RUB";
                var reference = v["Номер"];

                return(Statement(when, account, what, amount, kind, ccy, reference));
            }).ToList();

            return(statements);
        }