protected override Feed CreateFeed(XmlReader reader, string url) {
            var rssFeed = new RssFeed();
            rssFeed.Load(reader);

            var items = rssFeed.Channel.Items.OrderBy(i => GetPublishedDate(i)).Select(CreateFeedItem);

            var feed = new Feed {
                Title = rssFeed.Channel.Title,
                Url = url,
                SiteUrl = rssFeed.Channel.Link.ToString(),
                Description = rssFeed.Channel.Description,
                Items = new List<Item>(items)
            };

            var latest = items.LastOrDefault();
            if (latest != null) {
                feed.Updated = latest.Published;
            }

            return feed;
        }
        public RssSource ReadOne(string feedUrl)
        {
            var result = new RssSource { FeedUrl =  feedUrl};
            var feed =  new RssFeed();
            feed.Load(new Uri(feedUrl), null);
            feed.Channel.Items
                .ToList()
                .ForEach(x => {
                    if (x.Enclosures != null && x.Enclosures.Any(m => m.ContentType.ToLower().Contains("mp3"))) {
                        var item = new RssSourceItem {
                            Title =  x.Title,
                            Description = x.Description,
                            Link =  x.Link.ToString(),
                            Mp3 = x.Enclosures.First(mp3 => mp3.ContentType.ToLower().Contains("mp3")).Url.ToString()
                        };

                        item.PublicationDate = x.PublicationDate;
                        result.Items.Add(item);
                    }
                });

            return result;
        }
        protected override IList<Item> UpdateFeed(Feed feed, XmlReader reader) {
            var rssFeed = new RssFeed();
            rssFeed.Load(reader);

            var items = (from i in rssFeed.Channel.Items
                         let date = GetPublishedDate(i)
                         orderby date
                         where date > feed.Updated
                         select CreateFeedItem(i)).ToList();

            feed.Items.AddRange(items);

            var latest = items.LastOrDefault();
            if (latest != null) {
                feed.Updated = latest.Published;
            }

            // OPML does not set full description so check for one when updating.
            if (String.IsNullOrEmpty(feed.Description)) {
                feed.Description = rssFeed.Channel.Description;
            }

            return items;
        }
Example #4
0
        /// <summary>
        /// Creates a new <see cref="RssFeed"/> instance using the specified <see cref="Uri"/>, <see cref="ICredentials"/>, <see cref="IWebProxy"/>, and <see cref="SyndicationResourceLoadSettings"/> object.
        /// </summary>
        /// <param name="source">A <see cref="Uri"/> that represents the URL of the syndication resource XML data.</param>
        /// <param name="options">A <see cref="WebRequestOptions"/> that holds options that should be applied to web requests.</param>
        /// <param name="settings">The <see cref="SyndicationResourceLoadSettings"/> object used to configure the <see cref="RssFeed"/> instance. This value can be <b>null</b>.</param>
        /// <returns>An <see cref="RssFeed"/> object loaded using the <paramref name="source"/> data.</returns>
        /// <exception cref="ArgumentNullException">The <paramref name="source"/> is a null reference (Nothing in Visual Basic).</exception>
        /// <exception cref="FormatException">The <paramref name="source"/> data does not conform to the expected syndication content format. In this case, the feed remains empty.</exception>
        public static RssFeed Create(Uri source, WebRequestOptions options, SyndicationResourceLoadSettings settings)
        {
            //------------------------------------------------------------
            //	Local members
            //------------------------------------------------------------
            RssFeed syndicationResource = new RssFeed();

            //------------------------------------------------------------
            //	Validate parameters
            //------------------------------------------------------------
            Guard.ArgumentNotNull(source, "source");

            //------------------------------------------------------------
            //	Create new instance using supplied parameters
            //------------------------------------------------------------
            syndicationResource.Load(source, options, settings);

            return syndicationResource;
        }
        public void BasicGeocoding_LoadTest()
        {
            BasicGeocodingSyndicationExtension target = new BasicGeocodingSyndicationExtension(); // TODO: Initialize to an appropriate value
            var nt = new NameTable();
            var ns = new XmlNamespaceManager(nt);
             var xpc = new XmlParserContext(nt, ns, "US-en",XmlSpace.Default);
             var strXml = ExtensionTestUtil.GetWrappedXml(namespc, strExtXml);

            using (XmlReader reader = new XmlTextReader(strXml, XmlNodeType.Document, xpc)	)
            {
            #if false
                //var document  = new XPathDocument(reader);
                //var nav = document.CreateNavigator();
                //nav.Select("//item");
                do
                {
                    if (!reader.Read())
                        break;
                } while (reader.NodeType != XmlNodeType.EndElement || reader.Name != "webMaster");

                bool expected = true;
                bool actual;
                actual = target.Load(reader);
                Assert.AreEqual(expected, actual);
            #else
                RssFeed feed = new RssFeed();
                feed.Load(reader);
            #endif
            }
        }
        public void BasicGeocoding_FullTest()
        {
            var strXml = ExtensionTestUtil.GetWrappedXml(namespc, strExtXml);

             using (XmlReader reader = new XmlTextReader(strXml, XmlNodeType.Document, null))
             {
                 RssFeed feed = new RssFeed();
                 feed.Load(reader);

                 //				 Assert.IsTrue(feed.Channel.HasExtensions);
                 //				 Assert.IsInstanceOfType(feed.Channel.FindExtension(BasicGeocodingSyndicationExtension.MatchByType) as BasicGeocodingSyndicationExtension,
                 //						 typeof(BasicGeocodingSyndicationExtension));

                 Assert.AreEqual(1, feed.Channel.Items.Count());
                 var item = feed.Channel.Items.Single();
                 Assert.IsTrue(item.HasExtensions);
                 var itemExtension = item.FindExtension<BasicGeocodingSyndicationExtension>();
                 Assert.IsNotNull(itemExtension);
                 Assert.IsInstanceOfType(item.FindExtension(BasicGeocodingSyndicationExtension.MatchByType) as BasicGeocodingSyndicationExtension,
                  typeof(BasicGeocodingSyndicationExtension));
             }
        }
        private int ReadSource(Source source,bool? IsForTest)
        {
            var count = 0;
            var client = new WebClient();
            try
            {
                using (var stream = client.OpenRead(source.StreamUrl))
                {
                    try
                    {
                        #region RssFeed
                        RssFeed feed = new RssFeed();
                        feed.Load(stream);
                        foreach (var i in feed.Channel.Items)
                        {
                            try
                            {
                                if (ReadFeedItem(i, source,IsForTest))
                                    count++;
                                Thread.Sleep(500);
                            }

                            catch (Exception e)
                            {
                                var failed = new FailedUrl();

                                failed.Url = i.Link.ToString();
                                failed.Exception = e.Message;
                                this.Data.FailedUrls.Add(failed);
                                this.Data.SaveChanges();
                                continue;
                            }
                        }
                        #endregion RssFeed
                    }
                    catch (FormatException f)
                    {
                        #region AtomFeed
                        AtomFeed afeed = new AtomFeed();
                        using (var astream = client.OpenRead(source.StreamUrl))
                        {
                            afeed.Load(astream);
                            foreach (var i in afeed.Entries)
                            {
                                try
                                {
                                    if (ReadFeedItem(i, source,IsForTest))
                                        count++;
                                    Thread.Sleep(500);
                                }

                                catch (Exception e)
                                {
                                    var failed = new FailedUrl();

                                    failed.Url = i.Links.FirstOrDefault().ToString();
                                    failed.Exception = e.Message;
                                    this.Data.FailedUrls.Add(failed);
                                    this.Data.SaveChanges();
                                    continue;
                                }
                            }
                        }
                        #endregion AtomFeed
                    }

                    Source s = this.Data.Sources.FirstOrDefault(d => d.Id == source.Id);
                    s.LastUpdated = DateTime.Now;
                    this.Data.Sources.Update(s);
                    this.Data.SaveChanges();

                }
                //try
                //{
                //    if (count > 0)
                //        CacheManager.Clear(CacheRegions.News);
                //}
                //catch (Exception e)
                //{
                //    //TODO handle exc
                //}
            }
            catch (Exception e)
            {
                //TODO handle exc
            }

           
            Console.WriteLine(source.Name);
             return count;
        }
        /// <summary>
        /// Instantiates a <see cref="ISyndicationResource"/> that conforms to the specified <see cref="SyndicationContentFormat"/> using the supplied <see cref="Stream"/>.
        /// </summary>
        /// <param name="stream">The <see cref="Stream"/> used to load the syndication resource.</param>
        /// <param name="format">A <see cref="SyndicationContentFormat"/> enumeration value that indicates the type syndication resource the <paramref name="stream"/> represents.</param>
        /// <returns>
        ///     An <see cref="ISyndicationResource"/> object that conforms to the specified <paramref name="format"/>, initialized using the supplied <paramref name="stream"/>. 
        ///     If the <paramref name="format"/> is not supported by the provider, returns a <b>null</b> reference.
        /// </returns>
        /// <exception cref="ArgumentNullException">The <paramref name="stream"/> is a null reference (Nothing in Visual Basic).</exception>
        private static ISyndicationResource BuildResource(SyndicationContentFormat format, Stream stream)
        {
            //------------------------------------------------------------
            //	Validate parameters
            //------------------------------------------------------------
            Guard.ArgumentNotNull(stream, "stream");

            //------------------------------------------------------------
            //	Create syndication resource based on content format
            //------------------------------------------------------------
            if (format == SyndicationContentFormat.Apml)
            {
                ApmlDocument document   = new ApmlDocument();
                document.Load(stream);
                return document;
            }
            else if (format == SyndicationContentFormat.Atom)
            {
                XPathDocument document      = new XPathDocument(stream);
                XPathNavigator navigator    = document.CreateNavigator();
                navigator.MoveToRoot();
                navigator.MoveToChild(XPathNodeType.Element);

                if(String.Compare(navigator.LocalName, "entry", StringComparison.OrdinalIgnoreCase) == 0)
                {
                    AtomEntry entry     = new AtomEntry();
                    entry.Load(navigator);
                    return entry;
                }
                else if (String.Compare(navigator.LocalName, "feed", StringComparison.OrdinalIgnoreCase) == 0)
                {
                    AtomFeed feed       = new AtomFeed();
                    feed.Load(navigator);
                    return feed;
                }
                else
                {
                    return null;
                }
            }
            else if (format == SyndicationContentFormat.BlogML)
            {
                BlogMLDocument document = new BlogMLDocument();
                document.Load(stream);
                return document;
            }
            else if (format == SyndicationContentFormat.Opml)
            {
                OpmlDocument document   = new OpmlDocument();
                document.Load(stream);
                return document;
            }
            else if (format == SyndicationContentFormat.Rsd)
            {
                RsdDocument document    = new RsdDocument();
                document.Load(stream);
                return document;
            }
            else if (format == SyndicationContentFormat.Rss)
            {
                RssFeed feed            = new RssFeed();
                feed.Load(stream);
                return feed;
            }
            else
            {
                return null;
            }
        }