示例#1
0
 public FeedItem(Models.FeedItem feedItem, Models.Feed feed)
 {
     Id  = feedItem.Id;
     Uri = new List <string> {
         feedItem.Uri
     };
     PublishTimeInUtc = new List <DateTime> {
         feedItem.PublishTimeInUtc
     };
     Summary = new List <string> {
         Utils.RemoveHtmlTag(feedItem.Summary)
     };
     Content = new List <string> {
         Utils.RemoveHtmlTag(feedItem.Content)
     };
     Title = new List <string> {
         Utils.RemoveHtmlTag(feedItem.Title)
     };
     TopicPictureUri = new List <string> {
         feedItem.TopicPictureUri
     };
     FeedId = new List <string> {
         feed.Id
     };
     FeedName = new List <string> {
         feed.Name
     };
     FeedCategory = new List <string> {
         feed.Category
     };
     FeedIconUri = new List <string> {
         feed.IconUri
     };
 }
        public ActionResult Index()
        {
            // Create new stopwatch.
            //Stopwatch stopwatch = new Stopwatch();

            // Begin timing.
            //stopwatch.Start();

            Log.Logger = new LoggerConfiguration()
                         .Enrich.FromLogContext()
                         .WriteTo.LiterateConsole()
                         .CreateLogger();

            ILoggerFactory loggerFactory = new LoggerFactory().AddSerilog();
            var            logger        = loggerFactory.CreateLogger <HomeController>();

            var configPath = Server.MapPath(CONFIG_PATH);

            var settings = new Data.Settings();

            settings.Load(configPath);

            var feed  = new Models.Feed(settings, logger);
            var model = feed.GetItems();


            //var output = result.GetResult().ToList();
            // Stop timing.
            //stopwatch.Stop();

            return(View(model));
        }
示例#3
0
        public async Task RemoveFeedAsync(string nameOrUrl, ulong guildID, ulong channelID)
        {
            Models.Feed feed = await dbContext.Feeds.AsQueryable().SingleOrDefaultAsync(f => f.Name == nameOrUrl || (f.URL == nameOrUrl && f.GuildID == guildID && f.ChannelID == channelID)).ConfigureAwait(false);

            if (feed != null)
            {
                _ = dbContext.Feeds.Remove(feed);
                _ = await dbContext.SaveChangesAsync().ConfigureAwait(false);
            }
        }
示例#4
0
        public async Task RemoveFeedAsync(string nameOrUrl, ulong guildID)
        {
            var feeds = await _dbContext.Feeds.ToListAsync();

            Models.Feed feed = feeds.SingleOrDefault(f => f.Name.ToLowerInvariant() == nameOrUrl.ToLowerInvariant() || (f.URL.ToLowerInvariant() == nameOrUrl.ToLowerInvariant() && f.GuildID == guildID));
            if (feed != null)
            {
                _dbContext.Feeds.Remove(feed);
                await _dbContext.SaveChangesAsync();
            }
        }
示例#5
0
        public void GivenSuccessfullyProcessedFeedThenShouldPersistFeed()
        {
            // Arrange
            var feed = new Models.Feed();
            A.CallTo(() => _feedProcessor.CanProcess(A<string>.Ignored)).Returns(true);
            A.CallTo(() => _feedProcessor.Process(A<string>.Ignored)).Returns(feed);

            // Act
            var actual = _subject.Ingest("SOURCE");

            // Assert
            A.CallTo(() => _command.Execute(A<PersistFeedRequest>.That.Matches(r => r.Feed == feed))).MustHaveHappenedOnceExactly();
        }
示例#6
0
        public async Task AddFeedAsync(string name, string url, ulong guildID, ulong channelID)
        {
            var feed = new Models.Feed
            {
                Name      = name,
                URL       = url,
                GuildID   = guildID,
                ChannelID = channelID
            };

            _ = dbContext.Feeds.Add(feed);
            _ = await dbContext.SaveChangesAsync().ConfigureAwait(false);
            await GetFeedUpdateAsync(feed, true).ConfigureAwait(false);
        }
示例#7
0
        public async Task AddFeed(string uri)
        {
            var result = await FeedReader.ReadAsync(uri);

            var newFeed = new Models.Feed
            {
                Uri         = uri,
                Title       = result.Title,
                Added       = DateTime.Now,
                LastChecked = DateTime.Now
            };

            await AddFeed(newFeed);
            await GetNewItems(newFeed, result.Items);
        }
示例#8
0
        private async Task GetNewItems(Models.Feed feed, IEnumerable <FeedItem> items)
        {
            var newItems = new List <Item>();

            foreach (var item in items)
            {
                if (_itemsService.ItemExists(item.Link))
                {
                    // Item already exists from previous fetch or another feed
                    continue;
                }

                newItems.Add(new Item
                {
                    Feed      = feed,
                    Published = item.PublishingDate,
                    Title     = item.Title,
                    Uri       = item.Link
                });
            }
            await _itemsService.AddItems(newItems);
        }
示例#9
0
        private async Task GetFeedUpdateAsync(Models.Feed guildFeed, bool getLatest = false)
        {
            SocketGuild       guild   = discordClient.GetGuild(guildFeed.GuildID);
            SocketTextChannel channel = guild?.GetTextChannel(guildFeed.ChannelID);

            if (guild == null || channel == null || guildFeed.URL.IsEmpty())
            {
                return;
            }
            Feed feed;

            try
            {
                feed = await FeedReader.ReadAsync(guildFeed.URL).ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                logger.LogWarning(ex, $"Error getting feeds for {guildFeed.URL} in {guild.Name}:{channel.Name}");
                return;
            }
            if (feed == null || feed.Items == null || feed.Items.Count < 1)
            {
                return;
            }

            DateTime lastUpdateUTC              = guildFeed.LastUpdate ?? DateTime.UtcNow.Subtract(TimeSpan.FromMinutes(updateIntervallMinutes));
            IEnumerable <FeedItem> allFeeds     = feed?.Items?.Where(x => x.PublishingDate.HasValue);
            List <FeedItem>        updatedFeeds = allFeeds?.Where(x => x.PublishingDate.Value.ToUniversalTime() > lastUpdateUTC)
                                                  .OrderBy(x => x.PublishingDate)
                                                  .ToList();

            if (updatedFeeds != null && updatedFeeds.Count == 0 && getLatest)
            {
                updatedFeeds = allFeeds.Take(1).ToList();
            }
            if (updatedFeeds != null && updatedFeeds.Count > 0)
            {
                var builder = new EmbedBuilder();
                _ = builder.WithColor(new Color(21, 26, 35));
                if (!feed.ImageUrl.IsEmpty())
                {
                    _ = builder.WithImageUrl(feed.ImageUrl);
                }
                _ = builder.WithTitle($"New update{(updatedFeeds.Count > 1 ? "s" : "")} for \"{guildFeed.Name}".TruncateTo(255, "") + "\"");
                DateTime latestUpdateUTC = DateTime.MinValue;
                foreach (FeedItem feedItem in updatedFeeds)
                {
                    if (feedItem.PublishingDate.HasValue && feedItem.PublishingDate.Value.ToUniversalTime() > latestUpdateUTC)
                    {
                        latestUpdateUTC = feedItem.PublishingDate.Value.ToUniversalTime();
                    }
                    string fieldName = feedItem.PublishingDate.HasValue
                        ? feedItem.PublishingDate.Value.ToLocalTime().ToString()
                        : DateTime.Now.ToString();
                    string author = feedItem.Author;
                    if (author.IsEmpty())
                    {
                        if (feed.Type == FeedType.Rss_1_0)
                        {
                            author = (feedItem.SpecificItem as Rss10FeedItem)?.DC?.Creator;
                        }
                        else if (feed.Type == FeedType.Rss_2_0)
                        {
                            author = (feedItem.SpecificItem as Rss20FeedItem)?.DC?.Creator;
                        }
                    }
                    author = !author.IsEmptyOrWhiteSpace() ? $"{author.Trim()}: " : string.Empty;
                    string maskedLink = $"[{author}{ParseHtml(feedItem.Title).Trim()}]({feedItem.Link})";
                    string content    = ParseHtml(feedItem.Description).Trim();
                    if (content.IsEmptyOrWhiteSpace())
                    {
                        content = ParseHtml(feedItem.Content).Trim();
                    }
                    content = content.IsEmptyOrWhiteSpace() ? "[...]" : content.TruncateTo(250, "");
                    string fieldContent = $"{maskedLink}{Environment.NewLine}*{content}".TruncateTo(1023, "") + "*"; // Embed field value must be <= 1024 characters
                    _ = builder.AddField(fieldName, fieldContent, true);
                }
                _ = await(channel?.SendMessageAsync("", false, builder.Build())).ConfigureAwait(false);
                if (latestUpdateUTC > DateTime.MinValue)
                {
                    guildFeed.LastUpdate = latestUpdateUTC;
                    _ = dbContext.Feeds.Update(guildFeed);
                    _ = await dbContext.SaveChangesAsync().ConfigureAwait(false);
                }
            }
        }
示例#10
0
        private async Task GetFeedUpdateAsync(Models.Feed guildFeed, bool getLatest = false)
        {
            if (!_discordClient.Guilds.TryGetValue(guildFeed.GuildID, out DiscordGuild guild))
            {
                return;
            }
            DiscordChannel channel = guild?.GetChannel(guildFeed.ChannelID);

            if (guild == null || channel == null || guildFeed.URL.IsEmpty())
            {
                return;
            }
            Feed feed;

            try
            {
                feed = await FeedReader.ReadAsync(guildFeed.URL);
            }
            catch (Exception ex)
            {
                _logger.LogWarning(ex, $"Error getting feeds for {guildFeed.URL} in {guild.Name}:{channel.Name}");
                return;
            }
            if (feed == null || feed.Items == null || feed.Items.Count < 1)
            {
                return;
            }

            DateTime lastUpdateUTC          = guildFeed.LastUpdate ?? DateTime.UtcNow.Subtract(TimeSpan.FromMinutes(updateIntervallMinutes));
            IEnumerable <FeedItem> allFeeds = feed?.Items?.Where(x => x.PublishingDate.HasValue);
            var updatedFeeds = allFeeds?.Where(x => x.PublishingDate.Value.ToUniversalTime() > lastUpdateUTC)
                               .OrderBy(x => x.PublishingDate)
                               .ToList();

            if (updatedFeeds != null && updatedFeeds.Count == 0 && getLatest)
            {
                updatedFeeds = allFeeds.OrderByDescending(x => x.PublishingDate)
                               .Take(1)
                               .ToList();
            }
            if (updatedFeeds != null && updatedFeeds.Count > 0)
            {
                DateTime latestUpdateUTC = DateTime.MinValue;
                foreach (FeedItem feedItem in updatedFeeds)
                {
                    var builder = new DiscordEmbedBuilder();
                    builder.WithColor(DiscordColor.PhthaloGreen);

                    builder.WithTitle($"New update for \"{guildFeed.Name}".TruncateTo(255, "") + "\"");
                    var publishingDate = feedItem.PublishingDate.Value.ToUniversalTime();
                    if (feedItem.PublishingDate.HasValue && publishingDate > latestUpdateUTC)
                    {
                        latestUpdateUTC = publishingDate;
                    }
                    string fieldName = feedItem.PublishingDate.HasValue
                        ? feedItem.PublishingDate.Value.ToLocalTime().ToString()
                        : DateTime.Now.ToString();
                    string author = feedItem.Author;
                    if (author.IsEmpty())
                    {
                        if (feed.Type == FeedType.Rss_1_0)
                        {
                            author = (feedItem.SpecificItem as Rss10FeedItem)?.DC?.Creator;
                        }
                        else if (feed.Type == FeedType.Rss_2_0)
                        {
                            author = (feedItem.SpecificItem as Rss20FeedItem)?.DC?.Creator;
                        }
                    }
                    author = author?.Trim() ?? string.Empty;
                    (string content, string imgLink) = ParseHtml(feedItem.Description);
                    if (content.IsEmptyOrWhiteSpace())
                    {
                        (content, imgLink) = ParseHtml(feedItem.Content);
                    }
                    string title       = feedItem.Title.Trim();
                    string description = title != content
                        ? $"{title}:\n{content}"
                        : content;
                    description = description.IsEmptyOrWhiteSpace() ? "[...]" : description.TruncateTo(500, $" [[...]]({feedItem.Link})");
                    builder.WithDescription(description);
                    builder.WithUrl(feedItem.Link);
                    builder.WithFooter($"{feed.Title} {publishingDate}", feed.ImageUrl);
                    if (!imgLink.IsEmptyOrWhiteSpace() && imgLink.ToLowerInvariant().StartsWith("http"))
                    {
                        builder.WithImageUrl(imgLink);
                    }
                    try
                    {
                        await(channel?.SendMessageAsync(builder.Build()));
                    }
                    catch (Exception ex)
                    {
                        _logger.LogError(ex, $"Error with posting updates to feed {guildFeed.URL} in {guild.Name}.");
                    }
                }

                if (latestUpdateUTC > DateTime.MinValue)
                {
                    guildFeed.LastUpdate = latestUpdateUTC;
                    _dbContext.Feeds.Update(guildFeed);
                    await _dbContext.SaveChangesAsync();
                }
            }
        }
        void RSSClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            try
            {
                string xml = e.Result;
                XmlReader reader = XmlReader.Create(new StringReader(xml));
                try
                {
                    SyndicationFeed feed = SyndicationFeed.Load(reader);

                    List<SyndicationItem> listaFeeds = feed.Items.ToList();
                    ObservableCollection<Models.Feed> RSSData = new ObservableCollection<Models.Feed>();
                    foreach (SyndicationItem si in listaFeeds)
                    {
                        Models.Feed oFeed = new Models.Feed();
                        oFeed.Id = si.Id;
                        oFeed.Titulo = si.Title.Text;
                        oFeed.FechaPublicacion = si.PublishDate.ToString("dd/MM/yyyy");
                        oFeed.ContenidoHtml = "<html><body>" + common.RetornarContenidoFeed(si.Content) + "</body></html>";

                        foreach (SyndicationLink sl in feed.Items.ToList()[0].Links)
                        {
                            if (sl.RelationshipType == "alternate")
                            {
                                oFeed.Link = sl.Uri.ToString();
                            }
                        }

                        RSSData.Add(oFeed);
                    }

                    RSSList.ItemsSource = RSSData;
                    grabarFeeds(RSSData);

                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                SystemTray.ProgressIndicator.IsVisible = false;
            }
            catch (System.Reflection.TargetInvocationException tiEx)
            {
                RSSList.ItemsSource = leerFeeds();
            }
        }
        private ObservableCollection<Models.Feed> leerFeeds()
        {
            ObservableCollection<Models.Feed> RSSData = new ObservableCollection<Models.Feed>();

            
            for (int i = 1;i<=10;i++)
            {
                String strArchivoId = "ArchivoId" + i.ToString();
                String strArchivoTitulo = "ArchivoTitulo" + i.ToString();
                String strArchivoFecha = "ArchivoFecha" + i.ToString();
                String strArchivoLink = "ArchivoLink" + i.ToString();
                String strArchivoContenidoHtml = "ArchivoContenidoHtml" + i.ToString();

                Models.Feed oFeed;

                if (IsolatedStorageSettings.ApplicationSettings.Contains(strArchivoId))
                {
                    oFeed = new Models.Feed();
                    oFeed.Id = IsolatedStorageSettings.ApplicationSettings[strArchivoId].ToString();
                }
                else
                {
                    break;
                }
                
                if (IsolatedStorageSettings.ApplicationSettings.Contains(strArchivoTitulo))
                {
                    oFeed.Titulo = IsolatedStorageSettings.ApplicationSettings[strArchivoTitulo].ToString();
                }

                if (IsolatedStorageSettings.ApplicationSettings.Contains(strArchivoFecha))
                {
                    oFeed.FechaPublicacion = IsolatedStorageSettings.ApplicationSettings[strArchivoFecha].ToString();
                }

                if (IsolatedStorageSettings.ApplicationSettings.Contains(strArchivoLink))
                {
                    oFeed.Link = IsolatedStorageSettings.ApplicationSettings[strArchivoLink].ToString();
                }

                if (IsolatedStorageSettings.ApplicationSettings.Contains(strArchivoContenidoHtml))
                {
                    oFeed.ContenidoHtml = IsolatedStorageSettings.ApplicationSettings[strArchivoContenidoHtml].ToString();
                }

                RSSData.Add(oFeed);
            }
            SystemTray.ProgressIndicator.IsVisible = false;

            return RSSData;
        }
示例#13
0
 public async Task UpdateFeed(Models.Feed feed)
 {
     _context.Feeds.Update(feed);
     await _context.SaveChangesAsync();
 }
示例#14
0
 public async Task AddFeed(Models.Feed feed)
 {
     _context.Feeds.Add(feed);
     await _context.SaveChangesAsync();
 }