Beispiel #1
0
        public void ImportOpml(Stream opmlFile, string userEmail)
        {
            var user = _dbContext.Users.Single(x => x.Email == userEmail);
            using (var textReader = new XmlTextReader(opmlFile))
            {
                const string rss = "rss";
                var xs = new XmlSerializer(typeof(Opml));
                var opmlData = (Opml)xs.Deserialize(textReader);

                foreach (var outline in opmlData.Body.Outline.Where(x => x.Type == rss))
                {
                    var xmlUrlHash = outline.XmlUrl.GetHashCode();
                    var subscription = _dbContext.Subscriptions.SingleOrDefault(x => x.Hash == xmlUrlHash);

                    if (subscription == null)
                    {
                        subscription = new Subscription
                                           {
                                               FeedUrl = outline.XmlUrl,
                                               Hash = xmlUrlHash,
                                               SiteUrl = outline.HtmlUrl,
                                               AddedBy = user,
                                               AddedOnUTC = DateTime.UtcNow,
                                               Title = outline.Title,
                                           };
                        _dbContext.Subscriptions.Add(subscription);
                    }

                    var userSubscription = new UserSubscription
                                               {
                                                   Subscription = subscription,
                                                   User = user,
                                                   AddedOnUTC = DateTime.UtcNow,
                                                   UpdatedOnUTC = DateTime.UtcNow
                                               };
                    _dbContext.UserSubscriptions.Add(userSubscription);
                }
                _dbContext.SaveChanges();
            }
        }
Beispiel #2
0
 private int GetAtomFeedItems(AtomFeed feed, Subscription subscription)
 {
     var itemsAdded = 0;
     foreach (var entry in feed.Entries.Where(x => subscription.LastFeedUpdatesUTC == null
         || x.PublishedOn.ToUniversalTime() > subscription.LastFeedUpdatesUTC))
     {
         var categories = entry.Categories.Aggregate(String.Empty, (current, category) => current + (category.Label + ",")).TrimEnd(',');
         var authors = entry.Authors.Aggregate(String.Empty, (current, author) => current + (author.Name + ",")).TrimEnd(',');
         var subscriptionPost = new SubscriptionPost
                                    {
                                        Authors = authors,
                                        Categories = categories,
                                        Content = entry.Content != null ? entry.Content.Content : (entry.Summary != null) ? entry.Summary.Content : String.Empty,
                                        OriginalUrl = GetOriginalUrl(entry),
                                        PublishDateUTC = GetGoodDateTime(entry.PublishedOn.ToUniversalTime()),
                                        Subscription = subscription,
                                        Title = entry.Title.Content
                                    };
         itemsAdded++;
         _dbContext.SubscriptionPosts.Add(subscriptionPost);
         subscription.SubscriptionPosts.Add(subscriptionPost);
     }
     return itemsAdded;
 }
Beispiel #3
0
 private int GetRssFeedItems(RssFeed feed, Subscription subscription)
 {
     var itemsAdded = 0;
     foreach (var rssItem in feed.Channel.Items.Where(x => subscription.LastFeedUpdatesUTC == null
         || x.PublicationDate.ToUniversalTime() > subscription.LastFeedUpdatesUTC))
     {
         var categories = rssItem.Categories.Aggregate(String.Empty, (current, category) => current + (category.Value + ",")).TrimEnd(',');
         var subscriptionPost = new SubscriptionPost
                                    {
                                        Categories = categories,
                                        OriginalUrl = rssItem.Link.ToString(),
                                        PublishDateUTC = GetGoodDateTime(rssItem.PublicationDate.ToUniversalTime()),
                                        Subscription = subscription,
                                        Title = rssItem.Title,
                                        Content = GetRssContent(rssItem),
                                        Authors = GetRssAuthors(rssItem)
                                    };
         itemsAdded++;
         _dbContext.SubscriptionPosts.Add(subscriptionPost);
         subscription.SubscriptionPosts.Add(subscriptionPost);
     }
     return itemsAdded;
 }
Beispiel #4
0
        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);
            }
        }