Example #1
0
        public async Task ProcessRssFeeds()
        {
            // Asynchronous ForEach
            List <Task> feeds = new List <Task>();
            // More Optimized since evaluation is only done once.
            List <Guild> guilds = await _database.Guilds.Include(x => x.RssFeeds).Include(x => x.RssRecentUrls).Include(x => x.RssRoles).Where(x => x.EnableRssFeed && x.RssFeeds.Count > 0).ToListAsync();

            await _log.LogMessage(new LogMessage(LogSeverity.Debug, "RSS", "Processing RSS Service: " + guilds.Count + " guilds"));

            foreach (var guild in guilds)
            {
                await _log.LogMessage(new LogMessage(LogSeverity.Debug, "RSS", "Guild: Updating " + guild.RssFeeds.Count + " feeds"));

                foreach (var feed in guild.RssFeeds)
                {
                    await _log.LogMessage(new LogMessage(LogSeverity.Debug, "RSS", "Feed: Readying " + feed.FeedUrl));

                    feeds.Add(Task.Run(async() =>
                    {
                        await _log.LogMessage(new LogMessage(LogSeverity.Debug, "RSS", "Feed: Updating " + feed.FeedUrl));
                        try
                        {
                            RssDataObject rssData   = await GetRssAsync(feed.FeedUrl);
                            List <RssItem> rssPosts = await ExcludeRecentPosts(feed, rssData);

                            SocketGuild socketGuild   = _client.GetGuild(Convert.ToUInt64(guild.GuildId));
                            List <Object> rssMessages = await BuildRssFeedAsync(feed, rssPosts, socketGuild);

                            var channel = socketGuild.GetChannel(feed.ChannelId) as SocketTextChannel;
                            await SendToChannel(rssMessages, channel, feed, socketGuild);
                            // This all happens behind scenes, so doesn't inhibit the performance
                            await SaveRecentUrls(feed.Id, feed.Guild.Id, rssPosts);
                        }
                        catch (Exception ex)
                        {
                            await _log.LogMessage(new LogMessage(LogSeverity.Error, "RSS", ex.Message, ex));
                        }
                    }));
                }
            }

            // Run each feed in parallel and wait until all are completed
            await Task.WhenAll(feeds);

            await _database.SaveChangesAsync();
        }
Example #2
0
        private async Task <List <RssItem> > ExcludeRecentPosts(RssFeed feed, RssDataObject rssData)
        {
            var            recentUrls = feed.Guild.RssRecentUrls;
            List <RssItem> posts      = new List <RssItem>();

            await Task.Run(() =>
            {
                foreach (var item in rssData.Data.Items.Take(10).Reverse())
                {
                    if (!(recentUrls.Select(x => x.RecentUrl).Contains(item.Link)))
                    {
                        posts.Add(item);
                    }
                }
            });

            return(posts);
        }