Пример #1
0
        static RssUtility()
        {
            LinkUtility.TryAddHandler(LinkType.RSS, async(link, channel, cancellationToken) =>
            {
                var listUrls = new List <string>();
                var url      = link.Value;
                var feed     = FeedHelper.ParseRss(url);
                var items    = feed.Items
                               .Where(item => !LinkUtility.LinkItemExists(link, item.Guid, StringComparison.CurrentCultureIgnoreCase))
                               .Where(item => item.PublishDate.ToUniversalTime() > link.Date.ToUniversalTime())
                               .Reverse();

                if (items.Count() > 0)
                {
                    var retryCount = 0;
                    for (int i = 0; i < items.Count(); i++)
                    {
                        if (cancellationToken.IsCancellationRequested)
                        {
                            return(listUrls);
                        }

                        var item = items.ElementAt(i);
                        try
                        {
                            Log.Info($"Posting \"{item.Title}\" ({LinkType.RSS.ToString()}) in \"{link.Guild.Name}:{link.Channel.Name}\""); // Posting "10,408 Mysterious Necklaces" (Reddit) in "Team Aqua:general"
                            await channel.EmbedAsync(ParseEmbed(item.Link, channel.Guild, feed.Channel?.Title),
                                                     options: new RequestOptions()
                            {
                                RetryMode = RetryMode.AlwaysFail
                            }
                                                     );
                            listUrls.Add(item.Guid ?? item.Link);
                            retryCount = 0;
                        }
                        catch (Exception ex)
                        {
                            if (!await LinkUtility.SendRetryLinkMessageAsync(
                                    link.Type,
                                    retryCount++,
                                    ex is Discord.Net.RateLimitedException ? null : ex
                                    ))
                            {
                                break;
                            }
                            i--;
                        }
                    }
                }
                return(listUrls);
            });
        }
Пример #2
0
        static BdoLinkUtility()
        {
            LinkUtility.TryAddHandler(LinkType.BDO, async(link, channel, cancellationToken) =>
            {
                var listUrls = new List <string>();
                var url      = link.Value;
                var feed     = FeedHelper.ParseRss(url);
                var items    = feed.Items
                               .Where(item => !LinkUtility.LinkItemExists(link, item.Guid, StringComparison.CurrentCultureIgnoreCase))
                               .Where(item => item.PublishDate.ToUniversalTime() > link.Date.ToUniversalTime())
                               .Reverse();

                // Retrieve the last article id
                ulong news_id = 0;
                Ditto.Database.Do((uow) =>
                {
                    ulong.TryParse(uow.Configs.GetBdoNewsIdentifier(channel.Guild).Value, out news_id);
                }, complete: false);

                if (items.Count() > 0)
                {
                    var retryCount = 0;
                    for (int i = 0; i < items.Count(); i++)
                    {
                        if (cancellationToken.IsCancellationRequested)
                        {
                            return(listUrls);
                        }

                        var item = items.ElementAt(i);
                        if (ulong.TryParse(new string(item.Link.Reverse().Before('.').Reverse().ToArray()).Replace("/", "").Replace("\\", ""), out ulong identifier))
                        {
                            // Skip older posts
                            if (news_id > identifier)
                            {
                                continue;
                            }
                            else
                            {
                                // Add/update the news identifier for this guild
                                Ditto.Database.Do(uow =>
                                {
                                    uow.Configs.SetBdoNewsIdentifier(channel.Guild, identifier);
                                }, complete: true);
                            }
                        }

                        try
                        {
                            Log.Debug($"[BDO] Posting \"{item.Title}\" ({LinkType.BDO.ToString()}) in \"{link.Guild.Name}:{link.Channel.Name}\"");
                            await channel.EmbedAsync(ParseEmbed(item.Link, channel.Guild, feed.Channel?.Title),
                                                     options: new RequestOptions()
                            {
                                RetryMode = RetryMode.AlwaysFail
                            }
                                                     );
                            listUrls.Add(item.Guid ?? item.Link);
                            retryCount = 0;
                        }
                        catch (Exception ex)
                        {
                            if (!await LinkUtility.SendRetryLinkMessageAsync(
                                    link.Type,
                                    retryCount++,
                                    ex is Discord.Net.RateLimitedException ? null : ex
                                    ))
                            {
                                break;
                            }
                            i--;
                        }
                    }
                }
                return(listUrls);
            });
        }