Пример #1
0
        public void GetItemsForFeedWithRssRdfv091()
        {
            using (var stream = Resources.Resource.GetResourceAsStream("TestFeeds.rss_rdf_0_91.xml"))
            {
                NewsFeed f = new NewsFeed();
                f.link = TEST_BASE_URL + "rss_rdf_0_91.xml";
                var feedInfo = RssParser.GetItemsForFeed(f, stream, false, true);
                Assert.IsNotNull(feedInfo);
                Assert.IsNotNull(feedInfo.ItemsList);
                Assert.AreEqual(40, feedInfo.ItemsList.Count);
                Assert.AreEqual(new DateTime(2013, 9, 12, 12, 0, 0, DateTimeKind.Utc), feedInfo.ItemsList[39].Date);

                string requestUrl = null;
                var    xElem      = RssHelper.GetOptionalElement(feedInfo.OptionalElements, "image");
                if (xElem != null)
                {
                    var urlNode = xElem.SelectSingleNode("url");
                    if (urlNode != null)
                    {
                        requestUrl = urlNode.InnerText;
                    }
                }

                Assert.AreEqual("http://www.golem.de/staticrl/images/golem-rss.png", requestUrl);
            }
        }
		protected override void Run() {
			
			discoveredFeed = new NewsFeed();

			try {
				
				//feedInfo = feedHandler.GetFeedDetails(this.feedUrl, this.credentials);
				using (Stream mem = SyncWebRequest.GetResponseStream(this.feedUrl, this.credentials, RssBanditApplication.UserAgent, this.Proxy))
				{
					NewsFeed f = new NewsFeed();
					f.link = feedUrl;
					if (RssParser.CanProcessUrl(feedUrl)) {
						feedInfo = RssParser.GetItemsForFeed(f, mem, false); 
						if (feedInfo.ItemsList != null && feedInfo.ItemsList.Count > 0)
							f.containsNewMessages = true;
					}
				}

			} catch (ThreadAbortException) {
				// eat up
			} catch (Exception e) {	// fatal errors
				p_operationException = e;
			} finally {
				WorkDone.Set();
			}
		}
        /// <summary>
        /// Implentation required for the Thread start call
        /// </summary>
        /// <example>
        /// Here is the impl. recommendation:
        /// <code>
        /// try {
        /// // long running task
        /// } catch (System.Threading.ThreadAbortException) {
        /// // eat up: op. cancelled
        /// } catch(Exception ex) {
        /// // handle them, or publish:
        /// p_operationException = ex;
        /// } finally {
        /// this.WorkDone.Set();	// signal end of operation to dismiss the dialog
        /// }
        /// </code>
        /// </example>
        protected override void Run()
        {
            RssLocater    locator = new RssLocater(proxy, RssBanditApplication.UserAgent, this.Credentials);
            List <string> arrFeedUrls;
            Dictionary <string, string[]> htFeedUrls = null;

            // can raise System.Net.WebException: The remote server returned an error: (403) Forbidden
            try {
                if (locationMethod == FeedLocationMethod.AutoDiscoverUrl)
                {
                    arrFeedUrls = locator.GetRssFeedsForUrl(webPageUrl, true);
                    htFeedUrls  = new Dictionary <string, string[]>(arrFeedUrls.Count);

                    foreach (string rssurl in arrFeedUrls)
                    {
                        NewsFeed discoveredFeed = new NewsFeed();
                        discoveredFeed.link = rssurl;
                        IFeedDetails feedInfo;
                        try {
                            // can raise System.Net.WebException: The remote server returned an error: (403) Forbidden
                            feedInfo = RssParser.GetItemsForFeed(discoveredFeed, this.GetWebResponseStream(rssurl), false);
                        } catch (Exception) {                           // fatal errors
                            feedInfo = FeedInfo.Empty;
                        }

                        htFeedUrls.Add(rssurl, new string[] { (!string.IsNullOrEmpty(feedInfo.Title) ? feedInfo.Title: SR.AutoDiscoveredDefaultTitle), feedInfo.Description, feedInfo.Link, rssurl });
                    }
                }
                else                            // Syndic8SearchType.Keyword

                {
                    htFeedUrls = locator.GetFeedsFromSyndic8(searchTerms, locationMethod);
                }
            } catch (ThreadAbortException) {
                // eat up
            } catch (System.Net.WebException wex) {
                p_operationException = wex;
                // Would it make sense, to display a credentials dialog for error (403) here?
//				MessageBox.Show(
//					SR.WebExceptionOnUrlAccess(webPageUrl, wex.Message),
//					SR.GUIAutoDiscoverFeedFailedCaption, MessageBoxButtons.OK,MessageBoxIcon.Error);
                htFeedUrls = new Dictionary <string, string[]>();
            } catch (Exception e) {             // fatal errors
                p_operationException = e;
//				MessageBox.Show(
//					SR.WebExceptionOnUrlAccess(webPageUrl, e.Message),
//					SR.GUIAutoDiscoverFeedFailedCaption, MessageBoxButtons.OK,MessageBoxIcon.Error);
                htFeedUrls = new Dictionary <string, string[]>();
            } finally {
                discoveredFeeds = htFeedUrls;
                WorkDone.Set();
            }
        }
Пример #4
0
        public void GetItemsForFeedWithRssv20NoDates()
        {
            using (var stream = Resources.Resource.GetResourceAsStream("TestFeeds.rss_2_0_no_dates.xml"))
            {
                // ensure the self-assigned item dates are all older than this:
                DateTime utcNow = DateTime.UtcNow + TimeSpan.FromMinutes(1);
                NewsFeed f      = new NewsFeed();
                f.link = TEST_BASE_URL + "rss_2_0_no_dates.xml";
                var feedInfo = RssParser.GetItemsForFeed(f, stream, false, true);
                Assert.IsNotNull(feedInfo);
                Assert.IsNotNull(feedInfo.ItemsList);
                Assert.AreEqual(10, feedInfo.ItemsList.Count);

                INewsItem recent = null;
                foreach (var item in feedInfo.ItemsList)
                {
                    // items ordered newest first:
                    Assert.IsTrue(item.Date < utcNow);
                    if (recent != null)
                    {
                        Assert.IsTrue(item.Date < recent.Date);
                    }
                    recent = item;
                }

                string optElement = null;
                var    xElem      = RssHelper.GetOptionalElement(feedInfo.OptionalElements, "ttl");
                if (xElem != null)
                {
                    optElement = xElem.InnerText;
                }
                Assert.AreEqual("60", optElement);

                optElement = null;
                xElem      = RssHelper.GetOptionalElement(feedInfo.OptionalElements, "managingEditor");
                if (xElem != null)
                {
                    optElement = xElem.InnerText;
                }
                Assert.AreEqual("*****@*****.**", optElement);

                optElement = null;
                xElem      = RssHelper.GetOptionalElement(feedInfo.OptionalElements, "copyright");
                if (xElem != null)
                {
                    optElement = xElem.InnerText;
                }
                Assert.AreEqual("Copyright 2008 Ziff Davis Media Inc. All Rights Reserved.", optElement);
            }
        }
Пример #5
0
        public void TestRecentlyViewed()
        {
            //Load feed list.
            CacheManager cache   = new FileCacheManager(CACHE_DIR, TimeSpan.MaxValue);
            RssParser    handler = new RssParser(APP_NAME, cache);

            handler.LoadFeedlist(BASE_URL + "LocalTestFeedList.xml", null);
            Assertion.Assert("Feeds should be valid!", handler.FeedsListOK);

            //Grab a feed.
            feedsFeed feed = handler.FeedsTable["http://localhost/RssHandlerTestFiles/LocalTestFeed.xml"];

            //TODO: FeedInfo feedInfo = handler.GetFeedInfo(feed.link);
            //Assertion.AssertNotNull(feedInfo);
            //cache.SaveFeed(feedInfo);

            Assertion.AssertEquals("Rss Bandit Unit Test Feed", feed.title);
            Assertion.AssertEquals(1, feed.storiesrecentlyviewed.Count);

            //Grab feed items
            ArrayList items = handler.GetItemsForFeed(feed);

            Assertion.AssertEquals(2, items.Count);

            //The first one should have been read. the second not.
            NewsItem item = (NewsItem)items[0];

            Assertion.Assert(item.BeenRead);
            item = (NewsItem)items[1];
            Assertion.Assert(!item.BeenRead);

            //So let's read the second item.
            handler.MarkAllCachedItemsAsRead(feed);
            handler.ApplyFeedModifications(feed.link);

            //Let's save this guy.
            using (FileStream newFeedStream = File.OpenWrite(Path.Combine(WEBROOT_PATH, @"RssParserTestFiles\LocalTestFeedList_NEW.xml")))
            {
                //TODO: Ask Dare about RssParser.SaveFeedList()
                //handler.SaveFeedList(newFeedStream, RssFeedListFormat.RssParser);
                newFeedStream.Close();
                Assertion.Assert(File.Exists(Path.Combine(WEBROOT_PATH, @"RssParserTestFiles\LocalTestFeedList_NEW.xml")));
            }

            //Let's reload and see what happens.
            handler = new RssParser(APP_NAME, cache);
            handler.LoadFeedlist(BASE_URL + "LocalTestFeedList_NEW.xml", null);
            feed = handler.FeedsTable["http://localhost/RssHandlerTestFiles/LocalTestFeed.xml"];
            Assertion.AssertEquals("Should be two now.", 2, feed.storiesrecentlyviewed.Count);
        }
Пример #6
0
        public void GetItemsForFeedReturnsCorrectTitle()
        {
            using (var stream = Resources.Resource.GetResourceAsStream("TestFeeds.FeedWithWeirdLineBreaks.xml"))
            {
                NewsFeed f = new NewsFeed();
                f.link = TEST_BASE_URL + "FeedWithWeirdLineBreaks.xml";
                var feedInfo = RssParser.GetItemsForFeed(f, stream, false, true);
                Assert.IsNotNull(feedInfo);

                var feedItems = feedInfo.ItemsList;
                Assert.IsNotNull(feedItems, "null is not expected");
                Assert.AreEqual(2, feedItems.Count, "2 items expected");
                var item  = feedItems[0];
                var item2 = feedItems[1];
                Assert.AreEqual("This is item 1 of a really cool(...)", item.Title, "The title was not what we expected.");
                Assert.AreEqual("Testing a second item", item2.Title);
            }
        }
Пример #7
0
        /// <summary>
        /// Returns an RSS feed.
        /// </summary>
        /// <param name="feed">The feed whose FeedInfo is required.</param>
        /// <returns>The requested feed or null if it doesn't exist</returns>
        public override IInternalFeedDetails GetFeed(INewsFeed feed)
        {
            if (null == feed || null == feed.cacheurl)
            {
                return(null);
            }

            IInternalFeedDetails fi = null;

            string cachelocation = Path.Combine(CacheLocation, feed.cacheurl);

            if (File.Exists(cachelocation))
            {
                using (Stream feedStream = FileHelper.OpenForRead(cachelocation)) {
                    fi = RssParser.GetItemsForFeed(feed, feedStream, true);
                }
                this.LoadItemContent(fi);
            }

            return(fi);
        }