예제 #1
0
        private TaskInstancePublicationCache ToCache4(Task arg2)
        {
            var arg1   = new TaskInstancePublicationCache();
            var access = BCT.Context.BulletinDb.Accesses.Find(arg2.AccessId);

            if (access != null)
            {
                arg1.Login    = access.Login;
                arg1.Password = access.Password;
            }
            else
            {
                arg2.ErrorDescription = "Create task model TaskInstancePublicationCache crash. Access not found";
                BackTaskHelper.Error(new[] { arg2 });
                return(null);
            }
            var groupSign = GroupHelper.GetGroupSignature2(arg2.BulletinId.Value);

            if (groupSign != null)
            {
                arg1.Category1 = groupSign.Category1 == null ? "" : groupSign.Category1;
                arg1.Category2 = groupSign.Category2 == null ? "" : groupSign.Category2;
                arg1.Category3 = groupSign.Category3 == null ? "" : groupSign.Category3;
                arg1.Category4 = groupSign.Category4 == null ? "" : groupSign.Category4;
                arg1.Category5 = groupSign.Category5 == null ? "" : groupSign.Category5;
            }
            else
            {
                arg2.ErrorDescription = "Create task model TaskInstancePublicationCache crash. Groups not found";
                BackTaskHelper.Error(new[] { arg2 });
                return(null);
            }
            var bulletin = BCT.Context.BulletinDb.Bulletins.Find(arg2.BulletinId);

            if (bulletin != null)
            {
                arg1.Title       = bulletin.Title;
                arg1.Description = bulletin.Description;
                arg1.Price       = bulletin.Price;
                arg1.Images      = bulletin.Images.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None).ToArray();
            }
            else
            {
                arg2.ErrorDescription = "Create task model TaskInstancePublicationCache crash. Bulletin not found";
                BackTaskHelper.Error(new[] { arg2 });
                return(null);
            }
            arg1.InstanceId = arg2.InstanceId.Value;
            return(arg1);
        }
예제 #2
0
        public override string InstancePublication(FirefoxDriver driver, TaskInstancePublicationCache taskModel)
        {
            string result = null;

            try
            {
                if (!Auth(driver, taskModel.Login, taskModel.Password))
                {
                    return(result);
                }
                SendMessage($"InstancePublication => Started from task {taskModel.Id}. Instance {taskModel.InstanceId}");
                WaitPage(driver, 30000, "www.avito.ru/additem");
                WaitExecute(driver);

                WaitPage(driver, 30000, "header-button-add-item");

                FindTagByTextContains(driver, "a", "Подать объявление", e => JsClick(driver, e));
                SendMessage($"InstancePublication => Click from Add button");

                WaitPage(driver, 30000, "Выберите категорию");
                WaitExecute(driver);
                WaitPage(driver, 30000, "Животные");

                SendMessage($"InstancePublication => Page from add bulletin loaded");

                var setCategory     = false;
                var oldImplicitWait = driver.Manage().Timeouts().ImplicitWait;
                driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(5);
                var wait = new WebDriverWait(driver, TimeSpan.FromMilliseconds(60000));
                wait.Until(d =>
                {
                    var category1 = false;
                    var category2 = false;
                    var category3 = false;
                    var category4 = false;
                    var category5 = false;

                    //SetCategory first
                    FindTagByTextContains(driver, "button", "Животные", e => JsClick(driver, e));
                    SendMessage($"InstancePublication => Set default category 'Животные'");
                    WaitPage(driver, 10000, taskModel.Category1);
                    //SetCategory
                    if (!string.IsNullOrWhiteSpace(taskModel.Category1))
                    {
                        FindTagByTextContains(driver, "button", taskModel.Category1, e => JsClick(driver, e));
                        if (WaitPage(driver, 10000, taskModel.Category2))
                        {
                            SendMessage($"InstancePublication => Set category 1 {taskModel.Category1}");
                            category1 = true;
                        }
                        ;
                    }
                    else
                    {
                        category1 = true;
                    }
                    if (!string.IsNullOrWhiteSpace(taskModel.Category2))
                    {
                        FindTagByTextContains(driver, "button", taskModel.Category2, e => JsClick(driver, e));
                        if (WaitPage(driver, 10000, taskModel.Category3))
                        {
                            SendMessage($"InstancePublication => Set category 2 {taskModel.Category2}");
                            category2 = true;
                        }
                    }
                    else
                    {
                        category2 = true;
                    }
                    if (!string.IsNullOrWhiteSpace(taskModel.Category3))
                    {
                        FindTagByTextContains(driver, "button", taskModel.Category3, e => JsClick(driver, e));
                        if (WaitPage(driver, 10000, taskModel.Category4))
                        {
                            SendMessage($"InstancePublication => Set category 3 {taskModel.Category3}");
                            category3 = true;
                        }
                    }
                    else
                    {
                        category3 = true;
                    }
                    if (!string.IsNullOrWhiteSpace(taskModel.Category4))
                    {
                        FindTagByTextContains(driver, "button", taskModel.Category4, e => JsClick(driver, e));
                        if (WaitPage(driver, 10000, taskModel.Category5))
                        {
                            SendMessage($"InstancePublication => Set category 4 {taskModel.Category4}");
                            category4 = true;
                        }
                    }
                    else
                    {
                        category4 = true;
                    }
                    if (!string.IsNullOrWhiteSpace(taskModel.Category5))
                    {
                        FindTagByTextContains(driver, "button", taskModel.Category5, e => JsClick(driver, e));
                        SendMessage($"InstancePublication => Set category 5 {taskModel.Category5}");
                        category5 = true;
                    }
                    else
                    {
                        category5 = true;
                    }

                    if (category1 && category2 && category3 && category4 && category5)
                    {
                        setCategory = true;
                        return(true);
                    }
                    return(false);
                });
                driver.Manage().Timeouts().ImplicitWait = oldImplicitWait;
                if (setCategory)
                {
                    SendMessage($"InstancePublication => Category");
                }

                //Select type
                JsClick(driver, By.CssSelector($"input[value='20018']"));
                SendMessage($"InstancePublication => Select owner type");


                var comboboxCount = driver.FindElementByName("locationId");
                if (comboboxCount.Displayed)
                {
                    IList <IWebElement> allOptions = comboboxCount.FindElements(By.TagName("option"));
                    var option = allOptions.FirstOrDefault(q => q.Selected);
                    driver.ExecuteScript("arguments[0].setAttribute(arguments[1], arguments[2]);", option, "value", "639180");
                    driver.ExecuteScript("arguments[0].setAttribute(arguments[1], arguments[2]);", option, "data-parent-id", "637680");
                    SendMessage($"InstancePublication => Set location from selector");
                }
                else
                {
                    DoAction(driver, By.CssSelector($"input[id='item-edit__address']"), e =>
                    {
                        e.Clear();
                        e.SendKeys("Московская область, Подольск");
                        e.SendKeys(OpenQA.Selenium.Keys.Return);
                        Find(driver, "li", "data-marker", "suggest(0)", e2 => JsClick(driver, e2));
                    });
                    SendMessage($"InstancePublication => Set location from search string");
                }

                //Set fields
                //Title
                DoAction(driver, By.CssSelector($"input[id='item-edit__title']"), e => e.SendKeys(taskModel.Title));
                DoAction(driver, By.CssSelector($"textarea[id='item-edit__description']"), e => e.SendKeys(taskModel.Description));
                DoAction(driver, By.CssSelector($"input[id='item-edit__price']"), e => e.SendKeys(taskModel.Price));

                SendMessage($"InstancePublication => Set fields");

                if (taskModel.Images != null && taskModel.Images.Any())
                {
                    var count = 0;
                    foreach (var img in taskModel.Images)
                    {
                        try
                        {
                            count++;
                            var file = ImageHelper.ImageToTemp(img);
                            SendMessage($"InstancePublication => Image {img} save to {file}");
                            if (string.IsNullOrWhiteSpace(file))
                            {
                                continue;
                            }
                            var fileInput = driver.FindElementByCssSelector($"input[name='image']");
                            fileInput.SendKeys(file);
                            WaitElementCountByCssSelector(driver, 30, count, "div[class~='form-uploader-item'][data-state='active']");
                            SendMessage($"InstancePublication => Set image {img}");
                        }
                        catch (Exception ex)
                        {
                            var r4 = ex;
                        }
                    }
                }
                WaitExecute(driver);
                //Select base bulletin
                if (driver.PageSource.Contains("Разово"))
                {
                    SendMessage($"InstancePublication => Account {taskModel.Login} is blocked from publication limit");
                    Find(driver, "input", "name", "fees[eula]", c => JsClick(driver, c));
                }

                try
                {
                    WaitPage(driver, 10000, "Продолжить с пакетом «Обычная продажа»");
                    FindTagByTextContains(driver, "span", "Обычная продажа", e => JsClick(driver, e));
                }
                catch (Exception ex)
                {
                    var r5 = ex;
                    SendMessage($"InstancePublication => Error:{r5}");
                }

                WaitExecute(driver);
                SendMessage($"InstancePublication => Select sale type ");

                FindTagByTextContains(driver, "button", "Продолжить с пакетом «Обычная продажа»", e => JsClick(driver, e));
                WaitExecute(driver);
                SendMessage($"InstancePublication => Click continue");

                var servicePremium = Find(driver, By.Id("service-premium"));
                if (servicePremium != null && servicePremium.GetAttribute("checked") != null)
                {
                    JsClick(driver, By.Id("service-premium"));
                }
                var serviceVip = Find(driver, By.Id("service-vip"));
                if (serviceVip != null && serviceVip.GetAttribute("checked") != null)
                {
                    JsClick(driver, By.Id("service-vip"));
                }
                var serviceHighlight = Find(driver, By.Id("service-highlight"));
                if (serviceHighlight != null && serviceHighlight.GetAttribute("checked") != null)
                {
                    JsClick(driver, By.Id("service-highlight"));
                }
                WaitExecute(driver);
                SendMessage($"InstancePublication => Paid option has been disabled ");

                var isClassicDesign = false;
                var button          = FindMany(driver, By.TagName("button")).FirstOrDefault(q => q.Text == "Подать объявление");
                if (button == null)
                {
                    isClassicDesign = true;
                    button          = FindMany(driver, By.TagName("button")).FirstOrDefault(q => q.Text == "Продолжить");
                }

                JsClick(driver, button);
                WaitExecute(driver);
                SendMessage($"InstancePublication => Click continue");

                //Забираем URL
                if (isClassicDesign)
                {
                    var a = Find(driver, By.XPath("//*[@class='content-text']/p/a"));
                    if (a != null)
                    {
                        var href = a.GetAttribute("href");
                        result = href;
                    }
                }
                else
                {
                    FindContains(driver, "a", "class", "item-added-popup-link", e =>
                    {
                        var href = e.GetAttribute("href");
                        result   = href;
                    });
                }

                ////Забираем идентификатор
                //var idPattern = "itemId=(\\d+)";
                //var pageSource = driver.PageSource;
                //var id = RegexHelper.GetValue(idPattern, pageSource);

                ////Переходим на страницу профиля и забираем полный url по идентификатору
                //var inactivePage = @"www.avito.ru/profile/items/inactive";
                //driver.Navigate().GoToUrl("http://" + inactivePage + $"/rossiya?p=1&s=4");
                //WaitPage(driver, 3000, inactivePage);
                //WaitExecute(driver);
                //var nextPagePattern = "(Следующая страница)";
                //var urlPattern = $"a name=\"item_{id}\" href=\"(.*?)\"";
                //var pages = 1;
                //var hasNextPage = true;
                //while (hasNextPage)
                //{
                //    pages++;
                //    var html = driver.PageSource;
                //    hasNextPage = !string.IsNullOrEmpty(RegexHelper.GetValue(nextPagePattern, html));

                //    var match = RegexHelper.GetValue(urlPattern, html);
                //    if (!string.IsNullOrEmpty(match))
                //    {
                //        result = @"https://www.avito.ru" + match;

                //        SendMessage($"InstancePublication => Url is found - {result}");
                //        break;
                //    }
                //    if (hasNextPage)
                //    {
                //        driver.Navigate().GoToUrl("http://" + inactivePage + $"/rossiya?p={pages}&s=4");
                //        WaitPage(driver, 30000, inactivePage + $"/rossiya?p={pages}&s=4");
                //    }
                //}
                if (string.IsNullOrEmpty(result))
                {
                    SendMessage($"InstancePublication => Url is not found");
                }
                else
                {
                    SendMessage($"InstancePublication => Url is found - {result}");
                }
            }
            catch (Exception ex)
            {
            }
            return(result);
        }