public async Task <IEnumerable <FeedItem> > UpdateFeedAsync(Feed feed) { var list = new List <FeedItem>(); switch (feed.FeedType) { case FeedType.Rss: var rss = RssFeed.Create(new Uri(feed.Address)); foreach (var item in rss.Channel.Items) { list.Add(new FeedItem(item)); } break; case FeedType.Atom: var atom = AtomFeed.Create(new Uri(feed.Address)); foreach (var entry in atom.Entries) { list.Add(new FeedItem(entry)); } break; default: var unknown = GenericSyndicationFeed.Create(new Uri(feed.Address)); foreach (var item in unknown.Items) { list.Add(new FeedItem(item)); } break; } return(list); }
private Subscription AddSubscription(string feedUrl, User user) { try { var uri = new Uri(feedUrl); var feed = GenericSyndicationFeed.Create(uri, new SyndicationResourceLoadSettings { AutoDetectExtensions = true, Timeout = new TimeSpan(0, 0, 3) }); var feedHashCode = feedUrl.GetHashCode(); var subscription = new Subscription { AddedOnUTC = DateTime.UtcNow, AddedBy = user, FeedUrl = feedUrl, Hash = feedHashCode, Title = feed.Title, }; switch (feed.Format) { case SyndicationContentFormat.Atom: var atom = (AtomFeed)feed.Resource; subscription.Title = GetAtomSubscriptionTitle(atom); GetAtomFeedItems(atom, subscription); break; case SyndicationContentFormat.Rss: var rss = (RssFeed)feed.Resource; subscription.SiteUrl = rss.Channel.Link.ToString(); GetRssFeedItems(rss, subscription); break; case SyndicationContentFormat.None: throw new BralekTechnicalException("NO_RSS_FORMAT", null); default: throw new BralekTechnicalException("RSS_UNKNOWN_FORMAT", null); } _dbContext.Subscriptions.Add(subscription); return(subscription); } catch (UriFormatException ex) { throw new BralekTechnicalException("ADD_FEED_URI_ERROR", ex); } catch (WebException ex) { throw new BralekTechnicalException("FEED_READ_ERROR", ex); } catch (XmlException ex) { throw new BralekTechnicalException("FEED_XML_ERROR", ex); } }
public void UpdateFeeds() { var subscriptions = _dbContext.Subscriptions; foreach (var subscription in subscriptions) { try { var feed = GenericSyndicationFeed.Create(new Uri(subscription.FeedUrl)); var itemsAdded = 0; switch (feed.Format) { case SyndicationContentFormat.Atom: itemsAdded = GetAtomFeedItems((AtomFeed)feed.Resource, subscription); break; case SyndicationContentFormat.Rss: itemsAdded = GetRssFeedItems((RssFeed)feed.Resource, subscription); break; case SyndicationContentFormat.None: subscription.IsWorking = false; subscription.LastError = "Feed url is not a recognized format"; break; default: throw new Exception(String.Format("Format {0}, not supported for feed items", feed.Format)); } foreach (var userSubscription in subscription.UserSubscriptions) { userSubscription.UnreadItems += itemsAdded; } subscription.IsWorking = true; subscription.LastFeedUpdatesUTC = DateTime.UtcNow; } catch (WebException webException) { subscription.IsWorking = false; subscription.LastError = String.Format("WebException {0}", webException.Message); } catch (XmlException xmlException) { subscription.IsWorking = false; subscription.LastError = String.Format("XmlException {0}", xmlException.Message); } } _dbContext.SaveChanges(); }
public Feed GetFeed(Feed feed) { switch (feed.FeedType) { case FeedType.Rss: var rss = RssFeed.Create(new Uri(feed.Address)); return(new Feed(rss)); case FeedType.Atom: var atom = AtomFeed.Create(new Uri(feed.Address)); return(new Feed(atom)); default: var unknown = GenericSyndicationFeed.Create(new Uri(feed.Address)); return(new Feed(unknown)); } }
/// <summary> /// Provides example code for the GenericSyndicationFeed.Create(Uri) method /// </summary> public static void CreateExample() { GenericSyndicationFeed feed = GenericSyndicationFeed.Create(new Uri("http://feeds.feedburner.com/OppositionallyDefiant")); foreach (GenericSyndicationItem item in feed.Items) { if (item.PublishedOn > DateTime.Now.Subtract(new TimeSpan(7, 0, 0, 0))) { // Process generic item's published in the last week } foreach (GenericSyndicationCategory category in item.Categories) { if (String.Compare(category.Term, "WCF", StringComparison.OrdinalIgnoreCase) == 0) { // Process item category } } } }
/// <summary> /// /// </summary> /// <param name="feedUrl"></param> /// <param name="numberOfItems"></param> /// <returns></returns> public ContenidoRssFeed ReadFeed(string feedUrl, int numberOfItems) { var settings = new SyndicationResourceLoadSettings { RetrievalLimit = numberOfItems }; var feed = GenericSyndicationFeed.Create(new Uri(feedUrl), settings); if (feed.Resource is RssFeed) { return(ReadRssFeed((RssFeed)feed.Resource)); } if (feed.Resource is AtomFeed) { return(ReadAtomFeed((AtomFeed)feed.Resource)); } throw new InvalidOperationException("Error Feed no soportado"); }
//============================================================ // CLASS SUMMARY //============================================================ /// <summary> /// Provides example code for the GenericSyndicationFeed class. /// </summary> public static void ClassExample() { #region GenericSyndicationFeed GenericSyndicationFeed feed = GenericSyndicationFeed.Create(new Uri("http://feeds.feedburner.com/OppositionallyDefiant")); foreach (GenericSyndicationCategory category in feed.Categories) { if (String.Compare(category.Term, ".NET", StringComparison.OrdinalIgnoreCase) == 0) { // Process feed category } } // Enumerate through syndicated content foreach (GenericSyndicationItem item in feed.Items) { if (item.PublishedOn > DateTime.Now.Subtract(new TimeSpan(7, 0, 0, 0))) { // Process generic item's published in the last week } foreach (GenericSyndicationCategory category in item.Categories) { if (String.Compare(category.Term, "WCF", StringComparison.OrdinalIgnoreCase) == 0) { // Process item category } } } if (feed.Format == SyndicationContentFormat.Rss) { RssFeed rssFeed = feed.Resource as RssFeed; if (rssFeed != null) { // Process RSS format specific information } } #endregion }
public static DataTable GetRssFeedEntries( int moduleId, Guid moduleGuid, int entryCacheTimeout, int maxDaysOld, int maxEntriesPerFeed, bool enableSelectivePublishing) { DateTime cutoffDate = DateTime.UtcNow.AddDays(-maxDaysOld); DateTime cacheExpiration = DateTime.UtcNow.AddMinutes(-entryCacheTimeout); DateTime lastCacheTime = mojoPortal.Business.RssFeed.GetLastCacheTime(moduleGuid); if (lastCacheTime > cacheExpiration) { // data "cached" in the db has not expired so just return it return(mojoPortal.Business.RssFeed.GetEntries(moduleGuid)); } try { if (FeedManagerConfiguration.UseReadWriteLockForCacheMenagement) { cacheLock.AcquireWriterLock(cacheLockTimeoutInMilliseconds); } //if (debugLog) { log.Debug("got lock in GetRssFeeds"); } lastCacheTime = mojoPortal.Business.RssFeed.GetLastCacheTime(moduleGuid); if (lastCacheTime > cacheExpiration) { // data "cached" in the db has not expired so just return it return(mojoPortal.Business.RssFeed.GetEntries(moduleGuid)); } if (enableSelectivePublishing) { mojoPortal.Business.RssFeed.DeleteExpiredEntriesByModule(moduleGuid, cutoffDate); mojoPortal.Business.RssFeed.DeleteUnPublishedEntriesByModule(moduleGuid); } else { mojoPortal.Business.RssFeed.DeleteEntriesByModule(moduleGuid); } DataTable dtFeeds = mojoPortal.Business.RssFeed.GetFeeds(moduleId); string siteRoot = SiteUtils.GetNavigationSiteRoot(); string secureSiteRoot = SiteUtils.GetSecureNavigationSiteRoot(); foreach (DataRow dr in dtFeeds.Rows) { Guid feedGuid = new Guid(dr["ItemGuid"].ToString()); int feedId = Convert.ToInt32(dr["ItemID"]); string feedUrl = dr["RssUrl"].ToString(); if (feedUrl.StartsWith("~/")) { feedUrl = WebUtils.ResolveServerUrl(feedUrl).Replace("https:", "http:"); } bool publishByDefault = Convert.ToBoolean(dr["PublishByDefault"]); int countOfPreservedEntries = Convert.ToInt32(dr["TotalEntries"]); bool publish = true; if (enableSelectivePublishing) { if (!publishByDefault) { publish = false; } } int entriesAdded = countOfPreservedEntries; try { GenericSyndicationFeed gsFeed = GenericSyndicationFeed.Create(new Uri(FormatFeedUrl(feedUrl, siteRoot, secureSiteRoot))); #region RSSFeed_management if (gsFeed.Format == SyndicationContentFormat.Rss) { Argotic.Syndication.RssFeed rssFeed = gsFeed.Resource as Argotic.Syndication.RssFeed; if (rssFeed != null) { foreach (Argotic.Syndication.RssItem rssItem in rssFeed.Channel.Items) { DateTime itemPubDate = EnsureDate(rssItem.PublicationDate); if ((itemPubDate >= cutoffDate) || (maxDaysOld == 0)) { if ((entriesAdded < maxEntriesPerFeed) || (maxEntriesPerFeed == 0)) { string entryBlob = rssItem.Title + rssItem.Link.ToString(); int entryHash = GetEntryHash(entryBlob); string channelLink = string.Empty; if ((rssFeed.Channel != null) && (rssFeed.Channel.Link != null)) { channelLink = rssFeed.Channel.Link.ToString(); } if (UpdateEntry( moduleGuid, itemPubDate, rssItem.Title, rssItem.Author, channelLink, rssItem.Description, rssItem.Link.ToString(), entryHash, feedGuid, feedId, publish) > 0) { entriesAdded += 1; } } } } } } #endregion #region ATOMFeed_management if (gsFeed.Format == SyndicationContentFormat.Atom) { Argotic.Syndication.AtomFeed atomFeed = gsFeed.Resource as Argotic.Syndication.AtomFeed; foreach (AtomEntry atItem in atomFeed.Entries) { string entryLink = string.Empty; StringBuilder entryAuthor = new StringBuilder(); string comma = string.Empty; DateTime itemPubDate = EnsureDate(atItem.UpdatedOn); if ((itemPubDate >= cutoffDate) || (maxDaysOld == 0)) { if ((entriesAdded < maxEntriesPerFeed) || (maxEntriesPerFeed == 0)) { foreach (AtomPersonConstruct atPerson in atItem.Authors) { entryAuthor.Append(comma + atPerson.Name); comma = ","; } if (entryAuthor.Length == 0) { foreach (AtomPersonConstruct atPerson in atomFeed.Authors) { entryAuthor.Append(comma + atPerson.Name); comma = ","; } } foreach (AtomLink atLink in atItem.Links) { if (atLink.Relation == "alternate") { entryLink = atLink.Uri.ToString(); } } if ((entryLink.Length == 0) && (atItem.Links.Count > 0)) { entryLink = atItem.Links[0].Uri.ToString(); } string content = string.Empty; if (atItem.Content == null) { if (atItem.Summary != null) { content = atItem.Summary.Content; } } else { content = atItem.Content.Content; } // commented out 2010-02-27 some feeds have only a title and link //if (content.Length == 0) { continue; } string entryBlob = atItem.Title.Content + entryLink; int entryHash = GetEntryHash(entryBlob); if (UpdateEntry( moduleGuid, itemPubDate, atItem.Title.Content, entryAuthor.ToString(), feedUrl, content, entryLink, entryHash, feedGuid, feedId, publish) > 0) { entriesAdded += 1; } } } } } #endregion } catch (WebException ex) { if (log.IsErrorEnabled) { string logMsg = String.Format("There was a problem trying to read the feed for url {0}. Ignoring.", (string)dr["RssUrl"]); log.Error(logMsg, ex); } } catch (UriFormatException ex) { if (log.IsErrorEnabled) { string logMsg = String.Format("There was a problem trying to read the feed for url {0}. Ignoring.", (string)dr["RssUrl"]); log.Error(logMsg, ex); } } catch (System.Net.Sockets.SocketException ex) { if (log.IsErrorEnabled) { string logMsg = String.Format("There was a problem trying to read the feed for url {0}. Ignoring.", (string)dr["RssUrl"]); log.Error(logMsg, ex); } } catch (System.Xml.XmlException ex) { if (log.IsErrorEnabled) { string logMsg = String.Format("There was a problem trying to read the feed for url {0}. Ignoring.", (string)dr["RssUrl"]); log.Error(logMsg, ex); } } catch (System.Security.SecurityException ex) { log.Error("Could not load feed due to security exception. Must be running in restricted trust level. Creating server side web requests is not allowed in current configuration.", ex); } catch (ArgumentNullException ex) { string logMsg = String.Format("There was a problem trying to read the feed for url {0}. Ignoring.", (string)dr["RssUrl"]); log.Error(logMsg, ex); } catch (System.Data.Common.DbException ex) { log.Error("Error updating feed database cache", ex); } } } finally { if (FeedManagerConfiguration.UseReadWriteLockForCacheMenagement) { try { cacheLock.ReleaseWriterLock(); } catch (ApplicationException ex) { log.Error("swallowed error", ex); } } } return(mojoPortal.Business.RssFeed.GetEntries(moduleGuid)); }
public static void RefreshFeed( mojoPortal.Business.RssFeed feedInfo, int moduleId, Guid moduleGuid, int maxDaysOld, int maxEntriesPerFeed, bool enableSelectivePublishing) { if (feedInfo == null) { return; } try { if (FeedManagerConfiguration.UseReadWriteLockForCacheMenagement) { cacheLock.AcquireWriterLock(cacheLockTimeoutInMilliseconds); } DateTime cutoffDate = DateTime.Now.AddDays(-maxDaysOld); mojoPortal.Business.RssFeed.DeleteExpiredEntriesByModule(moduleGuid, cutoffDate); mojoPortal.Business.RssFeed.DeleteUnPublishedEntriesByFeed(feedInfo.ItemId); int entriesAdded = 0; string siteRoot = SiteUtils.GetNavigationSiteRoot(); string secureSiteRoot = SiteUtils.GetSecureNavigationSiteRoot(); bool publish = true; if (enableSelectivePublishing) { if (!feedInfo.PublishByDefault) { publish = false; } } string feedUrl = feedInfo.RssUrl; if (feedUrl.StartsWith("~/")) { feedUrl = WebUtils.ResolveServerUrl(feedUrl).Replace("https:", "http:"); } try { GenericSyndicationFeed gsFeed = GenericSyndicationFeed.Create(new Uri(FormatFeedUrl(feedUrl, siteRoot, secureSiteRoot))); #region RSSFeed_management if (gsFeed.Format == SyndicationContentFormat.Rss) { Argotic.Syndication.RssFeed rssFeed = gsFeed.Resource as Argotic.Syndication.RssFeed; if (rssFeed != null) { foreach (Argotic.Syndication.RssItem rssItem in rssFeed.Channel.Items) { if ((rssItem.PublicationDate >= cutoffDate) || (maxDaysOld == 0)) { if ((entriesAdded < maxEntriesPerFeed) || (maxEntriesPerFeed == 0)) { string entryBlob = rssItem.Title + rssItem.Link.ToString(); int entryHash = GetEntryHash(entryBlob); if (UpdateEntry( moduleGuid, EnsureDate(rssItem.PublicationDate), rssItem.Title, rssItem.Author, feedInfo.RssUrl, rssItem.Description, rssItem.Link.ToString(), entryHash, feedInfo.ItemGuid, feedInfo.ItemId, publish) > 0) { entriesAdded += 1; } } } } } } #endregion #region ATOMFeed_management if (gsFeed.Format == SyndicationContentFormat.Atom) { Argotic.Syndication.AtomFeed atomFeed = gsFeed.Resource as Argotic.Syndication.AtomFeed; foreach (AtomEntry atItem in atomFeed.Entries) { if ((atItem.PublishedOn >= cutoffDate) || (maxDaysOld == 0)) { if ((entriesAdded < maxEntriesPerFeed) || (maxEntriesPerFeed == 0)) { string entryLink = string.Empty; StringBuilder entryAuthor = new StringBuilder(); string comma = string.Empty; foreach (AtomPersonConstruct atPerson in atItem.Authors) { entryAuthor.Append(comma + atPerson.Name); comma = ","; } if (entryAuthor.Length == 0) { foreach (AtomPersonConstruct atPerson in atomFeed.Authors) { entryAuthor.Append(comma + atPerson.Name); comma = ","; } } foreach (AtomLink atLink in atItem.Links) { if (atLink.Relation == "alternate") { entryLink = atLink.Uri.ToString(); } } if ((entryLink.Length == 0) && (atItem.Links.Count > 0)) { entryLink = atItem.Links[0].Uri.ToString(); } string content = string.Empty; if (atItem.Content == null) { if (atItem.Summary != null) { content = atItem.Summary.Content; } } else { content = atItem.Content.Content; } if (content.Length == 0) { continue; } string entryBlob = atItem.Title.ToString() + entryLink; int entryHash = GetEntryHash(entryBlob); if (UpdateEntry( moduleGuid, EnsureDate(atItem.PublishedOn), atItem.Title.ToString(), entryAuthor.ToString(), feedInfo.RssUrl, content, entryLink, entryHash, feedInfo.ItemGuid, feedInfo.ItemId, publish) > 0) { entriesAdded += 1; } } } } } #endregion } catch (WebException ex) { if (log.IsErrorEnabled) { string logMsg = String.Format("There was a problem trying to read the feed for url {0}. Ignoring.", feedInfo.RssUrl); log.Error(logMsg, ex); } } catch (UriFormatException ex) { if (log.IsErrorEnabled) { string logMsg = String.Format("There was a problem trying to read the feed for url {0}. Ignoring.", feedInfo.RssUrl); log.Error(logMsg, ex); } } catch (System.Net.Sockets.SocketException ex) { if (log.IsErrorEnabled) { string logMsg = String.Format("There was a problem trying to read the feed for url {0}. Ignoring.", feedInfo.RssUrl); log.Error(logMsg, ex); } } catch (System.Xml.XmlException ex) { if (log.IsErrorEnabled) { string logMsg = String.Format("There was a problem trying to read the feed for url {0}. Ignoring.", feedInfo.RssUrl); log.Error(logMsg, ex); } } catch (System.Security.SecurityException ex) { log.Error("Could not load feed due to security exception. Must be running in restricted trust level. Creating server side web requests is not allowed in current configuration.", ex); } catch (System.Data.Common.DbException ex) { log.Error("Error updating feed database cache", ex); } } finally { if (FeedManagerConfiguration.UseReadWriteLockForCacheMenagement) { try { cacheLock.ReleaseWriterLock(); } catch (ApplicationException ex) { log.Error("swallowed error", ex); } } } }