Exemplo n.º 1
0
        private void HandleProviderList(TaskProcess tp)
        {
            string html = Encoding.Default.GetString(tp.TaskData.Bytes);

            if (!Validate(html))
            {
                tp.CpResult.Success = false;
                return;
            }

            HtmlNode root = GetRoot(html);

            if (root == null)
            {
                FailProcess(tp);
                return;
            }
            HtmlNodeCollection list = root.SelectNodes(@"//div[@id='list-content']//li[@class='list-item']");

            if (list == null)
            {
                FailProcess(tp);
                return;
            }
            bool success = true;

            foreach (HtmlNode node in list)
            {
                success &= HandleProviderListNode(tp, node);
                if (!success)
                {
                    break;
                }
            }
            //next page
            string nextPageURL = HAH.SafeGetSuccessorAttributeStringValue(root, @".//a[@class='page-next']", "href");

            if (nextPageURL != null)
            {
                tp.CpResult.NewTasks.Add(new Task
                {
                    Url     = FixRelativeURL(nextPageURL, tp.TaskData.Task.Host),
                    Type    = (int)TaobaoTaskType.PROVIDER_LIST,
                    Context = tp.TaskData.Task.Context
                });
            }
            tp.CpResult.Success = success;
        }
Exemplo n.º 2
0
        private bool HandleCombinedListMultiShops(TaskProcess tp, HtmlNode node)
        {
            string link = HAH.SafeGetSuccessorAttributeStringValue(node, @".//div[@class='legend2']/a", "href");

            if (link == null)
            {
                LogMissing("URL", node.InnerHtml);
                return(false);
            }
            Match m = RegexUniqID.Match(link);

            if (m.Success)
            {
                string uniqIDs = m.Groups["number"].Value;
                tp.CpResult.NewTasks.Add(new Task
                {
                    Type    = (int)TaobaoTaskType.PROVIDER_LIST,
                    Url     = FixRelativeURL(link, tp.TaskData.Task.Host),
                    Context = uniqIDs,
                });
            }
            return(true);
        }
Exemplo n.º 3
0
        private bool HandleCombinedListSingleShop(TaskProcess tp, HtmlNode node)
        {
            #region build an item
            Item i = new Item();

            string freight = HAH.SafeGetSuccessorInnerText(node, @".//li[@class='shipment']/span[@class='fee']");
            if (freight != null && freight.Length > 3)
            {
                //运费:8.00
                string freightD = freight.Substring(3);
                double d;
                if (double.TryParse(freightD, out d))
                {
                    i.Freight = d;
                }
                else
                {
                    LogMissing("freight", freightD);
                    return(false);
                }
            }
            else
            {
                LogMissing("freight", freight);
                return(false);
            }

            i.Name = HAH.SafeGetSuccessorAttributeStringValue(node, @".//a[@class='EventCanSelect']", "title");
            if (i.Name == null)
            {
                LogMissing("Name", node.InnerHtml);
                return(false);
            }
            i.Location = HAH.SafeGetSuccessorInnerText(node, @".//li[@class='shipment']/span[@class='loc']");
            if (i.Location == null)
            {
                LogMissing("Location", node.InnerHtml);
                return(false);
            }
            string price = HAH.SafeGetSuccessorInnerText(node, @".//li[@class='price']/em");
            if (!string.IsNullOrEmpty(price))
            {
                //359.00
                double d;
                if (double.TryParse(price, out d))
                {
                    i.Price = d;
                }
                else
                {
                    LogMissing("price", price);
                    return(false);
                }
            }
            else
            {
                LogMissing("price", price);
                return(false);
            }

            i.RecentDeal = 0;
            string recentDeal = HAH.SafeGetSuccessorInnerText(node, @".//li[@class='price']/span");

            if (!string.IsNullOrEmpty(recentDeal))
            {
                Match m = RegexRecentSellCount.Match(recentDeal);
                if (m.Success)
                {
                    i.RecentDeal = Int32.Parse(m.Groups["number"].Value);
                }
            }

            string sellerID = HAH.SafeGetSuccessorAttributeStringValue(node, @".//li[@class='seller']/a", "href");

            if (!string.IsNullOrEmpty(sellerID))
            {
                Match m = RegexSellerID.Match(sellerID);
                if (m.Success)
                {
                    i.SellerTaobaoId = Int32.Parse(m.Groups["number"].Value);
                }
                else
                {
                    LogMissing("SellerID", sellerID);
                    return(false);
                }
            }
            else
            {
                LogMissing("SellerID", node.InnerHtml);
                return(false);
            }

            i.UniqId  = 0;
            i.UrlLink = HAH.SafeGetSuccessorAttributeStringValue(node, ".//a[@class='EventCanSelect']", "href");
            i.UrlLink = FixRelativeURL(i.UrlLink, tp.TaskData.Task.Host);

            if (!string.IsNullOrEmpty(i.UrlLink))
            {
                string taobaoID;
                Match  m = RegexItemTaobaoID.Match(i.UrlLink);
                if (m.Success)
                {
                    taobaoID   = m.Groups["number"].Value;
                    i.TaobaoId = long.Parse(taobaoID);
                }
            }

            OpsItem.Upsert(i);
            #endregion
            #region build new task
            //Seller
            tp.CpResult.NewTasks.Add(new Task
            {
                Url     = RateURL.Replace("#UID#", i.SellerTaobaoId.ToString()),
                Type    = (int)TaobaoTaskType.PROVIDER_RATE,
                Context = i.SellerTaobaoId.ToString()
            });
            #endregion

            return(true);
        }
Exemplo n.º 4
0
        private void HandleCombinedList(TaskProcess tp)
        {
            string html = Encoding.Default.GetString(tp.TaskData.Bytes);

            if (!Validate(html))
            {
                tp.CpResult.Success = false;
                return;
            }

            HtmlNode root = GetRoot(html);

            if (root == null)
            {
                FailProcess(tp);
                return;
            }

            HtmlNodeCollection list = root.SelectNodes(@"//div[@id='list-content']//li[@class='list-item']");

            if (list == null)
            {
                FailProcess(tp);
                return;
            }
            bool success = true;

            foreach (HtmlNode node in list)
            {
                //only 1 shop?
                string count = HAH.SafeGetSuccessorInnerText(node, @".//div[@class='legend2']/a");
                if (string.IsNullOrEmpty(count))
                {
                    LogMissing("count", count);
                }
                else
                {
                    Match m = RegexShopCount.Match(count);
                    if (m.Success)
                    {
                        int c = Int32.Parse(m.Groups["number"].Value);
                        if (c == 1)
                        {
                            //single shop
                            success &= HandleCombinedListSingleShop(tp, node);
                        }
                        else
                        {
                            success &= HandleCombinedListMultiShops(tp, node);
                        }
                    }
                    else
                    {
                        LogMissing("count", count);
                        success = false;
                    }
                }
                if (!success)
                {
                    break;
                }
            }
            //next page
            string nextPageURL = HAH.SafeGetSuccessorAttributeStringValue(root, @".//a[@class='page-next']", "href");

            if (nextPageURL != null)
            {
                tp.CpResult.NewTasks.Add(new Task
                {
                    Url  = FixRelativeURL(nextPageURL, tp.TaskData.Task.Host),
                    Type = (int)TaobaoTaskType.COMBINED_LIST,
                });
            }


            tp.CpResult.Success = success;
        }