Ejemplo n.º 1
0
        private int GetProductInfoSku(HtmlAgilityPack.HtmlDocument _doc, ref ProductInfoSku PIS, ref List <string> images, ref List <String_String> name_and_des_title_img)
        {
            /* try
             * {*/
            HtmlNode jProductInfoSku = _doc.GetElementbyId("j-product-info-sku");

            IEnumerable <HtmlNode> proItems = jProductInfoSku.Elements("dl");

            foreach (HtmlNode proItem in proItems)
            {
                string name = "";
                //Gonna be added to NameVal_String
                NameValueCollection skuList = new NameValueCollection();
                int hasIMGTag = 0;

                name = proItem.Element("dt").InnerText.Replace(":", "");
                HtmlAgilityPack.HtmlNode jSkuList = proItem.Element("dd").Element("ul");
                IEnumerable <HtmlNode>   itemSkus = jSkuList.Elements("li");
                //itemSku: li element contains class item-sku-color
                foreach (HtmlNode itemSku in itemSkus)
                {
                    string dataSkuId = itemSku.FirstChild.Attributes["data-sku-id"].Value;
                    string title;
                    if (itemSku.FirstChild.Attributes.Contains("title"))
                    {
                        title = itemSku.FirstChild.Attributes["title"].Value;
                    }
                    else
                    {
                        title = itemSku.FirstChild.InnerText;
                    }
                    skuList.Add(dataSkuId, title);
                    //Images of colors

                    if (itemSku.FirstChild.Descendants("img").Count <HtmlNode>() > 0)
                    {
                        if (itemSku.FirstChild.Element("img").Attributes.Contains("bigpic"))
                        {
                            string bigpicWithTail    = itemSku.FirstChild.Element("img").Attributes["bigpic"].Value;
                            string bigpicWithoutTail = bigpicWithTail.Remove(bigpicWithTail.LastIndexOf('_'));
                            images.Add(bigpicWithoutTail);

                            string values_s = "<tr><td>" + name + ": " + title + "</td><td><img width='480' height='480' src='" + bigpicWithTail + "'></td></tr>";
                            name_and_des_title_img.Add(new String_String(name, values_s));
                        }
                        hasIMGTag = 1;
                    }

                    //Background colors
                    else if (itemSku.FirstChild.Descendants("span").Where(n => n.Attributes.Contains("class") && n.Attributes["class"].Value.Contains("sku-color-")).Count <HtmlNode>() > 0)
                    {
                        MessageBox.Show(itemSku.FirstChild.Descendants("span").First().OuterHtml);
                        HtmlNode backgroundColorCSSElem = itemSku.FirstChild.Descendants("span").Where(n => n.Attributes.Contains("class") && n.Attributes["class"].Value.Contains("sku-color-")).First();
                        string   className  = backgroundColorCSSElem.Attributes["class"].Value;
                        string   colorStyle = BackgroundColorSelector(className);
                        if (colorStyle == "")
                        {
                            return(-2);
                        }
                        string values_s = "<tr><td>" + name + ": " + title + "</td><td width='480' height='480' style='" + BackgroundColorSelector(className) + "'></td></tr>";
                        name_and_des_title_img.Add(new String_String(name, values_s));
                    }
                    else if (name.ToLower() == "color")
                    {
                        MessageBox.Show("Color not found! Liên hệ https://www.facebook.com/letruongquy96 \nOuterHTML: " + itemSku.InnerHtml);
                        return(-1);
                    }
                }
                NameVal_String skuList_hasIMGTag = new NameVal_String(skuList, hasIMGTag);
                PropertyItem   tempPI            = new PropertyItem(name, skuList_hasIMGTag);
                PIS.Add(tempPI);
            }

            /*}
             * catch(Exception ex)
             * {
             *  MessageBox.Show("GetProductInfoSku: " + ex.Message);
             *  return -7;
             * }*/

            return(0);
        }
Ejemplo n.º 2
0
        private int ProcessOneLink(string _link, ref StringBuilder _csv, List <string> _namesTempList)
        {
            string haha = _link;

            HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
            HtmlWeb htWeb = new HtmlWeb();

            htWeb.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36";
            doc             = htWeb.Load(_link);
            haha           += " " + doc.DocumentNode.OuterHtml;
            Clipboard.SetText(haha);
            if (doc == null)
            {
                MessageBox.Show("Error loading document. Please check the internet connection!");
                return(-6);
            }
            NameValueCollection record = new NameValueCollection();

            List <string> images = new List <string>();

            List <String_String> name_and_des_title_img = new List <String_String>();
            ProductInfoSku       PIS = new ProductInfoSku();
            int getPIS_code          = GetProductInfoSku(doc, ref PIS, ref images, ref name_and_des_title_img);

            if (getPIS_code != 0)
            {
                MessageBox.Show("GetProductInfoSku returns code: " + getPIS_code);
                return(-3);
            }
            List <string> namesOfSku = PIS.GetAllNames();

            //Description and brand
            HtmlNode desMasterNode = doc.DocumentNode.SelectSingleNode("//*[@id='j-product-desc']/div[1]/div[2]/ul");
            string   des           = ""; //+ Transfer.moreDescriptionHead + "\n";
            string   brandName     = "Unbranded";

            foreach (HtmlNode desChilds in desMasterNode.Elements("li"))
            {
                if (desChilds.InnerText.Contains("Brand Name:"))
                {
                    brandName = desChilds.Elements("span").Last().InnerText;
                }
                des += Regex.Replace(desChilds.InnerText.Replace("\n", ""), @"\s+", " ") + "\n";
            }
            des  = des.Replace("\n", "<br>");
            des += "<table style='width:100%' border='5'>";
            for (int _i = 0; _i < name_and_des_title_img.Count; _i++)
            {
                des += name_and_des_title_img.ElementAt(_i).name2; // name_and_des_title_img.Get(_i)
            }
            des += "</table><br>";
            record.Add("description", des);
            //Title
            string titleTextField = doc.DocumentNode.SelectSingleNode("//*[@id='j-product-detail-bd']/div[1]/div/h1/text()").InnerText;

            titleTextField = titleTextField.Replace(brandName, "");
            record.Add("title", titleTextField);



            HtmlNode scriptNode        = doc.DocumentNode.Descendants().Where(n => n.Name == "script" && n.InnerText.Contains("skuProducts")).First();
            string   skuProductsString = "";

            TrimStringByTwoChars('[', ']', scriptNode.InnerText, ref skuProductsString, "skuProducts");
            JArray jsonArray   = JArray.Parse(skuProductsString);
            string traitsField = "";



            //Condition
            traitsField += "[[Condition:" + Transfer.conditionStatus + "]]";

            //Weight
            string weight = "0";

            weight       = doc.DocumentNode.Descendants("li").Where(n => n.HasClass("packaging-item") && n.InnerText.Contains("Package Weight:")).First().Descendants("span").Where(m => m.InnerText.Contains("kg")).First().Attributes["rel"].Value;
            traitsField += "[[weight:" + weight + "]][[weight_unit:kg]]";

            //trait
            int totalQtt = 0;

            foreach (JToken jToken in jsonArray)
            {
                string   childTrait = "[";
                string   skuPropIds = jToken["skuPropIds"].ToString();
                string[] skuIds     = skuPropIds.Split(',');
                for (int i = 0; i < skuIds.Count <string>(); i++)
                {
                    childTrait += "[" + namesOfSku.ElementAt(i) + ":" + PIS.proItemList.ElementAt(i).skuList_hasIMGTag.skuList[skuIds.ElementAt(i)] + "]";
                }

                string displayPrice = "";
                //Discount price
                if (rdoDiscountPrice.Checked)
                {
                    if (jToken["skuVal"]["actSkuMultiCurrencyDisplayPrice"] != null)
                    {
                        displayPrice = jToken["skuVal"]["actSkuMultiCurrencyDisplayPrice"].ToString();
                    }
                    else
                    {
                        displayPrice = jToken["skuVal"]["skuMultiCurrencyDisplayPrice"].ToString();
                    }
                }
                if (rdoOriginalPrice.Checked)
                {
                    displayPrice = jToken["skuVal"]["skuMultiCurrencyDisplayPrice"].ToString();
                }

                if (displayPrice == "")
                {
                    MessageBox.Show("Error 153. Cannot continue!");
                    return(-10);
                }


                PricingMultiply(ref displayPrice);
                childTrait += "[price:" + displayPrice + "]";
                string availQuantity = jToken["skuVal"]["availQuantity"].ToString();
                if (availQuantity == "0" && FSF1.Controls.Find("chkReplaceZeroQtt", false).OfType <CheckBox>().First().Checked)
                {
                    availQuantity = FSF1.Controls.Find("mudReplaceZeroQtt", false).OfType <NumericUpDown>().First().Value.ToString();
                }
                totalQtt    += Int32.Parse(availQuantity);
                childTrait  += "[quantity:" + availQuantity + "]]";
                traitsField += childTrait;
            }
            record.Add("trait", traitsField);

            //quantity
            record.Add("quantity", totalQtt.ToString());

            //Images
            HtmlNode jImageThumbList         = doc.GetElementbyId("j-image-thumb-list");
            IEnumerable <HtmlNode> jImageLis = jImageThumbList.Elements("li");

            foreach (HtmlNode jImageLi in jImageLis)
            {
                string imgWithTail    = jImageLi.Element("span").Element("img").Attributes["src"].Value;
                string imgWithoutTail = imgWithTail.Remove(imgWithTail.LastIndexOf('_'));
                images.Add(imgWithoutTail);
            }
            string imagesField = string.Join(" ", images);

            record.Add("images", imagesField);

            //sku and id
            string gaDataString = "";

            TrimStringByTwoChars('{', '}', scriptNode.InnerText, ref gaDataString, "GaData");
            JToken productIds    = JToken.Parse(gaDataString)["productIds"];
            string IDandSKUField = "al" + productIds.ToString();

            record.Add("sku", IDandSKUField);
            record.Add("id", IDandSKUField);


            //Cheapest + fastest shipping price. shipping_price, shipping_type, shipping_service, worldwide_shipping_price, worldwide_shipping_type,
            string    baseShippingURL = @"https://freight.aliexpress.com/ajaxFreightCalculateService.htm?count=1&currencyCode=USD&sendGoodsCountry=&country=US";
            string    productID       = string.Join("", productIds.ToString().Where(char.IsDigit));
            WebClient webClient       = new WebClient();
            string    sourceString    = webClient.DownloadString(baseShippingURL + "&productid=" + productID);

            sourceString = sourceString.Remove(sourceString.IndexOf("("), 1);
            sourceString = sourceString.Remove(sourceString.LastIndexOf(")"));
            JToken shippingJsonRaw    = JToken.Parse(sourceString);
            List <ShippingMethod> SMs = new List <ShippingMethod>();
            int    cheapestInx        = -1;
            double cheapestPrice;

            decimal shortestTimeMean;
            int     shortestTimeMeanInx = -1;

            cheapestPrice = Convert.ToInt32(shippingJsonRaw["freight"][0]["price"].ToString());

            string _timeTemp = shippingJsonRaw["freight"][0]["time"].ToString();

            string[] _minMaxTimeTemp = _timeTemp.Split('-');
            shortestTimeMean = (Convert.ToInt32(_minMaxTimeTemp[0]) + Convert.ToInt32(_minMaxTimeTemp[1])) / 2m;


            for (int i = 0; i < shippingJsonRaw["freight"].Count(); i++)
            {
                string   _name       = shippingJsonRaw["freight"][i]["companyDisplayName"].ToString();
                string   _price      = shippingJsonRaw["freight"][i]["price"].ToString();
                string   _time       = shippingJsonRaw["freight"][i]["time"].ToString();
                string[] _minMaxTime = _time.Split('-');
                decimal  _timeMean   = (Convert.ToInt32(_minMaxTime[0]) + Convert.ToInt32(_minMaxTime[1])) / 2m;
                if (cheapestPrice >= Convert.ToDouble(_price))
                {
                    cheapestInx   = i;
                    cheapestPrice = Convert.ToDouble(_price);
                }
                if (shortestTimeMean >= _timeMean)
                {
                    shortestTimeMeanInx = i;
                    shortestTimeMean    = _timeMean;
                }
                SMs.Add(new ShippingMethod(_name, Convert.ToDouble(_price), _timeMean));
            }
            List <ShippingMethod> cheapestPricingSMs = new List <ShippingMethod>();

            for (int i = 0; i < SMs.Count; i++)
            {
                if (Convert.ToDouble(SMs.ElementAt(i).price) < cheapestPrice + 4)
                {
                    cheapestPricingSMs.Add(SMs.ElementAt(i));
                }
            }
            int            cheapestShortestSMInx = 0;
            ShippingMethod cheapestShortestSM    = cheapestPricingSMs.ElementAt(0);

            for (int i = 0; i < cheapestPricingSMs.Count; i++)
            {
                if (cheapestShortestSM.timeMean >= cheapestPricingSMs.ElementAt(i).timeMean)
                {
                    cheapestShortestSMInx = i;
                    cheapestShortestSM    = cheapestPricingSMs.ElementAt(i);
                }
            }
            record.Add("shipping_price", cheapestShortestSM.price.ToString());
            record.Add("shipping_type", "flat");
            if (cheapestShortestSM.timeMean > 0 && cheapestShortestSM.timeMean <= 10)
            {
                record.Add("shipping_service", "Economy shipping (1 to 10 business days)");
            }
            else if (cheapestShortestSM.timeMean > 10 && cheapestShortestSM.timeMean <= 22)
            {
                record.Add("shipping_service", "International Shipping (2 to 3 weeks)");
            }
            else if (cheapestShortestSM.timeMean > 22 && cheapestShortestSM.timeMean <= 28)
            {
                record.Add("shipping_service", "International Shipping (3 to 4 weeks)");
            }
            else if (cheapestShortestSM.timeMean > 28 && cheapestShortestSM.timeMean <= 45)
            {
                record.Add("shipping_service", "International Shipping (4 to 5 weeks)");
            }
            else
            {
                record.Add("shipping_service", "Unspecified shipping type");
            }
            record.Add("worldwide_shipping_price", FSF1.Controls.Find("nudWorldwideFlatRate", false).OfType <NumericUpDown>().First().Value.ToString());
            record.Add("worldwide_shipping_type", "flat");



            //alie_link
            record.Add("alie_id", productID);

            //force_update
            record.Add("force_update", "true");

            string newRecordString = "";

            foreach (string _name in _namesTempList)
            {
                newRecordString += "\"" + record[_name] + "\"" + ",";
            }
            _csv.AppendLine(newRecordString);

            return(0);
        }