Пример #1
0
        public void Handle(AliExpressPostResult result)
        {
            var postedLogFormat = "Posted item {0}, Status: {1}, Target Url: {2}, Reason: {3}";

            Console.WriteLine(string.Format(postedLogFormat, result.SourceUrl, result.Success ? "Success" : "Failed",
                                            result?.PostedUrl, result?.Reason));
        }
Пример #2
0
        public void Handle(AliExpressPostResult result)
        {
            var listViewSubItem1 = new ListViewItem.ListViewSubItem();

            listViewSubItem1.Text = result.SourceUrl;

            var listViewSubItem2 = new ListViewItem.ListViewSubItem();

            listViewSubItem2.Text = result.Success ? "Success" : "Failed";

            var listViewSubItem3 = new ListViewItem.ListViewSubItem();

            listViewSubItem3.Text = result?.Name;

            var listViewSubItem4 = new ListViewItem.ListViewSubItem();

            listViewSubItem4.Text = result?.PostedUrl;

            var listViewSubItem5 = new ListViewItem.ListViewSubItem();

            listViewSubItem5.Text = result?.Reason;

            var listViewItem = new ListViewItem(index++.ToString());

            listViewItem.SubItems.Add(listViewSubItem1);
            listViewItem.SubItems.Add(listViewSubItem2);
            listViewItem.SubItems.Add(listViewSubItem3);
            listViewItem.SubItems.Add(listViewSubItem4);
            listViewItem.SubItems.Add(listViewSubItem5);

            RunOnUIThread(() =>
            {
                listView1.Items.Add(listViewItem);
            });
        }
Пример #3
0
        private void CreateListViewItem(AliExpressPostResult result)
        {
            var listViewSubItem1 = new ListViewItem.ListViewSubItem();

            listViewSubItem1.Text = result.SourceUrl;

            var listViewSubItem2 = new ListViewItem.ListViewSubItem();

            listViewSubItem2.Text = result.Success ? "Success" : "Failed";

            var listViewSubItem3 = new ListViewItem.ListViewSubItem();

            listViewSubItem3.Text = result?.Name;

            var listViewSubItem4 = new ListViewItem.ListViewSubItem();

            listViewSubItem4.Text = result?.PostedUrl;

            var listViewItem = new ListViewItem(index++.ToString());

            listViewItem.SubItems.Add(listViewSubItem1);
            listViewItem.SubItems.Add(listViewSubItem2);
            listViewItem.SubItems.Add(listViewSubItem3);
            listViewItem.SubItems.Add(listViewSubItem4);

            listView1.Items.Add(listViewItem);
        }
Пример #4
0
        public void Run()
        {
            PostType postType = PostType.Draft;

            if (postTypeStr.ToLower().Equals("publish"))
            {
                postType = PostType.Publish;
            }

            foreach (var categoryUrl in included)
            {
                try
                {
                    this.logger.Info("Posting category: " + categoryUrl);
                    var currentCategoryUrl = categoryUrl;
                    var totalPosted        = 0;
                    for (int page = 1; page < 100; page++)
                    {
                        //currentCategoryUrl = currentCategoryUrl.Replace(".html", "/" + page + ".html");

                        var urlBuilder = new UriBuilder(currentCategoryUrl);
                        var nvc        = new NameValueCollection();
                        nvc.Add("shipCountry", "MY");
                        nvc.Add("minPrice", priceLowCap);
                        nvc.Add("maxPrice", priceHighCap);
                        nvc.Add("isFreeShip", "y");
                        nvc.Add("site", "glo");
                        urlBuilder.Query = ToQueryString(nvc);

                        this.logger.Info("Start processing category " + urlBuilder);

                        HtmlNode.ElementsFlags.Remove("form");
                        IWebDriver browser = Browser.Instance;

                        browser.Navigate().GoToUrl(urlBuilder.ToString());
                        var sourceCode = browser.PageSource;
                        //browser.Close();
                        var doc = new HtmlDocument();
                        doc.LoadHtml(sourceCode);
                        //doc = new HtmlWeb { UserAgent = chromeUserAgent }.Load(urlBuilder.ToString());
                        var productAnchors = doc.DocumentNode.Descendants()
                                             .Where(
                            a =>
                            a.Name.Equals("a") && a.Attributes["class"] != null &&
                            a.Attributes["class"].Value.Trim().Equals("product") &&
                            a.Attributes["href"] != null && a.Attributes["href"].Value.Contains("/item/"))
                                             .ToList();

                        this.logger.Info("Category returned " + productAnchors.Count + " urls");

                        foreach (var anchor in productAnchors)
                        {
                            bool loggedPause = false;
                            while (paused)
                            {
                                if (!loggedPause)
                                {
                                    loggedPause = true;
                                    this.logger.Info("Bot paused..");
                                }
                                Thread.Sleep(1000);
                            }

                            var postingLogFormat = "Posting item {0}";

                            var url = anchor.Attributes["href"].Value;

                            if (url.Contains("?"))
                            {
                                url = url.Substring(0, url.IndexOf("?"));
                            }

                            var sku = new Uri(url).Segments.Last().Replace(".html", "");

                            var poster = new AliExpressPoster(
                                restApiUrl,
                                restAPIKey,
                                restAPISecret,
                                postType,
                                markUpPercentage,
                                usdtoMyrCurrencyRate,
                                productMinPriceAfterConvert,
                                productBelowMinMarkup);

                            this.logger.Info(string.Format(postingLogFormat, url));

                            AliExpressPostResult result = null;
                            if (poster.ProductExists(sku))
                            {
                                result = new AliExpressPostResult
                                {
                                    SourceUrl = url,
                                    Success   = false,
                                    Reason    = "Product already exist"
                                };
                            }
                            else
                            {
                                // result = poster.Post(url);
                            }

                            this.resultHandler.Handle(result);

                            if (result.Success)
                            {
                                totalPosted++;
                            }

                            if (totalPosted > maxItemPerCategory)
                            {
                                break;
                            }

                            if (!result.Success && result.Reason.Equals("Login page"))
                            {
                                this.logger.Warn("Hit into login page. Pausing for " + productPauseDelay);
                                Thread.Sleep(loginPageAppearedPauseDelay);
                            }

                            // Product exist dont pause.
                            if (string.IsNullOrEmpty(result.Reason) ||
                                (!string.IsNullOrEmpty(result.Reason) && !result.Reason.Equals("Product already exist")))
                            {
                                this.logger.Info("Pausing for " + productPauseDelay);
                                Thread.Sleep(productPauseDelay);
                            }
                        }

                        if (totalPosted > maxItemPerCategory)
                        {
                            this.logger.Warn("Reached limit of " + totalPosted + " for category " + currentCategoryUrl);
                            break;
                        }

                        var nextPage    = page + 1;
                        var pageElement = doc.DocumentNode.Descendants()
                                          .FirstOrDefault(a => a.Name.Equals("div") && a.Attributes["class"] != null &&
                                                          a.Attributes["class"].Value.Equals("ui-pagination-navi util-left"));
                        var allPageElement  = pageElement.Descendants().Where(a => a.Name.Equals("a"));
                        var nextPageElement = allPageElement.FirstOrDefault(a => a.InnerText.Equals(nextPage.ToString()));

                        currentCategoryUrl = nextPageElement.Attributes["href"].Value;

                        this.logger.Info("Done processing category " + currentCategoryUrl);
                    }
                }
                catch (Exception ex)
                {
                    this.logger.Error("Woops. Pausing 30 minutes..", ex);
                    Thread.Sleep(TimeSpan.FromMinutes(30));
                }
            }

            this.logger.Info("Done posting all products.");
        }