Exemplo n.º 1
0
    protected void rptrCategory_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        try
        {
            crazyTattoosEntities n = new crazyTattoosEntities();
            if (e.CommandName.Equals("del"))
            {
                int          CategoryId = int.Parse(e.CommandArgument.ToString());
                categoryInfo cid        = new categoryInfo();
                cid = n.categoryInfoes.Single(v => v.CategoryId == CategoryId);
                if (cid != null)
                {
                    n.categoryInfoes.Attach(cid);
                    n.categoryInfoes.Remove(cid);
                    n.SaveChanges();
                    ViewCategory();
                    lblmsg.Text = "*New Deleted!";
                }
            }

            // crazyTattoosEntities n = new crazyTattoosEntities();
            if (e.CommandName.Equals("delall"))
            {
                var CateIds = from b in n.categoryInfoes select b.CategoryId; // Get all news Id(s)
                foreach (var nid in CateIds)
                {
                    categoryInfo bt = new categoryInfo();
                    bt = n.categoryInfoes.Single(c => c.CategoryId == nid); // lamda expression
                    n.categoryInfoes.Attach(bt);
                    n.categoryInfoes.Remove(bt);                            // delete category
                }
                n.SaveChanges();                                            // submit changes
                ViewCategory();                                             // Bind Categories again
            }
            else if (e.CommandName.Equals("edit"))
            {
                int          categoryID = int.Parse(e.CommandArgument.ToString());
                categoryInfo data       = new CategoryInfoAction().ViewCategory(categoryID);

                if (data != null)
                {
                    hfCategoryId.Value = data.CategoryId.ToString();
                    txtCategory.Text   = data.CategoryName;

                    lnkaddTest.Text = "Update Category";
                }
            }
        }
        catch (Exception ex)
        {
            lblmsg.Text = ex.Message;
        }
    }
Exemplo n.º 2
0
    /*
     * [Function]
     * extract product best seller rank number list from product html
     * [Input]
     * amazon product url or its html
     * [Output]
     * best seller rank number list
     * [Note]
     */
    public List<productBestRank> extractProductBestSellerRankList(string productUrl = "", string productHtml = "")
    {
        List<productBestRank> bestSellerRankList = new List<productBestRank>();

        //if not give html, get it
        if (string.IsNullOrEmpty(productHtml))
        {
            productHtml = crl.getUrlRespHtml_multiTry(productUrl);
        }

        //special:
        //http://www.amazon.com/Kindle-Paperwhite-Touch-light/dp/B007OZNZG0/ref=lp_1055398_1_1?ie=UTF8&qid=1370531997&sr=1-1
        //http://www.amazon.com/Intex-Pillow-Airbed-Built--Electric/dp/B000HBILB2/ref=lp_1055398_1_24?ie=UTF8&qid=1370531997&sr=1-24
        //no best seller rank

        //http://www.amazon.com/Sony-MDR-ZX100-Series-Headphones-White/dp/B004RKQM8I/ref=lp_1055398_1_22?ie=UTF8&qid=1370527505&sr=1-22
        //<li id="SalesRank">
        //<b>Amazon Best Sellers Rank:</b> 
        //......
        //<ul class="zg_hrsr">
        //    <li class="zg_hrsr_item">
        //        <span class="zg_hrsr_rank">#3</span> 
        //        <span class="zg_hrsr_ladder">in&nbsp;<a href="http://www.amazon.com/gp/bestsellers/electronics/ref=pd_zg_hrsr_e_1_1">Electronics</a> &gt; <a href="http://www.amazon.com/gp/bestsellers/electronics/281407/ref=pd_zg_hrsr_e_1_2">Accessories & Supplies</a> &gt; <a href="http://www.amazon.com/gp/bestsellers/electronics/172532/ref=pd_zg_hrsr_e_1_3">Audio & Video Accessories</a> &gt; <b><a href="http://www.amazon.com/gp/bestsellers/electronics/172541/ref=pd_zg_hrsr_e_1_4_last">Headphones</a></b></span>
        //    </li>
        //    <li class="zg_hrsr_item">
        //        <span class="zg_hrsr_rank">#4</span> 
        //        <span class="zg_hrsr_ladder">in&nbsp;<a href="http://www.amazon.com/gp/bestsellers/electronics/ref=pd_zg_hrsr_e_2_1">Electronics</a> &gt; <a href="http://www.amazon.com/gp/bestsellers/electronics/541966/ref=pd_zg_hrsr_e_2_2">Computers & Add-Ons</a> &gt; <b><a href="http://www.amazon.com/gp/bestsellers/electronics/193870011/ref=pd_zg_hrsr_e_2_3_last">Computer Components</a></b></span>
        //    </li>
        //    <li class="zg_hrsr_item">
        //        <span class="zg_hrsr_rank">#9</span> 
        //        <span class="zg_hrsr_ladder">in&nbsp;<a href="http://www.amazon.com/gp/bestsellers/electronics/ref=pd_zg_hrsr_e_3_1">Electronics</a> &gt; <a href="http://www.amazon.com/gp/bestsellers/electronics/172623/ref=pd_zg_hrsr_e_3_2">Portable Audio & Video</a> &gt; <a href="http://www.amazon.com/gp/bestsellers/electronics/172630/ref=pd_zg_hrsr_e_3_3">MP3 Players & Accessories</a> &gt; <b><a href="http://www.amazon.com/gp/bestsellers/electronics/290438/ref=pd_zg_hrsr_e_3_4_last">MP3 Player Accessories</a></b></span>
        //    </li>
        //</ul>
        //</li>

        //http://www.amazon.com/Maytag-UKF8001-Refrigerator-Filter-1-Pack/dp/B001XW8KW4/ref=lp_1055398_1_9?ie=UTF8&qid=1370531997&sr=1-9
        //<tr id="SalesRank">
        //    <td class="label">Best Sellers Rank</td>
        //    <td class="value">
        //        #3 in Home Improvement (<a href="http://www.amazon.com/gp/bestsellers/hi/ref=pd_dp_ts_hi_1">See top 100</a>)
        //        <ul class="zg_hrsr">
        //            <li class="zg_hrsr_item">
        //            <span class="zg_hrsr_rank">#1</span> 
        //            <span class="zg_hrsr_ladder">in&nbsp;<a href="http://www.amazon.com/gp/bestsellers/hi/ref=pd_zg_hrsr_hi_1_1">Home Improvement</a> &gt; <a href="http://www.amazon.com/gp/bestsellers/hi/3754161/ref=pd_zg_hrsr_hi_1_2">Kitchen & Bath Fixtures</a> &gt; <a href="http://www.amazon.com/gp/bestsellers/hi/13397631/ref=pd_zg_hrsr_hi_1_3">Water Filtration & Softeners</a> &gt; <a href="http://www.amazon.com/gp/bestsellers/hi/680337011/ref=pd_zg_hrsr_hi_1_4_last">Faucet Water Filters</a></span>
        //            </li>
        //            <li class="zg_hrsr_item">
        //            <span class="zg_hrsr_rank">#2</span> 
        //            <span class="zg_hrsr_ladder">in&nbsp;<a href="http://www.amazon.com/gp/bestsellers/hi/ref=pd_zg_hrsr_hi_2_1">Home Improvement</a> &gt; <a href="http://www.amazon.com/gp/bestsellers/hi/13397451/ref=pd_zg_hrsr_hi_2_2">Appliances</a> &gt; <a href="http://www.amazon.com/gp/bestsellers/hi/3741181/ref=pd_zg_hrsr_hi_2_3">Large Appliance Accessories</a> &gt; <a href="http://www.amazon.com/gp/bestsellers/hi/3741241/ref=pd_zg_hrsr_hi_2_4_last">Refrigerator Parts & Accessories</a></span>
        //            </li>
        //        </ul>
        //    </td>
        //</tr>


        HtmlAgilityPack.HtmlDocument htmlDoc = crl.htmlToHtmlDoc(productHtml);
        //HtmlNode salesRankNode = htmlDoc.DocumentNode.SelectSingleNode("//li[@id='SalesRank']");
        //HtmlNode salesRankNode = htmlDoc.DocumentNode.SelectSingleNode("//(li|tr)[@id='SalesRank']");
        HtmlNode salesRankNode = htmlDoc.DocumentNode.SelectSingleNode("//*[self::li|self::tr][@id='SalesRank']");
        if (salesRankNode != null)
        {
            //check if exist: <td class="value">
            //http://www.amazon.com/Maytag-UKF8001-Refrigerator-Filter-1-Pack/dp/B001XW8KW4/ref=lp_1055398_1_9?ie=UTF8&qid=1370531997&sr=1-9
            //http://www.amazon.com/Thermos-Insulated-18-Ounce-Stainless-Steel-Hydration/dp/B000FJ9DOK/ref=lp_1055398_1_6?ie=UTF8&qid=1370574186&sr=1-6
            HtmlNode tdClassValueNode = salesRankNode.SelectSingleNode("./td[@class='value']");
            if (tdClassValueNode != null)
            {
                //check whether match following
                //#3 in Home Improvement (<a href="http://www.amazon.com/gp/bestsellers/hi/ref=pd_dp_ts_hi_1">See top 100</a>)
                string tdClassValueHtml = tdClassValueNode.InnerHtml;//"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n#3 in Home Improvement (<a href=\"http://www.amazon.com/gp/bestsellers/hi/ref=pd_dp_ts_hi_1\">See top 100</a>)\n  \n\n\n\n\n\n\n \n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n<ul class=\"zg_hrsr\">\n    <li class=\"zg_hrsr_item\">\n    <span class=\"zg_hrsr_rank\">#1</span> \n    <span class=\"zg_hrsr_ladder\">in&nbsp;<a href=\"http://www.amazon.com/gp/bestsellers/hi/ref=pd_zg_hrsr_hi_1_1\">Home Improvement</a> &gt; <a href=\"http://www.amazon.com/gp/bestsellers/hi/3754161/ref=pd_zg_hrsr_hi_1_2\">Kitchen & Bath Fixtures</a> &gt; <a href=\"http://www.amazon.com/gp/bestsellers/hi/13397631/ref=pd_zg_hrsr_hi_1_3\">Water Filtration & Softeners</a> &gt; <a href=\"http://www.amazon.com/gp/bestsellers/hi/680337011/ref=pd_zg_hrsr_hi_1_4_last\">Faucet Water Filters</a></span>\n    </li>\n    <li class=\"zg_hrsr_item\">\n    <span class=\"zg_hrsr_rank\">#2</span> \n    <span class=\"zg_hrsr_ladder\">in&nbsp;<a href=\"http://www.amazon.com/gp/bestsellers/hi/ref=pd_zg_hrsr_hi_2_1\">Home Improvement</a> &gt; <a href=\"http://www.amazon.com/gp/bestsellers/hi/13397451/ref=pd_zg_hrsr_hi_2_2\">Appliances</a> &gt; <a href=\"http://www.amazon.com/gp/bestsellers/hi/3741181/ref=pd_zg_hrsr_hi_2_3\">Large Appliance Accessories</a> &gt; <a href=\"http://www.amazon.com/gp/bestsellers/hi/3741241/ref=pd_zg_hrsr_hi_2_4_last\">Refrigerator Parts & Accessories</a></span>\n    </li>\n</ul>\n\n\n\n\n"
                //Regex firstRankItemRx = new Regex(@"#(?<rankNumStr>\d+)\sin\s(?<categoryStr>.+?)\s\(<a\shref=""[^""]+?"">.+?</a>\)");
                //#1,663 in Home Improvement (<a href=\"http://www.amazon.com/gp/bestsellers/hi/ref=pd_dp_ts_hi_1\">See top 100</a>)
                Regex firstRankItemRx = new Regex(@"#(?<rankNumStr>[\d,]+)\sin\s(?<categoryStr>.+?)\s\(<a\shref=""[^""]+?"">.+?</a>\)");
                Match foundFirstRankItem = firstRankItemRx.Match(tdClassValueHtml);
                if (foundFirstRankItem.Success)
                {
                    string rankNumStr = foundFirstRankItem.Groups["rankNumStr"].Value; //1,663
                    string categoryStr = foundFirstRankItem.Groups["categoryStr"].Value;

                    productBestRank bestRankItem = new productBestRank();
                    rankNumStr = rankNumStr.Replace(",", ""); //1663
                    bestRankItem.rankNumber = Int32.Parse(rankNumStr);
                    bestRankItem.categoryList = new List<categoryInfo>();

                    categoryInfo curCategoryInfo = new categoryInfo();
                    curCategoryInfo.name = HttpUtility.HtmlDecode(categoryStr);
                    curCategoryInfo.link = "";
                    bestRankItem.categoryList.Add(curCategoryInfo);

                    bestSellerRankList.Add(bestRankItem);
                }
                else
                {
                    //something wrong
                    gLogger.Debug("can not find foundFirstRankItem");
                }
            }

            HtmlNodeCollection hrsrItemNodeList = salesRankNode.SelectNodes(".//ul[@class='zg_hrsr']/li[@class='zg_hrsr_item']");
            if ((hrsrItemNodeList != null) && (hrsrItemNodeList.Count > 0))
            {
                foreach (HtmlNode hrsrItemNode in hrsrItemNodeList)
                {
                    productBestRank bestRankItem = new productBestRank();
                    bestRankItem.rankNumber = 0;
                    bestRankItem.categoryList = new List<categoryInfo>();

                    HtmlNode rankNode = hrsrItemNode.SelectSingleNode(".//span[@class='zg_hrsr_rank']");
                    if (rankNode != null)
                    {
                        string rankNumStr = rankNode.InnerText; //#3
                        rankNumStr = rankNumStr.Replace("#", "");
                        int rankNumInt = Int32.Parse(rankNumStr);
                        bestRankItem.rankNumber = rankNumInt;
                    }
                    else
                    {
                        //something wrong
                    }

                    HtmlNode ladderNode = hrsrItemNode.SelectSingleNode(".//span[@class='zg_hrsr_ladder']");
                    if (ladderNode != null)
                    {
                        HtmlNodeCollection categoryNodeList = ladderNode.SelectNodes(".//a[@href]");
                        if ((categoryNodeList != null) && (categoryNodeList.Count > 0))
                        {
                            foreach (HtmlNode categoryNode in categoryNodeList)
                            {
                                categoryInfo curCategoryInfo = new categoryInfo();
                                curCategoryInfo.link = HttpUtility.HtmlDecode(categoryNode.Attributes["href"].Value);
                                curCategoryInfo.name = HttpUtility.HtmlDecode(categoryNode.InnerText);
                                bestRankItem.categoryList.Add(curCategoryInfo);
                            }
                        }
                        else
                        {
                            //something wrong
                        }
                    }
                    else
                    {
                        //something wrong
                    }

                    bestSellerRankList.Add(bestRankItem);
                }
            }
        }
        else
        {
            //something wrong
        }

        return bestSellerRankList;
    }