Example #1
0
        public static async Task SubscribeAsync(this DatabaseContextBuilder dbb, ulong gid, ulong cid, string url, string name = null)
        {
            var newest = RssService.GetFeedResults(url)?.FirstOrDefault();

            if (newest is null)
            {
                throw new Exception("Can't load the feed entries.");
            }

            using (DatabaseContext db = dbb.CreateContext())
            {
                DatabaseRssFeed feed = db.RssFeeds.SingleOrDefault(f => f.Url == url);
                if (feed is null)
                {
                    feed = new DatabaseRssFeed()
                    {
                        Url         = url,
                        LastPostUrl = newest.Links[0].Uri.ToString()
                    };
                    db.RssFeeds.Add(feed);
                    await db.SaveChangesAsync();
                }

                db.RssSubscriptions.Add(new DatabaseRssSubscription()
                {
                    ChannelId = cid,
                    GuildId   = gid,
                    Id        = feed.Id,
                    Name      = name ?? url
                });

                await db.SaveChangesAsync();
            }
        }
Example #2
0
        public Task WmRssAsync(CommandContext ctx)
        {
            IReadOnlyList <SyndicationItem> res = RssService.GetFeedResults("http://worldmafia.net/forums/-/index.rss");

            if (res == null)
            {
                throw new CommandFailedException("An error occured while reaching WM forum. Possibly Pakistani didn't pay this month?");
            }

            return(RssService.SendFeedResultsAsync(ctx.Channel, res));
        }
Example #3
0
        public Task NewsRssAsync(CommandContext ctx)
        {
            IReadOnlyList <SyndicationItem> res = RssService.GetFeedResults("https://news.google.com/news/rss/headlines/section/topic/WORLD?ned=us&hl=en");

            if (res == null)
            {
                throw new CommandFailedException("Error getting world news.");
            }

            return(RssService.SendFeedResultsAsync(ctx.Channel, res));
        }
Example #4
0
        public Task NewsRssAsync(CommandContext ctx)
        {
            var res = RssService.GetFeedResults("");

            if (res is null)
            {
                throw new CommandFailedException("Error getting world news.");
            }

            return(RssService.SendFeedResultsAsync(ctx.Channel, res));
        }
Example #5
0
        public Task RssAsync(CommandContext ctx, [Description("RSS feed URL.")] Uri url)
        {
            if (!RssService.IsValidFeedUrl(url.AbsoluteUri))
            {
                throw new InvalidCommandUsageException("No results found for given URL (maybe forbidden?).");
            }

            var res = RssService.GetFeedResults(url.AbsoluteUri);

            if (res is null)
            {
                throw new CommandFailedException("Error getting feed from given URL.");
            }

            return(RssService.SendFeedResultsAsync(ctx.Channel, res));
        }
Example #6
0
        public Task ExecuteGroupAsync(CommandContext ctx,
                                      [Description("RSS URL.")] Uri url)
        {
            if (!RssService.IsValidFeedURL(url.AbsoluteUri))
            {
                throw new InvalidCommandUsageException("No results found for given URL (maybe forbidden?).");
            }

            IReadOnlyList <SyndicationItem> res = RssService.GetFeedResults(url.AbsoluteUri);

            if (res == null)
            {
                throw new CommandFailedException("Error getting feed from given URL.");
            }

            return(RssService.SendFeedResultsAsync(ctx.Channel, res));
        }
Example #7
0
        public void GetFeedResultsTest()
        {
            IReadOnlyList <SyndicationItem> results;

            results = RssService.GetFeedResults("https://www.reddit.com/r/aww/.rss");
            Assert.IsNotNull(results);
            Assert.IsTrue(results.Any());
            Assert.AreEqual(5, results.Count);
            CollectionAssert.AllItemsAreNotNull(results);

            results = RssService.GetFeedResults("https://www.reddit.com/r/aww/.rss", 10);
            Assert.IsNotNull(results);
            Assert.IsTrue(results.Any());
            Assert.AreEqual(10, results.Count);
            CollectionAssert.AllItemsAreNotNull(results);

            results = RssService.GetFeedResults("https://www.reddit.com/r/MrRobot/.rss", 10);
            Assert.IsNotNull(results);
            Assert.IsTrue(results.Any());
            Assert.AreEqual(10, results.Count);
            CollectionAssert.AllItemsAreNotNull(results);

            results = RssService.GetFeedResults("https://news.google.com/news/rss/headlines/section/topic/WORLD?ned=us&hl=en", 1);
            Assert.IsNotNull(results);
            Assert.IsTrue(results.Any());
            CollectionAssert.AllItemsAreNotNull(results);

            results = RssService.GetFeedResults("https://www.youtube.com/feeds/videos.xml?channel_id=UCA5u8UquvO44Jcd3wZApyDg", 1);
            Assert.IsNotNull(results);
            Assert.IsTrue(results.Any());
            CollectionAssert.AllItemsAreNotNull(results);

            results = RssService.GetFeedResults("https://non_existing_URL.com/asdsada/");
            Assert.IsNull(results);

            Assert.Throws <ArgumentException>(() => RssService.GetFeedResults(null));
            Assert.Throws <ArgumentException>(() => RssService.GetFeedResults(""));
            Assert.Throws <ArgumentException>(() => RssService.GetFeedResults(" "));
            Assert.Throws <ArgumentException>(() => RssService.GetFeedResults("\n"));

            string aww = "https://www.reddit.com/r/aww/new/.rss";

            Assert.Throws <ArgumentException>(() => RssService.GetFeedResults(aww, -1));
            Assert.Throws <ArgumentException>(() => RssService.GetFeedResults(aww, 0));
            Assert.Throws <ArgumentException>(() => RssService.GetFeedResults(aww, 21));
        }
Example #8
0
        private async Task SearchAndSendResultsAsync(CommandContext ctx, string sub, RedditCategory category)
        {
            string url = RedditService.GetFeedUrlForSubreddit(sub, category, out string rsub);

            if (url is null)
            {
                throw new CommandFailedException("That subreddit doesn't exist.");
            }

            var res = RssService.GetFeedResults(url);

            if (res is null)
            {
                throw new CommandFailedException($"Failed to get the data from that subreddit ({Formatter.Bold(rsub)}).");
            }

            await RssService.SendFeedResultsAsync(ctx.Channel, res);
        }
Example #9
0
        public Task ExecuteGroupAsync(CommandContext ctx,
                                      [Description("Subreddit.")] string sub = "all")
        {
            string url = RssService.GetFeedURLForSubreddit(sub, out string rsub);

            if (url == null)
            {
                throw new CommandFailedException("That subreddit doesn't exist.");
            }

            IReadOnlyList <SyndicationItem> res = RssService.GetFeedResults(url);

            if (res == null)
            {
                throw new CommandFailedException($"Failed to get the data from that subreddit ({Formatter.Bold(rsub)}).");
            }

            return(RssService.SendFeedResultsAsync(ctx.Channel, res));
        }
Example #10
0
        public static async Task <bool> TryAddSubscriptionAsync(this DBService db, ulong cid, string url, string qname = null)
        {
            var newest = RssService.GetFeedResults(url)?.FirstOrDefault();

            if (newest == null)
            {
                return(false);
            }

            int?sid = null;
            await db.ExecuteTransactionAsync(async (con, tsem) => {
                int?id = null;
                using (var cmd = con.CreateCommand()) {
                    cmd.CommandText = "INSERT INTO gf.feeds VALUES (DEFAULT, @url, @savedurl) ON CONFLICT (url) DO UPDATE SET url = EXCLUDED.url RETURNING id;";
                    cmd.Parameters.AddWithValue("url", NpgsqlDbType.Text, url);
                    cmd.Parameters.AddWithValue("savedurl", NpgsqlDbType.Text, newest.Links[0].Uri.ToString());

                    object res = await cmd.ExecuteScalarAsync().ConfigureAwait(false);
                    if (res != null && !(res is DBNull))
                    {
                        id = (int)res;
                    }
                }

                using (var cmd = con.CreateCommand()) {
                    cmd.CommandText = "INSERT INTO gf.subscriptions VALUES (@id, @cid, @qname) ON CONFLICT DO NOTHING RETURNING id;";
                    cmd.Parameters.Add(new NpgsqlParameter <int>("id", id.Value));
                    cmd.Parameters.Add(new NpgsqlParameter <long>("cid", (long)cid));
                    cmd.Parameters.Add(new NpgsqlParameter <string>("qname", qname));

                    object res = await cmd.ExecuteScalarAsync().ConfigureAwait(false);
                    if (res != null && !(res is DBNull))
                    {
                        sid = (int)res;
                    }
                }
            });

            return(sid.HasValue);
        }