示例#1
0
        /***
         * This method extracts information about a product found on the OzBargain website.
         * Filtering of objects is also done here.
         * Input:
         *  - HtmlNode object.
         * Returns a BargainMessage object with the extracted inforamtion.
         */
        private BargainMessage ExtractProductInfo(HtmlNode productNode)
        {
            string name        = "";
            string price       = "";
            string externalUrl = "";
            string imageUrl    = "";
            string category    = "";

            try
            {
                var titleNodes = productNode.SelectNodes(BargainTitleXPath);
                if (titleNodes == null || titleNodes.Count == 0)
                {
                    Logger logger = Logger.GetInstance();
                    logger.realLogger.Info("TitleNodes is null.");
                    return(null);
                }

                name  = titleNodes.Count >= 1 ? titleNodes[0].InnerText : "";
                price = titleNodes.Count >= 2 ? titleNodes[1].InnerText : "";

                var rightNodes = productNode.SelectNodes(BargainExternalLinkXPath);
                if (rightNodes == null || rightNodes.Count == 0)
                {
                    Logger logger = Logger.GetInstance();
                    logger.realLogger.Info("External URL and image URL are null.");
                    return(null);
                }

                externalUrl = BaseUrl + rightNodes[0].Attributes["href"].Value;
                imageUrl    = rightNodes[0].FirstChild.Attributes["src"].Value;

                var categoryNode = productNode.SelectSingleNode(CategoryXPath);
                if (categoryNode == null)
                {
                    Logger logger = Logger.GetInstance();
                    logger.realLogger.Info("Category is null.");
                    return(null);
                }

                category = categoryNode.InnerText.ToLower();
            }

            catch (Exception e)
            {
                Logger logger = Logger.GetInstance();
                logger.realLogger.Error(e);
            }

            BargainMessage message = new BargainMessage(name, price, externalUrl, imageUrl, category);

            return(message);
        }
        public void CacheLayerTest()
        {
            Scraper scraper = new OzBargainScraper(null);

            IBotMessage message = new BargainMessage("test_title", "test_price", "test_image", "test_url", "test_category");

            scraper.AddMessage(message);
            Assert.True(scraper.GetMessages().Count == 1);

            scraper.AddMessage(message);
            Assert.Contains(message, scraper.GetMessages());
            Assert.True(scraper.GetMessages().Count == 1);
        }
示例#3
0
        public void FilterUndesirableItemTest()
        {
            IStorage      storage     = new MockStorage();
            Preferences   preferences = new Preferences(storage);
            FilterMessage filter      = new FilterMessage(preferences);

            BargainMessage message = new BargainMessage(
                "test", "900.50", "external_url", "image_url",
                "undesirable_category");

            preferences.AddCategory("test_category2");

            Assert.False(filter.IsDesirable(message));
        }
示例#4
0
        public void FilterUndesirablePriceTest()
        {
            IStorage      storage     = new MockStorage();
            Preferences   preferences = new Preferences(storage);
            FilterMessage filter      = new FilterMessage(preferences);

            BargainMessage message = new BargainMessage(
                "test", "1900.50", "external_url", "image_url",
                "test_category test_category2");

            preferences.AddCategory("test_category");
            preferences.AddPriceRange("test_category", new Tuple <double, double>(100.0, 1000.0));

            Assert.False(filter.IsDesirable(message));
        }